You are here

class Serialized in Drupal 10

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

Field handler to show data of serialized fields.

Plugin annotation

@ViewsField("serialized");

Hierarchy

  • class \Drupal\views\Plugin\views\field\Serialized extends \Drupal\views\Plugin\views\field\FieldPluginBase

Expanded class hierarchy of Serialized

2 string references to 'Serialized'
EntitySerializedField::baseFieldDefinitions in core/modules/system/tests/modules/entity_test/src/Entity/EntitySerializedField.php
Provides base field definitions for an entity type.
views.field.schema.yml in core/modules/views/config/schema/views.field.schema.yml
core/modules/views/config/schema/views.field.schema.yml

File

core/modules/views/src/Plugin/views/field/Serialized.php, line 15

Namespace

Drupal\views\Plugin\views\field
View source
class Serialized extends FieldPluginBase {

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['format'] = [
      'default' => 'unserialized',
    ];
    $options['key'] = [
      'default' => '',
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $form['format'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Display format'),
      '#description' => $this
        ->t('How should the serialized data be displayed. You can choose a custom array/object key or a print_r on the full output.'),
      '#options' => [
        'unserialized' => $this
          ->t('Full data (unserialized)'),
        'serialized' => $this
          ->t('Full data (serialized)'),
        'key' => $this
          ->t('A certain key'),
      ],
      '#default_value' => $this->options['format'],
    ];
    $form['key'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Which key should be displayed'),
      '#default_value' => $this->options['key'],
      '#states' => [
        'visible' => [
          ':input[name="options[format]"]' => [
            'value' => 'key',
          ],
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function validateOptionsForm(&$form, FormStateInterface $form_state) {

    // Require a key if the format is key.
    if ($form_state
      ->getValue([
      'options',
      'format',
    ]) == 'key' && $form_state
      ->getValue([
      'options',
      'key',
    ]) == '') {
      $form_state
        ->setError($form['key'], $this
        ->t('You have to enter a key if you want to display a key of the data.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $value = $values->{$this->field_alias};
    if ($this->options['format'] == 'unserialized') {
      return $this
        ->sanitizeValue(print_r(unserialize($value), TRUE));
    }
    elseif ($this->options['format'] == 'key' && !empty($this->options['key'])) {
      $value = (array) unserialize($value);
      return $this
        ->sanitizeValue($value[$this->options['key']]);
    }
    return $value;
  }

}

Members