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\NumericArgument;
14 * Argument handler for list field to show the human readable name in the
17 * @ingroup views_argument_handlers
19 * @ViewsArgument("number_list_field")
21 class NumberListField extends NumericArgument {
23 use AllowedTagsXssTrait;
24 use FieldAPIHandlerTrait;
27 * Stores the allowed values of this field.
31 protected $allowedValues = NULL;
36 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
37 parent::init($view, $display, $options);
39 $field_storage = $this->getFieldStorageDefinition();
40 $this->allowedValues = options_allowed_values($field_storage);
46 protected function defineOptions() {
47 $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 return FieldFilteredMarkup::create($this->allowedValues[$value]);
80 // Else, fallback to the key.