NumberListField.php

Same filename and directory in other branches
  1. 11.x core/modules/options/src/Plugin/views/argument/NumberListField.php
  2. 10 core/modules/options/src/Plugin/views/argument/NumberListField.php
  3. 9 core/modules/options/src/Plugin/views/argument/NumberListField.php
  4. 8.9.x core/modules/options/src/Plugin/views/argument/NumberListField.php

Namespace

Drupal\options\Plugin\views\argument

File

core/modules/options/src/Plugin/views/argument/NumberListField.php

View source
<?php

namespace Drupal\options\Plugin\views\argument;

use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\options\OptionsAllowedValuesInterface;
use Drupal\views\Attribute\ViewsArgument;
use Drupal\views\FieldAPIHandlerTrait;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\argument\NumericArgument;

/**
 * Argument handler for list field to show human readable name in the summary.
 *
 * @ingroup views_argument_handlers
 */
class NumberListField extends NumericArgument {
  use FieldAPIHandlerTrait;
  
  /**
   * Stores the allowed values of this field.
   *
   * @var array
   */
  protected $allowedValues = NULL;
  
  /**
   * The options allowed values service.
   */
  protected OptionsAllowedValuesInterface $optionsAllowedValues;
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ?OptionsAllowedValuesInterface $options_allowed_values = NULL) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    if (!$options_allowed_values) {
      @trigger_error('Calling ' . __METHOD__ . '() without the $options_allowed_values argument is deprecated in drupal:11.5.0 and it will be required in drupal:12.0.0. See https://www.drupal.org/node/3572185', E_USER_DEPRECATED);
      $options_allowed_values = \Drupal::service(OptionsAllowedValuesInterface::class);
    }
    $this->optionsAllowedValues = $options_allowed_values;
  }
  
  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, ?array &$options = NULL) {
    parent::init($view, $display, $options);
    $field_storage = $this->getFieldStorageDefinition();
    $this->allowedValues = $this->optionsAllowedValues
      ->getAllowedValues($field_storage);
  }
  
  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['summary']['contains']['human'] = [
      'default' => FALSE,
    ];
    return $options;
  }
  
  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $form['summary']['human'] = [
      '#title' => $this->t('Display list value as human readable'),
      '#type' => 'checkbox',
      '#default_value' => $this->options['summary']['human'],
      '#states' => [
        'visible' => [
          ':input[name="options[default_action]"]' => [
            'value' => 'summary',
          ],
        ],
      ],
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function summaryName($data) {
    $value = $data->{$this->name_alias};
    // If the list element has a human readable name show it.
    if (isset($this->allowedValues[$value]) && !empty($this->options['summary']['human'])) {
      return FieldFilteredMarkup::create($this->allowedValues[$value]);
    }
    else {
      return $value;
    }
  }

}

Classes

Title Deprecated Summary
NumberListField Argument handler for list field to show human readable name in the summary.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.