DblogMessage.php

Same filename in other branches
  1. 9 core/modules/dblog/src/Plugin/views/field/DblogMessage.php
  2. 8.9.x core/modules/dblog/src/Plugin/views/field/DblogMessage.php
  3. 10 core/modules/dblog/src/Plugin/views/field/DblogMessage.php

Namespace

Drupal\dblog\Plugin\views\field

File

core/modules/dblog/src/Plugin/views/field/DblogMessage.php

View source
<?php

namespace Drupal\dblog\Plugin\views\field;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Attribute\ViewsField;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;

/**
 * Provides a field handler that renders a log event with replaced variables.
 *
 * @ingroup views_field_handlers
 */
class DblogMessage extends FieldPluginBase {
    
    /**
     * {@inheritdoc}
     */
    public function init(ViewExecutable $view, DisplayPluginBase $display, ?array &$options = NULL) {
        parent::init($view, $display, $options);
        if ($this->options['replace_variables']) {
            $this->additional_fields['variables'] = 'variables';
        }
    }
    
    /**
     * {@inheritdoc}
     */
    protected function defineOptions() {
        $options = parent::defineOptions();
        $options['replace_variables'] = [
            'default' => TRUE,
        ];
        return $options;
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildOptionsForm(&$form, FormStateInterface $form_state) {
        parent::buildOptionsForm($form, $form_state);
        $form['replace_variables'] = [
            '#title' => $this->t('Replace variables'),
            '#type' => 'checkbox',
            '#default_value' => $this->options['replace_variables'],
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function render(ResultRow $values) {
        $value = $this->getValue($values);
        if ($this->options['replace_variables']) {
            $variables = unserialize($this->getvalue($values, 'variables'));
            return new FormattableMarkup($value, (array) $variables);
        }
        else {
            return $this->sanitizeValue($value);
        }
    }

}

Classes

Title Deprecated Summary
DblogMessage Provides a field handler that renders a log event with replaced variables.

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