diff --git a/includes/preprocess.html.inc b/includes/preprocess.html.inc index 61328a9..20514d7 100644 --- a/includes/preprocess.html.inc +++ b/includes/preprocess.html.inc @@ -9,6 +9,14 @@ * Implements hook_preprocess_HOOK() for html.html.twig. */ function kiso_preprocess_html(&$variables) { + // Add boolean variables detecting if regions are empty. + $theme = \Drupal::theme()->getActiveTheme()->getName(); + $regions = system_region_list($theme); + foreach ($regions as $key => $value) { + $region = $variables['page'][$key]; + $variables['has_' . $key] = _kiso_has_region(\Drupal::service('renderer')->render($region)); + } + // 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..e430161 100644 --- a/includes/preprocess.page.inc +++ b/includes/preprocess.page.inc @@ -9,6 +9,15 @@ * 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) { + $region = $variables['page'][$key]; + $variables['has_' . $key] = _kiso_has_region(\Drupal::service('renderer')->render($region)); + } + // 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..7d0fd2e 100644 --- a/includes/preprocess.utils.inc +++ b/includes/preprocess.utils.inc @@ -5,6 +5,42 @@ * 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 + */ + +use Twig\TwigFilter; + +function _kiso_has_region(?string $markup, string $allowed_tags = '') { + $moduleHandler = \Drupal::service('module_handler'); + if ($moduleHandler->moduleExists('twig_real_content')) { + $filters = \Drupal::service('twig_real_content.twig_extension')->getFilters(); + $key = array_search('real_content', array_map(function(TwigFilter $filter) { return $filter->getName(); }, $filters), true); + $callable = $filters[$key]->getCallable(); + + $real_content = $callable($markup); + + return !empty($real_content); + } else { + $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..b567363 100644 --- a/kiso.info.yml +++ b/kiso.info.yml @@ -61,4 +61,4 @@ libraries-override: # Defines module dependencies dependencies: - - drupal:twig_real_content \ No newline at end of file + - drupal:twig_real_content diff --git a/templates/layout/html.html.twig b/templates/layout/html.html.twig index c9be4e7..0677ed7 100644 --- a/templates/layout/html.html.twig +++ b/templates/layout/html.html.twig @@ -27,17 +27,16 @@ 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 %} +{% if has_navigation and has_complementary %} {% set body_classes = body_classes|merge(['sidebar', 'two-sidebars']) %} -{% elseif page_navigaton is real_content %} +{% elseif has_navigaton %} {% set body_classes = body_classes|merge(['sidebar', 'one-sidebar', 'is-visible--navigation']) %} -{% elseif page_complementary is real_content %} +{% elseif has_complementary %} {% 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 %}