1

The title of this user view page contains the name of the user. I want modify it and, thanks to twig debug, I see the template used is page-title.html.twig:

{% if title %}
  <h1{{ title_attributes.addClass('page-header') }}>{{ title }}</h1>
{% endif %}

If I override it, how can I make twig use my overrided template only in the user view page?

4
  • I think there is more than one way to achieve this, it depends on what content you want to display, whether that content is dynamic or static. If static, there is a simple way, even you do not need to touch code. If dynamic, then it depends. So what you want to change?
    – CodeNext
    Commented Oct 24, 2016 at 4:43
  • I want add a string before the user name in the view user page (/user). Instead "CodeNext", have "Explorer CodeNext" by example. This string is dynamic, it depend some user parameters. Commented Oct 24, 2016 at 6:17
  • 1
    So, how you are going to collect that string? Are you going to provide a input field so user can fill that field for this. Or you are going to set manually. Because, if this is the dynamic, Two things required, First from where this data(string) is coming and Second where it will be shown. So where it will be shown we can show that will be not big task, but how data will be input and how to fetch that is the thing.
    – CodeNext
    Commented Oct 24, 2016 at 6:27
  • I just thought push the value of the prefix string from the theme hook, like this (drupal.org/docs/8/theming/twig/…). Then, from the template, Ill add the prefix string to the "user" name string. By that way, the problem is the template I want use is generic and affect all title of all page, not only the user view page Commented Oct 24, 2016 at 9:15

1 Answer 1

3

As you mentioned your main issue is whatever data you are fetching it is overriding the all page-title on the site which you do not want. So to solve that follow following steps,

For common understanding I am referring bartik theme. I am showing with example to my local site so it will be very practical to understand.

In bartik.theme file,

function bartik_preprocess_page_title(&$variables) {
  $variables['staticString'] = 'Explorer ';
  $variables['user_profile_view'] = (\Drupal::routeMatch()->getRouteName() == 'entity.user.canonical'); }

in page-title.html.twig file,

{% if title %}
  {% if user_profile_view %}
    <h1{{ title_attributes.addClass('page-header') }}>{{ staticString }}{{ title }}</h1>
  {% else %}
    <h1{{ title_attributes.addClass('page-header') }}>{{ title }}</h1>
  {% endif %}
{% endif %}

Output image, On user page,

enter image description here

On other page(It will not interfere),

enter image description here

So, this way you can Override the page-title of user-profile page and can insert your own defined variable. So, this answer is with static variable, now same way, you have to just replace that variable with your dynamic variable, so it will get change based on your code.

Note: In solving this question, I have asked another question here: How to get the part of URI and pass it to as a argument?.

4
  • Work perfectly ! I have two questions , every twig variables are overridable by that way ? and, in "_preprocess_page_title", page_title is the name of the template I want override ? If no, how can I know I must use the string 'page_title' in "preprocess_page_title' ? Commented Oct 24, 2016 at 19:47
  • 1
    @user2137454 I got your 1st question, and answer is, Yes, every variable can be override, using the appropriate _preprocess hook, and preprocess hooks are made for that purpose only in Drupal, so before presenting data on site, if we want to change we can apply change. Could you please re-frame your second question?
    – CodeNext
    Commented Oct 24, 2016 at 19:57
  • sorry for my english writing x) In the future, if I want override another template variable, how can I know what is the {HOOK} (the string) I must use in the function hook_preprocess_{HOOK} name function ? Commented Oct 24, 2016 at 20:30
  • 1
    @user2137454 That is very easy, which preprocess you have to use that will be provided in the comment section of that twig file in last lines. For example, If you open, page.html.twig in comment section in last you can see one line written as @see template_preprocess_page(). This way every template provides which _preprocess is corrosponding to them. For page-title it is written @see template_preprocess_page_title(), I do not know why in my bartik theme's page-title.html.twig, that line is not there..:), But it should be...
    – CodeNext
    Commented Oct 24, 2016 at 20:37

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.