Version 1
[yaffs-website] / web / core / modules / options / src / Plugin / views / argument / StringListField.php
1 <?php
2
3 namespace Drupal\options\Plugin\views\argument;
4
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;
12
13 /**
14  * Argument handler for list field to show the human readable name in summary.
15  *
16  * @ingroup views_argument_handlers
17  *
18  * @ViewsArgument("string_list_field")
19  */
20 class StringListField extends StringArgument {
21
22   use AllowedTagsXssTrait;
23   use FieldAPIHandlerTrait;
24
25   /**
26    * Stores the allowed values of this field.
27    *
28    * @var array
29    */
30   protected $allowedValues = NULL;
31
32   /**
33    * {@inheritdoc}
34    */
35   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
36     parent::init($view, $display, $options);
37
38     $field_storage = $this->getFieldStorageDefinition();
39     $this->allowedValues = options_allowed_values($field_storage);
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function defineOptions() {
46     $options = parent::defineOptions();
47
48     $options['summary']['contains']['human'] = ['default' => FALSE];
49
50     return $options;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
57     parent::buildOptionsForm($form, $form_state);
58
59     $form['summary']['human'] = [
60       '#title' => $this->t('Display list value as human readable'),
61       '#type' => 'checkbox',
62       '#default_value' => $this->options['summary']['human'],
63       '#states' => [
64         'visible' => [
65           ':input[name="options[default_action]"]' => ['value' => 'summary'],
66         ],
67       ],
68     ];
69   }
70
71   /**
72    * {@inheritdoc}
73    */
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];
79     }
80     return FieldFilteredMarkup::create($this->caseTransform($value, $this->options['case']));
81   }
82
83 }