Skip to content

Instantly share code, notes, and snippets.

@paulsheldrake
Created September 4, 2018 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulsheldrake/553461a5eb36c02ca61a9fd5d60cd1dc to your computer and use it in GitHub Desktop.
Save paulsheldrake/553461a5eb36c02ca61a9fd5d60cd1dc to your computer and use it in GitHub Desktop.
From 879c47e6c03216b03b86bbe8c30ed190b44ec943 Mon Sep 17 00:00:00 2001
From: Paul Sheldrake <paul.sheldrake@gmail.com>
Date: Tue, 4 Sep 2018 11:08:09 -0700
Subject: [PATCH] re-roll patch for for settings and tab title
---
.gitignore | 1 +
src/Plugin/views/style/ViewsBootstrapTab.php | 30 ++++++++++++++++---
templates/views-bootstrap-tab.html.twig | 12 +++++++-
views_bootstrap.theme.inc | 44 ++++++++++++++++++++++++++--
4 files changed, 79 insertions(+), 8 deletions(-)
create mode 100644 .gitignore
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..485dee6
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+.idea
diff --git a/src/Plugin/views/style/ViewsBootstrapTab.php b/src/Plugin/views/style/ViewsBootstrapTab.php
index d2cbed9..62d1fc0 100644
--- a/src/Plugin/views/style/ViewsBootstrapTab.php
+++ b/src/Plugin/views/style/ViewsBootstrapTab.php
@@ -29,17 +29,32 @@ class ViewsBootstrapTab extends StylePluginBase {
*/
protected $usesFields = TRUE;
+ /**
+ * Does the style plugin allows to use style plugins.
+ *
+ * @var bool
+ */
+ protected $usesRowPlugin = TRUE;
+
/**
* {@inheritdoc}
*/
protected $usesOptions = TRUE;
+ /**
+ * Does the style plugin support grouping of rows.
+ *
+ * @var bool
+ */
+ protected $usesGrouping = FALSE;
+
/**
* Definition.
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['tab_field'] = ['default' => NULL];
+ $options['tab_group'] = ['default' => FALSE];
$options['tab_type'] = ['default' => 'tabs'];
$options['justified'] = ['default' => FALSE];
return $options;
@@ -50,8 +65,10 @@ class ViewsBootstrapTab extends StylePluginBase {
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
- if (isset($form['grouping'])) {
- unset($form['grouping']);
+
+ if (isset($form['grouping'])) {
+ unset($form['grouping']);
+ }
$form['tab_field'] = [
'#type' => 'select',
@@ -62,6 +79,13 @@ class ViewsBootstrapTab extends StylePluginBase {
'#description' => t('Select the field that will be used as the tab.'),
];
+ $form['tab_group'] = [
+ '#type' => 'checkbox',
+ '#title' => $this->t('Group by tab field'),
+ '#default_value' => $this->options['tab_group'],
+ '#description' => t('If you want to group tabs by same values.'),
+ ];
+
$form['tab_type'] = [
'#type' => 'select',
'#title' => $this->t('Tab Type'),
@@ -82,6 +106,4 @@ class ViewsBootstrapTab extends StylePluginBase {
];
}
- }
-
}
\ No newline at end of file
diff --git a/templates/views-bootstrap-tab.html.twig b/templates/views-bootstrap-tab.html.twig
index 5955b42..88e2002 100644
--- a/templates/views-bootstrap-tab.html.twig
+++ b/templates/views-bootstrap-tab.html.twig
@@ -14,7 +14,17 @@
{% for key, row in rows %}
{% set row_classes = ['tab-pane', loop.first ? 'active'] %}
<div class="{{ row_classes|join(' ') }}" id="tab-{{ id }}-{{ key }}" {{ row.attributes }}>
- {{ row.content }}
+
+ {% if row.content is not empty %}
+ {{ row.content }}
+ {% endif %}
+
+ {% if row.content_tabs is not empty %}
+ {% for tc_key, tab_content in row.content_tabs %}
+ {{ tab_content }}
+ {% endfor %}
+ {% endif %}
+
</div>
{% endfor %}
</div>
diff --git a/views_bootstrap.theme.inc b/views_bootstrap.theme.inc
index 09f1750..b417762 100644
--- a/views_bootstrap.theme.inc
+++ b/views_bootstrap.theme.inc
@@ -181,18 +181,56 @@ function template_preprocess_views_bootstrap_tab(array &$vars) {
$tab_field = $view->style_plugin->options['tab_field'];
$vars['tab_type'] = $view->style_plugin->options['tab_type'];
$vars['justified'] = $view->style_plugin->options['justified'];
+ $vars['tab_group'] = $view->style_plugin->options['tab_group'];
// Get tabs.
if ($tab_field) {
+ // Keeps which id is used for group by field value
+ $tab_ids = array();
+
if (isset($view->field[$tab_field])) {
foreach (array_keys($vars['rows']) as $key) {
- $vars['tabs'][$key] = $view->style_plugin->getFieldValue($key, $tab_field);
+
+ $tab_field_value = $view->style_plugin->getFieldValue($key, $tab_field);
+ // The rendered field but strip tags so no views markup ruins
+ // the tab specific markup.
+ $tab_field_rendered = strip_tags($view->style_plugin->getField($key, $tab_field));
+
+ // If tabs are grouped get previous key for the value
+ if ($vars['tab_group'] && isset($tab_ids[$tab_field_value])) {
+ $key = $tab_ids[$tab_field_value];
+ }
+ else {
+ $tab_ids[$tab_field_value] = $key;
+ }
+
+ $vars['tabs'][$key] = $tab_field_rendered;
}
}
+
foreach ($vars['rows'] as $id => $row) {
$vars['rows'][$id] = array();
- $vars['rows'][$id]['content'] = $row;
- $vars['rows'][$id]['attributes'] = new Attribute();
+
+ $tab_field_value = $view->style_plugin->getFieldValue($id, $tab_field);
+
+ // If groups the tabs create new array with all the values for the tab content
+ if ($vars['tab_group'] && isset($tab_ids[$tab_field_value])) {
+ if (!isset($vars['rows'][$id]['content_tabs'])) {
+ $vars['rows'][$id]['content_tabs'] = array();
+ }
+
+ // group by the key of first met value of the field grouped by
+ $id = $tab_ids[$tab_field_value];
+ $vars['rows'][$id]['content_tabs'][] = $row;
+ }
+ else {
+ $vars['rows'][$id]['content'] = $row;
+ }
+
+ if (!isset($vars['rows'][$id]['attributes'])) {
+ $vars['rows'][$id]['attributes'] = new Attribute();
+ }
+
if ($row_class = $view->style_plugin->getRowClass($id)) {
$vars['rows'][$id]['attributes']->addClass($row_class);
}
--
2.14.3 (Apple Git-98)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment