Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / dblog / src / Plugin / views / field / DblogMessage.php
1 <?php
2
3 namespace Drupal\dblog\Plugin\views\field;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Plugin\views\field\FieldPluginBase;
8 use Drupal\views\ResultRow;
9 use Drupal\views\ViewExecutable;
10 use Drupal\views\Plugin\views\display\DisplayPluginBase;
11
12 /**
13  * Provides a field handler that renders a log event with replaced variables.
14  *
15  * @ingroup views_field_handlers
16  *
17  * @ViewsField("dblog_message")
18  */
19 class DblogMessage extends FieldPluginBase {
20
21   /**
22    * {@inheritdoc}
23    */
24   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
25     parent::init($view, $display, $options);
26
27     if ($this->options['replace_variables']) {
28       $this->additional_fields['variables'] = 'variables';
29     }
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function defineOptions() {
36     $options = parent::defineOptions();
37     $options['replace_variables'] = ['default' => TRUE];
38
39     return $options;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
46     parent::buildOptionsForm($form, $form_state);
47
48     $form['replace_variables'] = [
49       '#title' => $this->t('Replace variables'),
50       '#type' => 'checkbox',
51       '#default_value' => $this->options['replace_variables'],
52     ];
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function render(ResultRow $values) {
59     $value = $this->getValue($values);
60
61     if ($this->options['replace_variables']) {
62       $variables = unserialize($this->getvalue($values, 'variables'));
63       return new FormattableMarkup($value, (array) $variables);
64     }
65     else {
66       return $this->sanitizeValue($value);
67     }
68   }
69
70 }