2

I changed the site's front page from /node to another page. However, now the title of the front page is page name | site name. To clear up any confusion, by "title," I mean the phrase entered between the HTML <title> tags.

I tried this SO answer, but it had no effect. How can I declaratively change the name of the site's main page?

  • Are you using the Metatag module? – sonfd Jun 27 at 0:23
  • No, I was in fact hoping to avoid adding another module just to change the main page title. – glenviewjeff Jun 27 at 2:58
  • What do you want it to be instead? And you mean the meta title that's been displayed on SERPs and as tab title in browsers, not the page title in the page title block, right? – leymannx Jun 27 at 5:35
5

You can use the Metatag module. It has a section for front page metatags, where you can set the title.

| improve this answer | |
  • Any way to do this without a module (and without callbacks?) – glenviewjeff Jun 27 at 2:57
  • 2
    What do you mean without a callback? And why not this module? It's a very helpful module - most sites need custom metatags of various sorts on them. – Jaypan Jun 27 at 3:25
  • 1
    Metatag is a staple module. I can't imagine a project where I wouldn't use it. – sonfd Jun 27 at 10:26
  • @Jaypan the callback wasn't with respect to your answer; I was hoping/expecting a declarative technique to change the front-page title. By callback method, I was anticipating answers that proposed writing methods such as this one. – glenviewjeff Jun 27 at 14:25
4

You can achieve this with a few lines of code implementing template_preprocess_html in your custom theme.

/**
 * Implements template_preprocess_html().
 */
function MYTHEME_preprocess_html(&$variables) {
  if (\Drupal::service('path.matcher')->isFrontPage()) {
    $variables['head_title']['title'] = t('Hello World');
  }
}
| improve this answer | |
  • This strikes me as something that should be baked into Drupal's core UI. – glenviewjeff Jul 1 at 15:55
  • This didn't work for me; I wonder, does the isFrontPage() only return true if the front page is left at the default /node/0? And of course I flushed all caches. Looks like the backslash leading before Drupal:: is a typo. However, what this code does is only prepend the page title before the site name, such that the page title becomes Hello World | Site Name – glenviewjeff Jul 1 at 16:16
  • @glenviewjeff – Did you get what isFrontPage is doing now that you have it in your own answer or looked up the function yourself? – leymannx Jul 2 at 5:53
  • @leymanx I was confused/mistaken. The backslash was irrelevant/unnecessary and has no effect at all either way. The problem was I'd included the conditional inside x_preprocess_page rather than html, which was the problem. I went to record the correct answer for posterity, not realizing yours was correct other than the minor issue of adding the ['title'] qualifier, which isn't what I was looking for. If you remove that, I'll accept your answer and delete mine. – glenviewjeff Jul 3 at 2:46
4

The default Frontpage is a view (admin/structure/views/view/frontpage/edit) showing a waterfall of all content that is Published and "Promoted to the Frontpage". By default, the Frontpage view's Title is "none" while the HTML Title tag is "Home | MySite.com".

So you can change the Title of the view there to e.g. "Start" and the HTML title will change to "Start | MySite.com".

If, instead of using the Frontpage view, you choose a node for your home page (e.g. "node/10 ), then you can name the page "Home" and the HTML title will become "Home | MySite.com".

If you don't want the title of the page showing up in the page's content area (but you do want it in the home page's HTML title tag), you can hide the title block in admin/structure/block/manage/yourtheme_page_title.

The Metatag module, along with Token, can also be used for this and general SEO enhancements, but you said you are trying to avoid that.

| improve this answer | |
0

It's not possible to change the front page title declaratively without adding a new module, such as the Metatag module. Drupal's core does not have this functionality. However, you can achieve this with a few lines of code implementing template_preprocess_html in your custom theme.

To achieve this dynamically in a subtheme, add this code to your preprocess_html function of your theme file or add if it doesn't exist.

function MYTHEME_preprocess_html(&$variables) {
  if (\Drupal::service('path.matcher')->isFrontPage()) {
    $variables['head_title'] = [t('Hello World')];
  }
}

Note: Credit to @leymanx for the basis for the above code, which just needed a slight modification to update the title as originally asked, rather than just the portion of the before name of the site.

| improve this answer | |
  • Really awkward to copy my answer into yours and not even explaining the slight difference. 👎 And no, the backslash is no typo you could have found out easily by scanning your codebase for that pattern. – leymannx Jul 2 at 5:51
  • I apologize, included the appropriate attribution. – glenviewjeff Jul 2 at 23:10
  • +1. I also find it okay when you accept your own answer as it really matches what you were looking for. It was just the nearly identical copy without explaining the slight differences that bothered me. – leymannx yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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