From ec87c1266fa3af541559b9fd2c9a8194de370e7c Mon Sep 17 00:00:00 2001 From: Ruben Marques Date: Thu, 4 Jul 2024 18:31:58 +0200 Subject: [PATCH 1/4] Revert "Replace old implementation to check empty regions with contrib module "Twig Real Content"" This reverts commit c1697b8f37e17f7b1a6465ad11ea1dbb91ba2803. --- includes/preprocess.html.inc | 19 +++++++++++++++++++ includes/preprocess.page.inc | 7 +++++++ includes/preprocess.utils.inc | 20 ++++++++++++++++++++ kiso.info.yml | 4 ---- templates/layout/html.html.twig | 11 ----------- templates/layout/page.html.twig | 24 ++++++++++++++---------- 6 files changed, 60 insertions(+), 25 deletions(-) diff --git a/includes/preprocess.html.inc b/includes/preprocess.html.inc index 61328a9..dea2629 100644 --- a/includes/preprocess.html.inc +++ b/includes/preprocess.html.inc @@ -9,6 +9,25 @@ * Implements hook_preprocess_HOOK() for html.html.twig. */ function kiso_preprocess_html(&$variables) { + // Added body classes when sidebar(s) has (have) content. + if (_kiso_has_region(\Drupal::service('renderer')->render($variables['page']['navigation'])) && _kiso_has_region(\Drupal::service('renderer')->render($variables['page']['complementary']))) { + $variables['attributes']['class'][] = 'sidebar'; + $variables['attributes']['class'][] = 'two-sidebars'; + } + elseif (_kiso_has_region(\Drupal::service('renderer')->render($variables['page']['navigation']))) { + $variables['attributes']['class'][] = 'sidebar'; + $variables['attributes']['class'][] = 'one-sidebar'; + $variables['attributes']['class'][] = 'is-visible--navigation'; + } + elseif (_kiso_has_region(\Drupal::service('renderer')->render($variables['page']['complementary']))) { + $variables['attributes']['class'][] = 'sidebar'; + $variables['attributes']['class'][] = 'one-sidebar'; + $variables['attributes']['class'][] = 'is-visible--complementary'; + } + else { + $variables['attributes']['class'][] = 'no-sidebars'; + } + // Add body classes related to node content. $node = \Drupal::routeMatch()->getParameter('node'); if ($node) { diff --git a/includes/preprocess.page.inc b/includes/preprocess.page.inc index 50f37a5..465d732 100644 --- a/includes/preprocess.page.inc +++ b/includes/preprocess.page.inc @@ -9,6 +9,13 @@ * Implements hook_preprocess_HOOK() for page.html.twig. */ function kiso_preprocess_page(&$variables) { + // Add boolean variables detecting if regions are empty. + $theme = \Drupal::theme()->getActiveTheme()->getName(); + $regions = system_region_list($theme); + foreach ($regions as $key => $value) { + $variables['has_' . $key] = _kiso_has_region(\Drupal::service('renderer')->render($variables['page'][$key])); + } + // Create variable for status code. if ($exception = \Drupal::request()->get('exception')) { $status_code = $exception->getStatusCode(); diff --git a/includes/preprocess.utils.inc b/includes/preprocess.utils.inc index b3e6f4d..3ea2531 100644 --- a/includes/preprocess.utils.inc +++ b/includes/preprocess.utils.inc @@ -5,6 +5,26 @@ * Utilities used by the theme preprocess functions. */ +/** + * Properly detect if regions are empty. + * + * @param ?string $markup + * The rendered region markup to be tested if empty. + * @param string $allowed_tags + * Allowed tags to be excluded from the strip HTML tags process. + * + * @return + * TRUE if the region exists and is not empty, FALSE otherwise. + * + * @see https://www.drupal.org/node/953034 + * @see https://drupal.stackexchange.com/questions/175389/how-do-i-properly-detect-if-region-is-empty + */ +function _kiso_has_region(?string $markup, string $allowed_tags = '') { + $non_conditional_html_comments_pattern = '/\s*|\r|\n/'; + $cleaned_region_output = preg_replace($non_conditional_html_comments_pattern, '', $markup); + return !empty(_kiso_strip_tags($cleaned_region_output, $allowed_tags)); +} + /** * Strips html tags, except allowed, returning a trimmed clean markup. */ diff --git a/kiso.info.yml b/kiso.info.yml index fbc8628..0c44882 100644 --- a/kiso.info.yml +++ b/kiso.info.yml @@ -58,7 +58,3 @@ libraries-override: css: theme: extlink.css: 'css/components/extlink/extlink-window.css' - -# Defines module dependencies -dependencies: - - drupal:twig_real_content \ No newline at end of file diff --git a/templates/layout/html.html.twig b/templates/layout/html.html.twig index c9be4e7..5796f45 100644 --- a/templates/layout/html.html.twig +++ b/templates/layout/html.html.twig @@ -27,17 +27,6 @@ db_offline ? 'db-offline', ] %} -{% set page_navigaton = page.navigation|render %} -{% set page_complementary = page.complementary|render %} -{% if page_navigation is real_content and page_complementary is real_content %} - {% set body_classes = body_classes|merge(['sidebar', 'two-sidebars']) %} -{% elseif page_navigaton is real_content %} - {% set body_classes = body_classes|merge(['sidebar', 'one-sidebar', 'is-visible--navigation']) %} -{% elseif page_complementary is real_content %} - {% set body_classes = body_classes|merge(['sidebar', 'one-sidebar', 'is-visible--complementary']) %} -{% else %} - {% set body_classes = body_classes|merge(['no-sidebars']) %} -{% endif %} {# Enable the "Back to top" button. #} {% if backtotop_enable %} {% set attributes = attributes.setAttribute('id', 'top') %} diff --git a/templates/layout/page.html.twig b/templates/layout/page.html.twig index 364328d..1c01cd4 100644 --- a/templates/layout/page.html.twig +++ b/templates/layout/page.html.twig @@ -7,6 +7,10 @@ * can be found in the 'html.html.twig' template in this directory. * * You will find same variables as in the core 'page.html.twig' template. + * + * Custom variables: + * - has_tools, has_header, has_header_collapsible, has_highlighted, has_navigation, + * has_complementary, has_postscript, has_footer: Properly detect if regions are empty. * * Regions: * - page.tools: Items for the Toolbar region. @@ -36,7 +40,7 @@ {% set container = container_fluid ? 'container-fluid' : 'container' %} {# Toolbar Area #} -{% if page.tools|render is real_content %} +{% if has_tools %} {% block tools %}
@@ -47,7 +51,7 @@ {% endif %} {# Banner Landmark #} -{% if page.header|render is real_content or page.header_collapsible|render is real_content %} +{% if has_header or has_header_collapsible %} {% block header %}
@@ -61,7 +65,7 @@ {% endif %} {# Featured content Area #} -{% if page.highlighted|render is real_content %} +{% if has_highlighted %} {% block highlighted %}
@@ -85,7 +89,7 @@ {{ page.breadcrumb }} {% endblock %} - {% if page.navigation|render is real_content or page.complementary|render is real_content %} + {% if has_navigation or has_complementary %}
{% endif %} @@ -95,7 +99,7 @@ set content_classes = [ 'page__section', 'page__section--content', - page.navigation|render is real_content or page.complementary|render is real_content ? '' : container, + has_navigation or has_complementary ? '' : container, ] %} {% block content %} @@ -110,7 +114,7 @@ {% endblock %} {# Navigation sidebar (Left) #} - {% if page.navigation|render is real_content %} + {% if has_navigation %} {% block navigation %}
{{ page.navigation }} @@ -119,7 +123,7 @@ {% endif %} {# Related content sidebar (Right) #} - {% if page.complementary|render is real_content %} + {% if has_complementary %} {% block complementary %}
{{ page.complementary }} @@ -127,7 +131,7 @@ {% endblock %} {% endif %} - {% if page.navigation|render is real_content or page.complementary|render is real_content %} + {% if has_navigation or has_complementary %}
{% endif %} @@ -136,7 +140,7 @@ {% endblock %} {# Footnotes Area #} -{% if page.postscript|render is real_content %} +{% if has_postscript %} {% block postscript %}
@@ -147,7 +151,7 @@ {% endif %} {# Contentinfo Landmark #} -{% if page.footer|render is real_content %} +{% if has_footer %} {% block footer %}