813e4306c0987ee4a43b1547eac260b147789998
[yaffs-website] / web / core / modules / views / src / Plugin / views / area / AreaPluginBase.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\area;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ViewExecutable;
7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
8 use Drupal\views\Plugin\views\HandlerBase;
9
10 /**
11  * @defgroup views_area_handlers Views area handler plugins
12  * @{
13  * Plugins governing areas of views, such as header, footer, and empty text.
14  *
15  * Area handler plugins extend \Drupal\views\Plugin\views\area\AreaPluginBase.
16  * They must be annotated with \Drupal\views\Annotation\ViewsArea annotation,
17  * and they must be in namespace directory Plugin\views\area.
18  *
19  * @ingroup views_plugins
20  * @see plugin_api
21  */
22
23 /**
24  * Base class for area handler plugins.
25  */
26 abstract class AreaPluginBase extends HandlerBase {
27
28   /**
29    * The type of this area handler, i.e. 'header', 'footer', or 'empty'.
30    *
31    * @var string
32    */
33   public $areaType;
34
35   /**
36    * Overrides Drupal\views\Plugin\views\HandlerBase::init().
37    *
38    * Make sure that no result area handlers are set to be shown when the result
39    * is empty.
40    */
41   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
42     parent::init($view, $display, $options);
43
44     if ($this->areaType == 'empty') {
45       $this->options['empty'] = TRUE;
46     }
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function usesGroupBy() {
53     return FALSE;
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   protected function defineOptions() {
60     $options = parent::defineOptions();
61
62     $this->definition['field'] = !empty($this->definition['field']) ? $this->definition['field'] : '';
63     $label = !empty($this->definition['label']) ? $this->definition['label'] : $this->definition['field'];
64     $options['admin_label']['default'] = $label;
65     $options['empty'] = ['default' => FALSE];
66
67     return $options;
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function adminSummary() {
74     return $this->adminLabel();
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
81     parent::buildOptionsForm($form, $form_state);
82
83     if ($form_state->get('type') != 'empty') {
84       $form['empty'] = [
85         '#type' => 'checkbox',
86         '#title' => $this->t('Display even if view has no result'),
87         '#default_value' => isset($this->options['empty']) ? $this->options['empty'] : 0,
88       ];
89     }
90   }
91
92   /**
93    * Performs any operations needed before full rendering.
94    *
95    * @param array $results
96    *   The results of the view.
97    */
98   public function preRender(array $results) {
99   }
100
101   /**
102    * Render the area.
103    *
104    * @param bool $empty
105    *   (optional) Indicator if view result is empty or not. Defaults to FALSE.
106    *
107    * @return array
108    *   In any case we need a valid Drupal render array to return.
109    */
110   public abstract function render($empty = FALSE);
111
112   /**
113    * Does that area have nothing to show.
114    *
115    * This method should be overridden by more complex handlers where the output
116    * is not static and maybe itself be empty if it's rendered.
117    *
118    * @return bool
119    *   Return TRUE if the area is empty, else FALSE.
120    */
121   public function isEmpty() {
122     return empty($this->options['empty']);
123   }
124
125 }
126
127 /**
128  * @}
129  */