Skip to content

Commit

Permalink
Replace old implementation to check empty regions with contrib module…
Browse files Browse the repository at this point in the history
… "Twig Real Content"
  • Loading branch information
jclemmenb4y committed Jul 4, 2024
1 parent 4035ba0 commit c1697b8
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 60 deletions.
19 changes: 0 additions & 19 deletions includes/preprocess.html.inc
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,6 @@
* 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) {
Expand Down
7 changes: 0 additions & 7 deletions includes/preprocess.page.inc
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
* 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();
Expand Down
20 changes: 0 additions & 20 deletions includes/preprocess.utils.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,6 @@
* 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)*?-->\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.
*/
Expand Down
4 changes: 4 additions & 0 deletions kiso.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ libraries-override:
css:
theme:
extlink.css: 'css/components/extlink/extlink-window.css'

# Defines module dependencies
dependencies:
- drupal:twig_real_content
11 changes: 11 additions & 0 deletions templates/layout/html.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
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') %}
Expand Down
24 changes: 10 additions & 14 deletions templates/layout/page.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
* 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.
Expand Down Expand Up @@ -40,7 +36,7 @@
{% set container = container_fluid ? 'container-fluid' : 'container' %}

{# Toolbar Area #}
{% if has_tools %}
{% if page.tools|render is real_content %}
{% block tools %}
<div class="page__wrapper page__wrapper--tools">
<div class="page__section page__section--tools {{ container }}">
Expand All @@ -51,7 +47,7 @@
{% endif %}

{# Banner Landmark #}
{% if has_header or has_header_collapsible %}
{% if page.header|render is real_content or page.header_collapsible|render is real_content %}
{% block header %}
<div class="page__wrapper page__wrapper--header">
<header class="page__section page__section--header {{ container }}">
Expand All @@ -65,7 +61,7 @@
{% endif %}

{# Featured content Area #}
{% if has_highlighted %}
{% if page.highlighted|render is real_content %}
{% block highlighted %}
<div class="page__wrapper page__wrapper--highlighted">
<div class="page__section page__section--highlighted {{ container }}">
Expand All @@ -89,7 +85,7 @@
{{ page.breadcrumb }}
{% endblock %}

{% if has_navigation or has_complementary %}
{% if page.navigation|render is real_content or page.complementary|render is real_content %}
<div class="{{ container }}">
<div class="row">
{% endif %}
Expand All @@ -99,7 +95,7 @@
set content_classes = [
'page__section',
'page__section--content',
has_navigation or has_complementary ? '' : container,
page.navigation|render is real_content or page.complementary|render is real_content ? '' : container,
]
%}
{% block content %}
Expand All @@ -114,7 +110,7 @@
{% endblock %}

{# Navigation sidebar (Left) #}
{% if has_navigation %}
{% if page.navigation|render is real_content %}
{% block navigation %}
<div class="page__section page__section--navigation">
{{ page.navigation }}
Expand All @@ -123,15 +119,15 @@
{% endif %}

{# Related content sidebar (Right) #}
{% if has_complementary %}
{% if page.complementary|render is real_content %}
{% block complementary %}
<div class="page__section page__section--complementary">
{{ page.complementary }}
</div>
{% endblock %}
{% endif %}

{% if has_navigation or has_complementary %}
{% if page.navigation|render is real_content or page.complementary|render is real_content %}
</div>
</div>
{% endif %}
Expand All @@ -140,7 +136,7 @@
{% endblock %}

{# Footnotes Area #}
{% if has_postscript %}
{% if page.postscript|render is real_content %}
{% block postscript %}
<div class="page__wrapper page__wrapper--postscript">
<div class="page__section page__section--postscript {{ container }}">
Expand All @@ -151,7 +147,7 @@
{% endif %}

{# Contentinfo Landmark #}
{% if has_footer %}
{% if page.footer|render is real_content %}
{% block footer %}
<div class="page__wrapper page__wrapper--footer">
<footer class="page__section page__section--footer {{ container }}">
Expand Down

0 comments on commit c1697b8

Please sign in to comment.