From 7d47b6de93e3d153f01e8385cf33c484f5ad87f3 Mon Sep 17 00:00:00 2001 From: Keshav Kumar <keshav.kumar@axelerant.com> Date: Wed, 16 Aug 2023 12:37:10 +0530 Subject: [PATCH] Added configuration to toggle last clicked tab --- config/schema/quicktabs.schema.yml | 3 ++ js/quicktabs.js | 53 ++++++++++++----------- src/Entity/QuickTabsInstance.php | 15 +++++++ src/Entity/QuickTabsInstanceInterface.php | 8 ++++ src/Form/QuickTabsInstanceEditForm.php | 2 +- src/Plugin/TabRenderer/QuickTabs.php | 9 ++++ 6 files changed, 64 insertions(+), 26 deletions(-) diff --git a/config/schema/quicktabs.schema.yml b/config/schema/quicktabs.schema.yml index 014f9dd..03bdb9e 100644 --- a/config/schema/quicktabs.schema.yml +++ b/config/schema/quicktabs.schema.yml @@ -20,6 +20,9 @@ quicktabs.quicktabs_instance.*: hide_empty_tabs: type: boolean label: 'Hide Empty Tabs' + remember_last_clicked_tab: + type: boolean + label: 'Remember last clicked tab' default_tab: type: integer label: 'Default Tab' diff --git a/js/quicktabs.js b/js/quicktabs.js index d2843b6..137a6d1 100644 --- a/js/quicktabs.js +++ b/js/quicktabs.js @@ -124,32 +124,35 @@ Drupal.quicktabs.tab = function (el) { $(once('form-group', 'div.quicktabs-wrapper', context)).each(function () { var el = $(this); - // el.id format: "quicktabs-$name" - var qt_name = Drupal.quicktabs.getQTName(el); - var $ul = $(el).find('ul.quicktabs-tabs:first'); - - // Default cookie options. - var cookieOptions = {path: '/'}; - var cookieName = 'Drupal-quicktabs-active-tab-id-' + qt_name; - - $ul.find('li a').each(function (i, element) { - var $link = $(element); - $link.data('myTabIndex', i); - - // Click the tab ID if a cookie exists. - var $cookieValue = cookies.get(cookieName); - if ($cookieValue !== '' && $link.data('myTabIndex') == $cookieValue) { - $(element).click(); - } - - // Set the click handler for all tabs, this updates the cookie on - // every tab click. - $link.on('click', function () { - var $linkdata = $(this); - var tabIndex = $linkdata.data('myTabIndex'); - cookies.set(cookieName, tabIndex, cookieOptions); + // only remember last clicked tab if the option is enabled. + if (el.data('rememberLast')) { + // el.id format: "quicktabs-$name" + var qt_name = Drupal.quicktabs.getQTName(el); + var $ul = $(el).find('ul.quicktabs-tabs:first'); + + // Default cookie options. + var cookieOptions = {path: '/'}; + var cookieName = 'Drupal-quicktabs-active-tab-id-' + qt_name; + + $ul.find('li a').each(function (i, element) { + var $link = $(element); + $link.data('myTabIndex', i); + + // Click the tab ID if a cookie exists. + var $cookieValue = cookies.get(cookieName); + if ($cookieValue !== '' && $link.data('myTabIndex') == $cookieValue) { + $(element).click(); + } + + // Set the click handler for all tabs, this updates the cookie on + // every tab click. + $link.on('click', function () { + var $linkdata = $(this); + var tabIndex = $linkdata.data('myTabIndex'); + cookies.set(cookieName, tabIndex, cookieOptions); + }); }); - }); + } }); } }; diff --git a/src/Entity/QuickTabsInstance.php b/src/Entity/QuickTabsInstance.php index fe6b565..b12e63a 100644 --- a/src/Entity/QuickTabsInstance.php +++ b/src/Entity/QuickTabsInstance.php @@ -39,6 +39,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBase; * "renderer" = "renderer", * "options" = "options", * "hide_empty_tabs" = "hide_empty_tabs", + * "remember_last_clicked_tab" = "remember_last_clicked_tab", * "default_tab" = "default_tab", * "configuration_data" = "configuration_data" * }, @@ -77,6 +78,13 @@ class QuickTabsInstance extends ConfigEntityBase implements QuickTabsInstanceInt */ protected $options; + /** + * Whether or not to use a cookie to remember the last clicked tab. + * + * @var bool + */ + protected $remember_last_clicked_tab; + /** * Whether or not to hide empty tabs. * @@ -129,6 +137,13 @@ class QuickTabsInstance extends ConfigEntityBase implements QuickTabsInstanceInt return $this->hide_empty_tabs; } + /** + * {@inheritdoc} + */ + public function getRememberLastClickedTab() { + return $this->remember_last_clicked_tab; + } + /** * {@inheritdoc} */ diff --git a/src/Entity/QuickTabsInstanceInterface.php b/src/Entity/QuickTabsInstanceInterface.php index 04389f7..466f80c 100644 --- a/src/Entity/QuickTabsInstanceInterface.php +++ b/src/Entity/QuickTabsInstanceInterface.php @@ -41,6 +41,14 @@ interface QuickTabsInstanceInterface extends ConfigEntityInterface { */ public function getHideEmptyTabs(); + /** + * Returns boolean value of use last clicked tab setting. + * + * @return bool + * Use last clicked tab setting. + */ + public function getRememberLastClickedTab(); + /** * Returns the number of the default tab for this instance. * diff --git a/src/Form/QuickTabsInstanceEditForm.php b/src/Form/QuickTabsInstanceEditForm.php index 079cba0..8e9a5f2 100644 --- a/src/Form/QuickTabsInstanceEditForm.php +++ b/src/Form/QuickTabsInstanceEditForm.php @@ -294,7 +294,7 @@ class QuickTabsInstanceEditForm extends EntityForm { $ordered_configuration_data[] = $item; } $this->entity->setConfigurationData($ordered_configuration_data); - + $this->entity->set('remember_last_clicked_tab', $form_state->getValue('options')['quick_tabs']['remember_last_clicked_tab']); $status = $this->entity->save(); if ($status == SAVED_NEW) { $form_state->setRedirect('quicktabs.admin'); diff --git a/src/Plugin/TabRenderer/QuickTabs.php b/src/Plugin/TabRenderer/QuickTabs.php index dba20c2..69ee9bc 100644 --- a/src/Plugin/TabRenderer/QuickTabs.php +++ b/src/Plugin/TabRenderer/QuickTabs.php @@ -61,6 +61,14 @@ class QuickTabs extends TabRendererBase { '#description' => $this->t('Choose how the content of tabs should be loaded.<p>By choosing "Yes", only the first tab will be loaded when the page first viewed. Content for other tabs will be loaded only when the user clicks the other tab. This will provide faster initial page loading, but subsequent tab clicks will be slower. This can place less load on a server.</p><p>By choosing "No", all tabs will be loaded when the page is first viewed. This will provide slower initial page loading, and more server load, but subsequent tab clicks will be faster for the user. Use with care if you have heavy views.</p><p>Warning: if you enable Ajax, any block you add to this quicktabs block will be accessible to anonymous users, even if you place role restrictions on the quicktabs block. Do not enable Ajax if the quicktabs block includes any blocks with potentially sensitive information.</p>'), '#weight' => -6, ]; + $form['remember_last_clicked_tab'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Remember last clicked tab'), + '#default_value' => $instance->getRememberLastClickedTab(), + '#description' => $this->t('Use cookies to set the default tab to the last clicked tab by the current user.'), + '#weight' => -3, + ]; + return $form; } @@ -235,6 +243,7 @@ class QuickTabs extends TabRendererBase { 'container' => [ '#attributes' => [ 'class' => $classes, + 'data-remember-last' => intval($instance->getRememberLastClickedTab()), 'id' => 'quicktabs-' . $qt_id, ], ], -- GitLab