Skip to content

Instantly share code, notes, and snippets.

@jenlampton
Last active November 10, 2020 18:02
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 jenlampton/1afd107d71ff01ff1bc051728ae45b0c to your computer and use it in GitHub Desktop.
Save jenlampton/1afd107d71ff01ff1bc051728ae45b0c to your computer and use it in GitHub Desktop.
How to add a subtitle to a page template
.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']);
}
}
}
}
@jenlampton
Copy link
Author

jenlampton commented Nov 6, 2020

template file would then look something like this

  <div class="l-page-title gdlr-page-title-wrapper gdlr-parallax-wrapper" data-bgspeed="0.5">
    <div class="gdlr-page-title-overlay"></div>
    <div class="gdlr-page-title-container container container-fluid">
      <?php print render($title_prefix); ?>
      <?php if ($title): ?>
        <h1 class="page-title"><?php print $title; ?></h1>
      <?php endif; ?>
      <?php if ($subtitle): ?>
        <h2 class="subtitle gdlr-page-caption"><?php print $subtitle; ?></h2>
      <?php endif; ?>
      <?php print render($title_suffix); ?>
    </div>
  </div>

@rstvo14
Copy link

rstvo14 commented Nov 8, 2020

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>

@kthull
Copy link

kthull commented Nov 9, 2020

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.

@jenlampton
Copy link
Author

jenlampton commented Nov 10, 2020

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.

@kthull
Copy link

kthull commented Nov 10, 2020

@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.

@jenlampton
Copy link
Author

jenlampton commented Nov 10, 2020

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?

@jenlampton
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment