Skip to main content
2 of 3
added 107 characters in body
CodeNext
  • 3.7k
  • 1
  • 20
  • 40

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

CodeNext
  • 3.7k
  • 1
  • 20
  • 40