From 395a42ab7f3f7f6b2a77ee4e37cf1d1194c009ae Mon Sep 17 00:00:00 2001 From: William Hearn Date: Sun, 14 May 2017 18:34:01 -0400 Subject: [PATCH] Issue #2878352 by sylus: Add Bootstrap .panels views style --- src/Plugin/views/style/ViewsBootstrapPanel.php | 67 ++++++++++++++++++++++++++ templates/views-bootstrap-panel.html.twig | 14 ++++++ views_bootstrap.theme.inc | 37 +++++++++++++- 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/Plugin/views/style/ViewsBootstrapPanel.php create mode 100644 templates/views-bootstrap-panel.html.twig diff --git a/src/Plugin/views/style/ViewsBootstrapPanel.php b/src/Plugin/views/style/ViewsBootstrapPanel.php new file mode 100644 index 0000000..778aba6 --- /dev/null +++ b/src/Plugin/views/style/ViewsBootstrapPanel.php @@ -0,0 +1,67 @@ + NULL]; + + return $options; + } + + /** + * Render the given style. + */ + public function buildOptionsForm(&$form, FormStateInterface $form_state) { + parent::buildOptionsForm($form, $form_state); + if (isset($form['grouping'])) { + unset($form['grouping']); + + $form['panel_title_field'] = [ + '#type' => 'select', + '#title' => $this->t('Panel title field'), + '#options' => $this->displayHandler->getFieldLabels(TRUE), + '#required' => TRUE, + '#default_value' => $this->options['panel_title_field'], + '#description' => $this->t('Select the field that will be used as the panel heading titles.'), + ]; + } + } + +} diff --git a/templates/views-bootstrap-panel.html.twig b/templates/views-bootstrap-panel.html.twig new file mode 100644 index 0000000..34c4bc6 --- /dev/null +++ b/templates/views-bootstrap-panel.html.twig @@ -0,0 +1,14 @@ +
+ {% for key, row in rows -%} +
+
+

+ {{row.title}} +

+
+
+ {{row.content}} +
+
+ {%- endfor %} +
diff --git a/views_bootstrap.theme.inc b/views_bootstrap.theme.inc index 09f1750..ec02c9c 100644 --- a/views_bootstrap.theme.inc +++ b/views_bootstrap.theme.inc @@ -166,6 +166,42 @@ function template_preprocess_views_bootstrap_media_object(array &$vars) { } /** + * Prepares variables for views panel templates. + * + * Default template: views-bootstrap-panel.html.twig. + * + * @param array $vars + * An associative array containing: + * - view: A ViewExecutable object. + * - rows: The raw row data. + */ +function template_preprocess_views_bootstrap_panel(array &$vars) { + $view = $vars['view']; + $vars['id'] = ViewsBootstrap::getUniqueId($view); + $panel_title_field = $view->style_plugin->options['panel_title_field']; + $vars['attributes']['class'][] = 'panel-group'; + if ($panel_title_field) { + foreach ($vars['rows'] as $id => $row) { + $vars['rows'][$id] = []; + $vars['rows'][$id]['content'] = $row; + $vars['rows'][$id]['title'] = $view->style_plugin->getField($id, $panel_title_field); + } + } + else { + // @TODO: This would be better as valdiation errors on the style plugin options form. + drupal_set_message(t('@style style will not display without the "@field" setting.', + [ + '@style' => $view->style_plugin->definition['title'], + '@field' => 'Panel title', + ] + ), 'warning'); + } + // @TODO: Make sure that $vars['rows'] is rendered array. + // @SEE: Have a look template_preprocess_views_view_unformatted() + // and views-view-unformatted.html.twig +} + +/** * Prepares variables for views tab templates. * * Default template: views-bootstrap-tab.html.twig. @@ -226,4 +262,3 @@ function template_preprocess_views_bootstrap_table(array &$vars) { $vars['attributes']['class'][] = 'table-' . $style; } } - -- 2.9.3