-
-
Save jenlampton/1afd107d71ff01ff1bc051728ae45b0c to your computer and use it in GitHub Desktop.
.theme file could have something like this... | |
function hook_preprocess_page(&$variables){ | |
$variables['subtitle'] = FALSE; | |
if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) { | |
$node = node_load(arg(1)); // entity cache (in core) should save us here. | |
if (property_exists($node, 'field_subtitle')) { | |
if (!empty($node->field_subtitle)) { | |
$lang = $node->langcode; | |
$variables['subtitle'] = check_plain($node->field_subtitle[$lang][0]['value']); | |
} | |
} | |
} | |
} |
Going from the post arg() is deprecated and will be removed (https://www.drupal.org/node/2274705)
function hook_preprocess_page(&$variables){
$variables['subtitle'] = FALSE;
// Load the current node.
$node = \Drupal::routeMatch()->getParameter('node');
if ($node) {
if (property_exists($node, 'field_subtitle')) {
if (!empty($node->field_subtitle)) {
$lang = $node->langcode;
$variables['subtitle'] = check_plain($node->field_subtitle[$lang][0]['value']);
}
}
}
}
I still don't think subtitle is being populated or if I am checking the right $node. I updated the template file block--page-title-block.html.twig:
<section id="page-title" class="img-title-bg">
<div{{ attributes.addclass(classes) }}>
{{ title_prefix }}
{% if label %}
<h2{{ title_attributes }}>{{ label }}</h2>
{% endif %}
{% if subtitle %}
<h2 class="subtitle">{{ subtitle }}</h2>
{% endif %}
{{ title_suffix }}
</div>
</section>
The preprocess function you have would be adding the subtitle variable to page templates, so you would have to move this logic to themename_preprocess_block
to add it to the block template you're referencing.
Going from the post arg() is deprecated and will be removed
@Gustavo we talked about this in the meeting, that example is from Drupal 7. You will need to find the Drupal 8 equivalent way to load a node from the page template.
you would have to move this logic to themename_preprocess_block to add it to the block template you're referencing.
There should be no block template in use (if so, it should be removed) -- the page title should only appear in the page template.
@jenlampton D8 moved the page title out of the page template and into a block. Same with messages and tabs (local actions). So in the page twig template, it's actually placing the rendered block.
Is this also true for layout-builder pages? Does the layout-builder area still only replace the main-page-content block like it did with panels in D7?
@Gustavo I tried the following on abag and it worked like a charm:
In .theme
file:
/**
* Implements hook_preprocess_block()
*/
function abag_preprocess_block(&$variables) {
$variables['node'] = \Drupal::routeMatch()->getParameter('node');
}
in block template block--page-title-block.html.twig
:
{% if not is_front %}
<div class="container container-fluid">
<div class="row">
<div{{ attributes.addClass(title_wrapper_classes) }}>
{% block content %}
{{ content }}
{% endblock %}
</div>
{{ node.field_subtitle.value }}
</div>
</div>
{% endif %}
You will of course need to add the markup you want around the subtitle, and some logic to ensure that it won't show on pages that don't have a subtitle.
template file would then look something like this