1

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?

3
  • Are you using the Metatag module?
    – sonfd
    Jun 27, 2020 at 0:23
  • No, I was in fact hoping to avoid adding another module just to change the main page title. Jun 27, 2020 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, 2020 at 5:35

4 Answers 4

7

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

5
  • Any way to do this without a module (and without callbacks?) Jun 27, 2020 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, 2020 at 3:25
  • 1
    Metatag is a staple module. I can't imagine a project where I wouldn't use it.
    – sonfd
    Jun 27, 2020 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. Jun 27, 2020 at 14:25
  • metatag doesn't work for me. I changed "Page title" metatag and this does not pull through to the page title in <title>...</title> - i.e. that shown in the window pane heading or browser tab itself. Apr 7, 2021 at 7:38
5

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.

0
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');
  }
}
4
  • This strikes me as something that should be baked into Drupal's core UI. Jul 1, 2020 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 Jul 1, 2020 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, 2020 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. Jul 3, 2020 at 2:46
-2

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.

6
  • 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, 2020 at 5:51
  • I apologize, included the appropriate attribution. Jul 2, 2020 at 23:10
  • Unclear why it was necessary to rollback coding standards. Other people finding your answer now see how it's not supposed to be done.
    – leymannx
    Jul 12, 2020 at 21:57
  • 1
    For using the backslash in a class name, that is said in drupal.org/docs/develop/coding-standards/namespaces. Classes and interfaces without a backslash \ inside their fully-qualified name (for example, the built-in PHP Exception class) must be fully qualified when used in a namespaced file. For example: new \Exception();. Do not use global classes. For the Drupal class, it's the same as the Exception class.
    – apaderno
    Jul 23, 2020 at 12:30
  • 1
    As for the opening bracket on the same line containing the arguments declaration, the coding standards aren't explicit, but that has always been how the Drupal core code is formatted.
    – apaderno
    Jul 23, 2020 at 12:31

Your Answer

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

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