3 namespace Drupal\views_bootstrap\Plugin\views\style;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\style\StylePluginBase;
7 use Drupal\Component\Utility\Html;
10 * Style plugin to render each item in an ordered or unordered list.
12 * @ingroup views_style_plugins
15 * id = "views_bootstrap_grid",
16 * title = @Translation("Bootstrap Grid"),
17 * help = @Translation("Displays rows in a Bootstrap Grid layout"),
18 * theme = "views_bootstrap_grid",
19 * theme_file = "../views_bootstrap.theme.inc",
20 * display_types = {"normal"}
23 class ViewsBootstrapGrid extends StylePluginBase {
25 * Overrides \Drupal\views\Plugin\views\style\StylePluginBase::usesRowPlugin.
29 protected $usesRowPlugin = TRUE;
32 * Overrides \Drupal\views\Plugin\views\style\StylePluginBase::usesRowClass.
36 protected $usesRowClass = TRUE;
39 * Return the token-replaced row or column classes for the specified result.
41 * @param int $result_index
42 * The delta of the result item to get custom classes for.
44 * The type of custom grid class to return, either "row" or "col".
47 * A space-delimited string of classes.
49 public function getCustomClass($result_index, $type) {
50 if (isset($this->options[$type . '_class_custom'])) {
51 $class = $this->options[$type . '_class_custom'];
52 if ($this->usesFields() && $this->view->field) {
53 $class = strip_tags($this->tokenizeValue($class, $result_index));
56 $classes = explode(' ', $class);
57 foreach ($classes as &$class) {
58 $class = Html::cleanCssIdentifier($class);
60 return implode(' ', $classes);
65 * Normalize a list of columns.
67 * Normalize columns based upon the fields that are available. This compares
68 * the fields stored in the style handler
69 * to the list of fields actually in the view, removing fields that
70 * have been removed and adding new fields in their own column.
71 * - Each field must be in a column.
72 * - Each column must be based upon a field, and that field is somewhere in
74 * - Any fields not currently represented must be added.
75 * - Columns must be re-ordered to match the fields.
77 * @param array $columns
78 * An array of all fields; the key is the id of the field and the
79 * value is the id of the column the field should be in.
80 * @param array|null $fields
81 * The fields to use for the columns. If not provided, they will
82 * be requested from the current display. The running render should
83 * send the fields through, as they may be different than what the
84 * display has listed due to access control or other changes.
87 * An array of all the sanitized columns.
89 public function sanitizeColumns(array $columns, $fields = NULL) {
91 if ($fields === NULL) {
92 $fields = $this->displayHandler->getOption('fields');
94 // Pre-configure the sanitized array so that the order is retained.
95 foreach ($fields as $field => $info) {
96 // Set to itself so that if it isn't touched, it gets column
97 // status automatically.
98 $sanitized[$field] = $field;
101 if (!empty($columns)) {
105 foreach ($columns as $field => $column) {
106 // first, make sure the field still exists.
107 if (!isset($sanitized[$field])) {
111 // If the field is the column, mark it so, or the column
112 // it's set to is a column, that's ok.
113 if ($field == $column || $columns[$column] == $column && !empty($sanitized[$column])) {
114 $sanitized[$field] = $column;
116 // Since we set the field to itself initially, ignoring
117 // the condition is ok; the field will get its column
127 protected function defineOptions() {
128 $options = parent::defineOptions();
130 $options['alignment'] = ['default' => 'horizontal'];
131 $options['columns'] = ['default' => '4'];
132 $options['col_xs'] = ['default' => 'col-xs-12'];
133 $options['col_sm'] = ['default' => 'col-sm-12'];
134 $options['col_md'] = ['default' => 'col-md-12'];
135 $options['col_lg'] = ['default' => 'col-lg-12'];
136 $options['automatic_width'] = ['default' => TRUE];
137 $options['col_class_custom'] = ['default' => ''];
138 $options['col_class_default'] = ['default' => TRUE];
139 $options['row_class_custom'] = ['default' => ''];
140 $options['row_class_default'] = ['default' => TRUE];
141 $options['default'] = ['default' => ''];
142 $options['info'] = ['default' => []];
143 $options['override'] = ['default' => TRUE];
144 $options['sticky'] = ['default' => FALSE];
145 $options['order'] = ['default' => 'asc'];
146 $options['caption'] = ['default' => ''];
147 $options['summary'] = ['default' => ''];
148 $options['description'] = ['default' => ''];
155 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
156 parent::buildOptionsForm($form, $form_state);
158 $form['alignment'] = [
160 '#title' => $this->t('Alignment'),
162 'horizontal' => $this->t('Horizontal'),
163 'vertical' => $this->t('Vertical'),
165 '#description' => $this->t('Horizontal alignment will place items starting in the upper left and moving right.
166 Vertical alignment will place items starting in the upper left and moving down.'),
167 '#default_value' => $this->options['alignment'],
172 '#title' => $this->t('Number of columns per row'),
174 '#default_value' => isset($this->options['columns']) ? $this->options['columns'] : NULL,
182 999 => $this->t('All'),
186 foreach (['xs', 'sm', 'md', 'lg'] as $size) {
187 $form["col_${size}"] = [
189 '#title' => $this->t("Number of columns (col-${size})"),
191 '#default_value' => isset($this->options["col_${size}"]) ? $this->options["col_${size}"] : NULL,
193 "col-${size}-12" => 1,
194 "col-${size}-6" => 2,
195 "col-${size}-4" => 3,
196 "col-${size}-3" => 4,
197 "col-${size}-2" => 6,
198 "col-${size}-1" => 12,