Is there a way to print the page title within a paragraph.html.twig?

In Drupal 7 your could just do a drupal_get_title(); in an tpl.php and use it as a variable.

That function is depreciated and from what I read about TWIG not in the spirit of themeing in Drupal 8 now.

What is the Drupal 8 way to do this?

2 Answers 2


An alternative strategy to setting cache context is to use a placeholder. This is a more complex approach but you will never have issues with the database cache table filling up - as 4k4 points out this can be a real problem.

All of this code goes in themename.theme.

First we must define a callback function that returns the page title to the placeholder:

function _MyTheme_paragraphs_title_placeholder($page_title) {
  // Must go here, if you attempt to pass in $page_title from preprocess it will
  // be cached!
  $request = \Drupal::request();
  $route_match = \Drupal::routeMatch();
  $page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());

  $title_array = array(
    '#theme' => 'page_title',
    '#title' => $page_title,

  return $title_array;

Next we create the placeholder in preprocess, we will print a variable in the template, so we must populate it with the placeholder:

function MyTheme_preprocess_paragraph(&$variables) {
  // Note: the first item in the #lazy_builder array is our callback.
  $placeholder_title = array(
    '#lazy_builder' => ['_MyTheme_paragraphs_title_placeholder', ['page_title']],
    '#create_placeholder' => TRUE,

  $variables['title'] = $placeholder_title;

Finally create copy the paragraphs template and use the title variable:

<div{{ attributes.addClass(classes) }}>
  {{ title }}
  {{ content }}

This is how I have implemented the breadcrumb title feature in Adaptivetheme (D8 version), to avoid cache context url as I noticed the database entries, I think it's a great method and I'd love more feedback if I have done something wrong - I'm a themer of course, not a hardcore dev :)


You can borrow the code from preprocess breadcrumb:

function mytheme_preprocess_paragraph(&$variables)
  $request = \Drupal::request();
  $route_match = \Drupal::routeMatch();
  $variables['title'] = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());

  $variables['#cache']['contexts'][] = 'url';


{{ title }}

You need to set a cache context. Normally a paragraph is cached until the cache tag is invalidated by a change in the database. It will appear with the title it was first generated with on all following pages. If you use a context on the url, you get a new cached version of the paragraph for every new path. This will fill up your cache table. So you should try to target only that paragraphs where you need the title.

Or think about another solution, because if you have a big site with many pages this may not be a good idea at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.