Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#64 3.0.x #121

Open
wants to merge 4 commits into
base: 3.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions includes/preprocess.page.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
36 changes: 36 additions & 0 deletions includes/preprocess.utils.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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)*?-->\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
2 changes: 1 addition & 1 deletion kiso.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ libraries-override:

# Defines module dependencies
dependencies:
- drupal:twig_real_content
- drupal:twig_real_content
9 changes: 4 additions & 5 deletions templates/layout/html.html.twig
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How this html.html.twig template can access the has_[region] variables if those are created from the kiso_preprocess_page() function only?

Original file line number Diff line number Diff line change
Expand Up @@ -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') %}
Expand Down
24 changes: 14 additions & 10 deletions templates/layout/page.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 %}
<div class="page__wrapper page__wrapper--tools">
<div class="page__section page__section--tools {{ container }}">
Expand All @@ -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 %}
<div class="page__wrapper page__wrapper--header">
<header class="page__section page__section--header {{ container }}">
Expand All @@ -61,7 +65,7 @@
{% endif %}

{# Featured content Area #}
{% if page.highlighted|render is real_content %}
{% if has_highlighted %}
{% block highlighted %}
<div class="page__wrapper page__wrapper--highlighted">
<div class="page__section page__section--highlighted {{ container }}">
Expand All @@ -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 %}
<div class="{{ container }}">
<div class="row">
{% endif %}
Expand All @@ -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 %}
Expand All @@ -110,7 +114,7 @@
{% endblock %}

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

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

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

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

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