938c1b5236459fcaf925dec76f3adc6945280023
[yaffs-website] / web / core / modules / views / src / Plugin / views / style / Grid.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\style;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Style plugin to render each item in a grid cell.
10  *
11  * @ingroup views_style_plugins
12  *
13  * @ViewsStyle(
14  *   id = "grid",
15  *   title = @Translation("Grid"),
16  *   help = @Translation("Displays rows in a grid."),
17  *   theme = "views_view_grid",
18  *   display_types = {"normal"}
19  * )
20  */
21 class Grid extends StylePluginBase {
22
23   /**
24    * Does the style plugin allows to use style plugins.
25    *
26    * @var bool
27    */
28   protected $usesRowPlugin = TRUE;
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function defineOptions() {
34     $options = parent::defineOptions();
35     $options['columns'] = ['default' => '4'];
36     $options['automatic_width'] = ['default' => TRUE];
37     $options['alignment'] = ['default' => 'horizontal'];
38     $options['col_class_custom'] = ['default' => ''];
39     $options['col_class_default'] = ['default' => TRUE];
40     $options['row_class_custom'] = ['default' => ''];
41     $options['row_class_default'] = ['default' => TRUE];
42     return $options;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
49     parent::buildOptionsForm($form, $form_state);
50     $form['columns'] = [
51       '#type' => 'number',
52       '#title' => $this->t('Number of columns'),
53       '#default_value' => $this->options['columns'],
54       '#required' => TRUE,
55       '#min' => 1,
56     ];
57     $form['automatic_width'] = [
58       '#type' => 'checkbox',
59       '#title' => $this->t('Automatic width'),
60       '#description' => $this->t('The width of each column will be calculated automatically based on the number of columns provided. If additional classes are entered or a theme injects classes based on a grid system, disabling this option may prove beneficial.'),
61       '#default_value' => $this->options['automatic_width'],
62     ];
63     $form['alignment'] = [
64       '#type' => 'radios',
65       '#title' => $this->t('Alignment'),
66       '#options' => ['horizontal' => $this->t('Horizontal'), 'vertical' => $this->t('Vertical')],
67       '#default_value' => $this->options['alignment'],
68       '#description' => $this->t('Horizontal alignment will place items starting in the upper left and moving right. Vertical alignment will place items starting in the upper left and moving down.'),
69     ];
70     $form['col_class_default'] = [
71       '#title' => $this->t('Default column classes'),
72       '#description' => $this->t('Add the default views column classes like views-col, col-1 and clearfix to the output. You can use this to quickly reduce the amount of markup the view provides by default, at the cost of making it more difficult to apply CSS.'),
73       '#type' => 'checkbox',
74       '#default_value' => $this->options['col_class_default'],
75     ];
76     $form['col_class_custom'] = [
77       '#title' => $this->t('Custom column class'),
78       '#description' => $this->t('Additional classes to provide on each column. Separated by a space.'),
79       '#type' => 'textfield',
80       '#default_value' => $this->options['col_class_custom'],
81     ];
82     if ($this->usesFields()) {
83       $form['col_class_custom']['#description'] .= ' ' . $this->t('You may use field tokens from as per the "Replacement patterns" used in "Rewrite the output of this field" for all fields.');
84     }
85     $form['row_class_default'] = [
86       '#title' => $this->t('Default row classes'),
87       '#description' => $this->t('Adds the default views row classes like views-row, row-1 and clearfix to the output. You can use this to quickly reduce the amount of markup the view provides by default, at the cost of making it more difficult to apply CSS.'),
88       '#type' => 'checkbox',
89       '#default_value' => $this->options['row_class_default'],
90     ];
91     $form['row_class_custom'] = [
92       '#title' => $this->t('Custom row class'),
93       '#description' => $this->t('Additional classes to provide on each row. Separated by a space.'),
94       '#type' => 'textfield',
95       '#default_value' => $this->options['row_class_custom'],
96     ];
97     if ($this->usesFields()) {
98       $form['row_class_custom']['#description'] .= ' ' . $this->t('You may use field tokens from as per the "Replacement patterns" used in "Rewrite the output of this field" for all fields.');
99     }
100   }
101
102   /**
103    * Return the token-replaced row or column classes for the specified result.
104    *
105    * @param int $result_index
106    *   The delta of the result item to get custom classes for.
107    * @param string $type
108    *   The type of custom grid class to return, either "row" or "col".
109    *
110    * @return string
111    *   A space-delimited string of classes.
112    */
113   public function getCustomClass($result_index, $type) {
114     $class = $this->options[$type . '_class_custom'];
115     if ($this->usesFields() && $this->view->field) {
116       $class = strip_tags($this->tokenizeValue($class, $result_index));
117     }
118
119     $classes = explode(' ', $class);
120     foreach ($classes as &$class) {
121       $class = Html::cleanCssIdentifier($class);
122     }
123     return implode(' ', $classes);
124   }
125
126 }