diff --git a/composer.json b/composer.json index 7c282a4..f6e7a81 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ } }, "require": { - "drupal/core": "^9 || ^10" + "drupal/core": "^9 || ^10", + "drupal/twig_real_content": "^1.0" }, "require-dev": {}, "minimum-stability": "dev", diff --git a/includes/preprocess.html.inc b/includes/preprocess.html.inc index 61328a9..d8cd242 100644 --- a/includes/preprocess.html.inc +++ b/includes/preprocess.html.inc @@ -9,6 +9,31 @@ * Implements hook_preprocess_HOOK() for html.html.twig. */ function kiso_preprocess_html(&$variables) { + + $variables['page_navigation'] = _kiso_render_region($variables['page']['navigation']); + $variables['page_complementary'] = _kiso_render_region($variables['page']['complementary']); + $has_navigation = _kiso_has_region($variables['page_navigation']); + $has_complementary = _kiso_has_region($variables['page_complementary']); + + // Added body classes when sidebar(s) has (have) content. + if ($has_navigation && $has_complementary) { + $variables['attributes']['class'][] = 'sidebar'; + $variables['attributes']['class'][] = 'two-sidebars'; + } + elseif ($has_navigation) { + $variables['attributes']['class'][] = 'sidebar'; + $variables['attributes']['class'][] = 'one-sidebar'; + $variables['attributes']['class'][] = 'is-visible--navigation'; + } + elseif ($has_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..908333d 100644 --- a/includes/preprocess.page.inc +++ b/includes/preprocess.page.inc @@ -9,6 +9,14 @@ * 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['page_'. $key] = _kiso_render_region($variables['page'][$key]); + $variables['has_' . $key] = _kiso_has_region($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..3ce4afe 100644 --- a/includes/preprocess.utils.inc +++ b/includes/preprocess.utils.inc @@ -1,14 +1,94 @@ render($region); +} + /** * @file * 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 = '') { + $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. + * + * @param string $markup + * Original markup. + * @param string|array $allowed_tags + * Allowed tags, can be an array of tag of a string. + * + * @return string + * @throws \Twig\Error\RuntimeError */ -function _kiso_strip_tags(string $markup, string $allowed_tags = '') { - $allowed_tags .= ''; - return trim(strip_tags($markup, $allowed_tags)); +function _kiso_strip_tags(string $markup, string|array $allowed_tags) { + $allowed_tags_base = [ + '', + '', + '
', + '