Version 1
[yaffs-website] / web / core / modules / views / src / Plugin / views / field / Custom.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\field;
4
5 use Drupal\Component\Utility\Xss;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Render\ViewsRenderPipelineMarkup;
8 use Drupal\views\ResultRow;
9
10 /**
11  * A handler to provide a field that is completely custom by the administrator.
12  *
13  * @ingroup views_field_handlers
14  *
15  * @ViewsField("custom")
16  */
17 class Custom extends FieldPluginBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public function usesGroupBy() {
23     return FALSE;
24   }
25
26   /**
27    * {@inheritdoc}
28    */
29   public function query() {
30     // do nothing -- to override the parent query.
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function defineOptions() {
37     $options = parent::defineOptions();
38
39     // Override the alter text option to always alter the text.
40     $options['alter']['contains']['alter_text'] = ['default' => TRUE];
41     $options['hide_alter_empty'] = ['default' => FALSE];
42     return $options;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
49     parent::buildOptionsForm($form, $form_state);
50
51     // Remove the checkbox
52     unset($form['alter']['alter_text']);
53     unset($form['alter']['text']['#states']);
54     unset($form['alter']['help']['#states']);
55     $form['#pre_render'][] = [$this, 'preRenderCustomForm'];
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function render(ResultRow $values) {
62     // Return the text, so the code never thinks the value is empty.
63     return ViewsRenderPipelineMarkup::create(Xss::filterAdmin($this->options['alter']['text']));
64   }
65
66   /**
67    * Prerender function to move the textarea to the top of a form.
68    *
69    * @param array $form
70    *   The form build array.
71    *
72    * @return array
73    *   The modified form build array.
74    */
75   public function preRenderCustomForm($form) {
76     $form['text'] = $form['alter']['text'];
77     $form['help'] = $form['alter']['help'];
78     unset($form['alter']['text']);
79     unset($form['alter']['help']);
80
81     return $form;
82   }
83
84 }