Skip to content

Instantly share code, notes, and snippets.

@jameswilson
Created January 25, 2018 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameswilson/391e417316794eb14784e4126f979917 to your computer and use it in GitHub Desktop.
Save jameswilson/391e417316794eb14784e4126f979917 to your computer and use it in GitHub Desktop.
Drupal 8 has_page_title
{#
/**
* @file
* Theme override to display a node.
*
* Available variables:
* - see Stable Theme's node.twig.html for standard variables.
* - has_page_title: Flag for page title block visibility. Will be true when
* the iu_page_title block is present on the page. This is useful to add the
* H1 inside the article when page title block not present.
*
* @see template_preprocess_node()
* @see themename_preprocess_node()
*/
#}
<article{{ attributes }}>
{{ title_prefix }}
{% if page and not has_page_title %}
<h1{{ title_attributes }}>{{ label }}</h1>
{% elseif not page %}
<h2{{ title_attributes }}>
<a href="{{ url }}" rel="bookmark">{{ label }}</a>
</h2>
{% endif %}
{{ title_suffix }}
{% if display_submitted %}
<footer>
{{ author_picture }}
<div{{ author_attributes }}>
{% trans %}Submitted by {{ author_name }} on {{ date }}{% endtrans %}
{{ metadata }}
</div>
</footer>
{% endif %}
<div{{ content_attributes }}>
{{ content }}
</div>
</article>
/**
* Prepares variables for `node.html.twig`.
*/
function themename_preprocess_node(&$variables) {
static $cacheable_metadata_list = [];
$blockRepository = \Drupal::service('block.repository');
/** @var $blocks \Drupal\block\BlockInterface[] */
$blocks = $blockRepository->getVisibleBlocksPerRegion($cacheable_metadata_list);
$variables['has_page_title'] = (isset($blocks['page_title']) && !empty($blocks['page_title']['themename_page_title']));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment