3 namespace Drupal\options\Plugin\views\argument;
5 use Drupal\Core\Field\AllowedTagsXssTrait;
6 use Drupal\Core\Field\FieldFilteredMarkup;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\views\FieldAPIHandlerTrait;
9 use Drupal\views\ViewExecutable;
10 use Drupal\views\Plugin\views\display\DisplayPluginBase;
11 use Drupal\views\Plugin\views\argument\StringArgument;
14 * Argument handler for list field to show the human readable name in summary.
16 * @ingroup views_argument_handlers
18 * @ViewsArgument("string_list_field")
20 class StringListField extends StringArgument {
22 use AllowedTagsXssTrait;
23 use FieldAPIHandlerTrait;
26 * Stores the allowed values of this field.
30 protected $allowedValues = NULL;
35 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
36 parent::init($view, $display, $options);
38 $field_storage = $this->getFieldStorageDefinition();
39 $this->allowedValues = options_allowed_values($field_storage);
45 protected function defineOptions() {
46 $options = parent::defineOptions();
48 $options['summary']['contains']['human'] = ['default' => FALSE];
56 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
57 parent::buildOptionsForm($form, $form_state);
59 $form['summary']['human'] = [
60 '#title' => $this->t('Display list value as human readable'),
61 '#type' => 'checkbox',
62 '#default_value' => $this->options['summary']['human'],
65 ':input[name="options[default_action]"]' => ['value' => 'summary'],
74 public function summaryName($data) {
75 $value = $data->{$this->name_alias};
76 // If the list element has a human readable name show it.
77 if (isset($this->allowedValues[$value]) && !empty($this->options['summary']['human'])) {
78 $value = $this->allowedValues[$value];
80 return FieldFilteredMarkup::create($this->caseTransform($value, $this->options['case']));