class Serialized

Same name and namespace in other branches
  1. 11.x 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

Expanded class hierarchy of Serialized

Related topics

5 string references to 'Serialized'
DatabaseBackend::doSetMultiple in core/lib/Drupal/Core/Cache/DatabaseBackend.php
Stores multiple items in the persistent cache.
dblog_views_data in core/modules/dblog/dblog.views.inc
Implements hook_views_data().
EntitySerializedField::baseFieldDefinitions in core/modules/system/tests/modules/entity_test/src/Entity/EntitySerializedField.php
PhpSerialize::getFileExtension in core/lib/Drupal/Component/Serialization/PhpSerialize.php
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;
  }

}

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