Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
eca
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
eca
Merge requests
!484
Issue
#3352398
by luke.leber, mysdiir, jurgenhaas, danielspeicher: New Action List Sort
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Issue
#3352398
by luke.leber, mysdiir, jurgenhaas, danielspeicher: New Action List Sort
issue/eca-3352398:3352398-new-action-list
into
3.0.x
Overview
2
Commits
9
Pipelines
9
Changes
2
Open
Luke Leber
requested to merge
issue/eca-3352398:3352398-new-action-list
into
3.0.x
2 months ago
Overview
2
Commits
9
Pipelines
9
Changes
2
Expand
Closes
#3352398
0
0
Merge request reports
Compare
3.0.x
version 9
d95359d6
1 month ago
version 8
d95359d6
1 month ago
version 7
0372ea1d
1 month ago
version 6
6e828687
1 month ago
version 5
77e51059
1 month ago
version 4
59b760c3
2 months ago
version 3
46b6c73c
2 months ago
version 2
8944f19d
2 months ago
version 1
876289e0
2 months ago
3.0.x (HEAD)
and
latest version
latest version
27924b64
9 commits,
1 month ago
version 9
d95359d6
8 commits,
1 month ago
version 8
d95359d6
8 commits,
1 month ago
version 7
0372ea1d
7 commits,
1 month ago
version 6
6e828687
6 commits,
1 month ago
version 5
77e51059
5 commits,
1 month ago
version 4
59b760c3
4 commits,
2 months ago
version 3
46b6c73c
3 commits,
2 months ago
version 2
8944f19d
2 commits,
2 months ago
version 1
876289e0
1 commit,
2 months ago
2 files
+
195
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
modules/base/src/Plugin/Action/ListSort.php
0 → 100644
+
124
−
0
Options
<?php
namespace
Drupal\eca_base\Plugin\Action
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\eca\Plugin\Action\ListOperationBase
;
use
Drupal\eca\Plugin\DataType\DataTransferObject
;
use
Drupal\eca\Plugin\ECA\PluginFormTrait
;
/**
* Sorts a list.
*
* @Action(
* id = "eca_list_sort",
* label = @Translation("List: sort items"),
* description = @Translation("Sorts a list."),
* eca_version_introduced = "2.2.0"
* )
*/
class
ListSort
extends
ListOperationBase
{
use
PluginFormTrait
;
/**
* Gets the supported sorting methods.
*
* @return array
* The supported sorting methods.
*/
public
function
getSupportedSortMethods
()
{
return
[
'asort'
=>
$this
->
t
(
'Ascending by value'
),
'arsort'
=>
$this
->
t
(
'Descending by value'
),
'ksort'
=>
$this
->
t
(
'Ascending by key'
),
'krsort'
=>
$this
->
t
(
'Descending by key'
),
'natsort'
=>
$this
->
t
(
'Natural'
),
'natcasesort'
=>
$this
->
t
(
'Natural (case insensitive)'
),
];
}
/**
* {@inheritdoc}
*/
public
function
defaultConfiguration
():
array
{
return
[
'sort_method'
=>
'sort'
,
'token_name'
=>
''
,
]
+
parent
::
defaultConfiguration
();
}
/**
* {@inheritdoc}
*/
public
function
buildConfigurationForm
(
array
$form
,
FormStateInterface
$form_state
):
array
{
$form
[
'sort_method'
]
=
[
'#type'
=>
'select'
,
'#title'
=>
$this
->
t
(
'Sort method'
),
'#options'
=>
$this
->
getSupportedSortMethods
(),
'#default_value'
=>
$this
->
configuration
[
'sort_method'
],
'#weight'
=>
0
,
];
$form
[
'token_name'
]
=
[
'#type'
=>
'textfield'
,
'#title'
=>
$this
->
t
(
'Name of token'
),
'#description'
=>
$this
->
t
(
'The sorted list will be loaded into this specified token. If this is not provided, the list will be sorted in-place.'
),
'#default_value'
=>
$this
->
configuration
[
'token_name'
],
'#weight'
=>
4
,
];
return
parent
::
buildConfigurationForm
(
$form
,
$form_state
);
}
/**
* {@inheritdoc}
*/
public
function
submitConfigurationForm
(
array
&
$form
,
FormStateInterface
$form_state
):
void
{
parent
::
submitConfigurationForm
(
$form
,
$form_state
);
$this
->
configuration
[
'sort_method'
]
=
$form_state
->
getValue
(
'sort_method'
);
$this
->
configuration
[
'token_name'
]
=
$form_state
->
getValue
(
'token_name'
);
}
/**
* {@inheritdoc}
*/
public
function
execute
():
void
{
$list
=
$this
->
getItemList
();
$items
=
(
$list
instanceof
DataTransferObject
)
?
$list
->
toArray
()
:
\iterator_to_array
(
$list
);
// Note - verbosity is intended to aid in static analysis.
switch
(
$this
->
configuration
[
'sort_method'
])
{
case
'asort'
:
\asort
(
$items
);
break
;
case
'arsort'
:
\arsort
(
$items
);
break
;
case
'ksort'
:
\ksort
(
$items
);
break
;
case
'krsort'
:
\krsort
(
$items
);
break
;
case
'natsort'
:
\natsort
(
$items
);
break
;
case
'natcasesort'
:
\natcasesort
(
$items
);
break
;
}
$dest_token
=
trim
((
string
)
$this
->
configuration
[
'token_name'
]);
if
(
!
$dest_token
)
{
$dest_token
=
trim
((
string
)
$this
->
configuration
[
'list_token'
]);
}
$this
->
tokenService
->
addTokenData
(
$dest_token
,
DataTransferObject
::
create
(
$items
));
}
}
Loading