Score:0

Can't set custom document title in theme-settings.php

gb flag

I'm a newbie with Drupal, so I'm not sure I do everything correctly. I need to set custom document title (i.e. between <title> and </title>), not in a module, but in another PHP file for example theme-settings.php or even root index.php. I'm trying the following code:

function mythemename_preprocess_page_title(&$variables) {
    $variables['title'] = 'New title';
}

But the new title is not rendered in HTML code of main and inner pages.

ps. Drupal 8.7.3

UPDATED. Here https://drupal.stackexchange.com/a/228208/104765 I read that the theme is a wrong place. Is it still possible to set the title without creating a module?

4uk4 avatar
cn flag
This is a rather complex issue, but most times you should be able to get the result you want by using the module Metatag. See https://drupal.stackexchange.com/questions/238704/how-do-i-alter-the-page-title-after-a-controller-sets-the-title
stckvrw avatar
gb flag
I was not able to install the Metatag on my Drupal 8.7.3 - it caused an error. But I've successfully installed the SimpleMeta module. Also it's possible to handle titles in `html.html.twig`
Score:1
cn flag

You need to use the THEMENAME.theme file to store this in.

stckvrw avatar
gb flag
I've tried, but the title is still not changed at any page.
Score:1
de flag

Easiest solution is to use the Metatag module, indeed.

For a full code solution, I'd recommend you to have a look at core/themes/classy/templates/layout/html.html.twig file (which is the default html template used by Drupal if your theme does not override it) for clues concerning template variables. The variable you want to alter is head_title which, as stated in header of html.html.twig is:

 * - head_title: List of text elements that make up the head_title variable.
 *   May contain one or more of the following:
 *   - title: The title of the page.
 *   - name: The name of the site.
 *   - slogan: The slogan of the site.

In your theme myTheme.theme file, use the hook_preprocess_html() hook to alter your $variables["head_title"] variable.

PS: Do not forget to flush caches when working with templates to be sure your modifications are taken into account.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.