You are here

class NumberListField in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/options/src/Plugin/views/argument/NumberListField.php \Drupal\options\Plugin\views\argument\NumberListField
  2. 9 core/modules/options/src/Plugin/views/argument/NumberListField.php \Drupal\options\Plugin\views\argument\NumberListField

Argument handler for list field to show the human readable name in the summary.

Plugin annotation

@ViewsArgument("number_list_field");

Hierarchy

Expanded class hierarchy of NumberListField

File

core/modules/options/src/Plugin/views/argument/NumberListField.php, line 20

Namespace

Drupal\options\Plugin\views\argument
View source
class NumberListField extends NumericArgument {
  use FieldAPIHandlerTrait;

  /**
   * Stores the allowed values of this field.
   *
   * @var array
   */
  protected $allowedValues = NULL;

  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
    $field_storage = $this
      ->getFieldStorageDefinition();
    $this->allowedValues = options_allowed_values($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;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldAPIHandlerTrait::$entityFieldManager protected property The entity field manager.
FieldAPIHandlerTrait::$fieldDefinition protected property The field definition.
FieldAPIHandlerTrait::$fieldStorageDefinition protected property The field storage definition.
FieldAPIHandlerTrait::getEntityFieldManager protected function Returns the entity field manager.
FieldAPIHandlerTrait::getFieldDefinition protected function Gets the field definition.
FieldAPIHandlerTrait::getFieldStorageDefinition protected function Gets the field storage configuration.
NumberListField::$allowedValues protected property Stores the allowed values of this field.
NumberListField::buildOptionsForm public function Overrides NumericArgument::buildOptionsForm
NumberListField::defineOptions protected function Overrides NumericArgument::defineOptions
NumberListField::init public function
NumberListField::summaryName public function
NumericArgument::$value public property The actual value which is used for querying.
NumericArgument::getContextDefinition public function
NumericArgument::getSortName public function
NumericArgument::query public function 1
NumericArgument::title public function 2
NumericArgument::titleQuery public function Override for specific title lookups. 4