Skip to content
Snippets Groups Projects

Issue #3352398 by luke.leber, mysdiir, jurgenhaas, danielspeicher: New Action List Sort

Open Issue #3352398 by luke.leber, mysdiir, jurgenhaas, danielspeicher: New Action List Sort
Open Luke Leber requested to merge issue/eca-3352398:3352398-new-action-list into 3.0.x
Files
2
+ 124
0
<?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