Setting the Page Title in TYPO3

Introduction

Changing the browser page title programmatically via PHP is sometimes needed. For example, you are developing an extension with a detail view inside. The default page title (e.g. “Detail”, “Single view”, etc.) will be set from the page on which a plugin is inserted. There are several reasons to change this default page title:

  • In the browser tab the title should be displayed
  • Bookmarking a page should store the “correct” page title
  • For search engine optimisation: The title is shown in search snippets

I will guide you through the possibilities to set a page title in TYPO3 with PHP. Imagine you are developing an extension which holds fruits with two actions in the controller: a list of all available fruits where each fruit is linked to a single page with detailed information about it. On the single page we want to set the browser page title to the name of the fruit.

The Classic Way of Setting the Page Title

Before TYPO3 v9 LTS you had to set the page title by changing a property in the global variable $GLOBALS['TSFE']. This can be done, for example, in the show action of the Extbase controller or in a separate view helper:

// Set the browser page title
$GLOBALS['TSFE']->altPageTitle = $fruit->getName();
// Set title for use in the system extension indexed_search
$GLOBALS['TSFE']->indexedDocTitle = $fruit->getName();

There are some caveats with this variant:

  • A global variable is accessed. That’s not necessarily a good idea.
  • What if another extension uses this possibility, which title will be shown at the end?

It would be nice to have an API to set the browser page title on TYPO3 pages with a defined orderings.

The new API Way

TYPO3 v9 LTS, published in October 2018, introduced a new feature, the PageTitle API.

There are some advantages compared to the previous method:

  • The generation of the browser page title is stored in a separate class.
  • The ordering of different page title generations is possible.

The Page Title Provider

The providers are called in an defined order. If the provider with the highest priority returns an empty string, the next provider in the list is called. And so on.

The built-in Providers

TYPO3 v9 LTS comes with three pre-defined page title providers. The following list reflects the priority of the three providers:

  1. The AltPageTitleProvider returns the deprecated property $GLOBALS['TSFE']->altPageTitle. But keep in mind: This property and thus the provider is removed in TYPO3 v10.
  2. The SeoTitlePageTitleProvider is only available if the system extension seo is activated. The field seo_title in the page properties will be returned.
  3. And last, the RecordPageTitleProvider will return the page title which should be always set in the page properties.

Create your own Provider

In three steps you can build your own page title provider.

First you have to create a page title provider class within your extension, with regard to our example let it call FruitPageTitleProvider:

<?php
declare(strict_types = 1);

namespace Acme\Fruit\PageTitle;

use TYPO3\CMS\Core\PageTitle\AbstractPageTitleProvider;

class FruitPageTitleProvider extends AbstractPageTitleProvider
{
    public function setTitle(string $title): void
    {
        $this->title = $title;
    }
}
Classes/PageTitle/FruitPageTitleProvider.php

The class extends the AbstractPageTitleProvider from the TYPO3 core. It is small and holds only a title property and a getter for returning the title.

In the FruitPageTitleProvider you just define a setter for the title which stores the given page title in the title property.

In the second step you use this class in your Extbase action and set the according browser page title:

<?php
declare(strict_types = 1);

namespace Acme\Fruit\Controller;

use Acme\Fruit\Domain\Model\Fruit;
use Acme\Fruit\PageTitle\FruitPageTitleProvider;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

class FruitController extends ActionController
{

    // ... some other stuff

    public function showAction(Fruit $fruit): void
    {
        $titleProvider = GeneralUtility::makeInstance(FruitPageTitleProvider::class);
        $titleProvider->setTitle($fruit->getName());

        $this->view->assign('fruit', $fruit);
    }
}
Classes/Controller/FruitController.php

And last but not least you have to tell TYPO3 about our newly created provider. This is done with the following TypoScript setup:

config.pageTitleProviders {
    tx_fruit {
        provider = Acme\Fruit\PageTitle\FruitPageTitleProvider
        before = record
    }
}

With TYPO3 v9 LTS a new config TypoScript property was introduced: The pageTitleProviders. For each provider there is an entry in this list which defines the priority of the particular providers. We define a new entry in this list. It should be unique, otherwise you may override an existing provider, so we call it tx_fruit. With the tx_ prefix we separate our provider from the providers delivered by the core – but this is just my recommendation.

Of course, you can do some other stuff in your page title provider instead of just having a setter, like querying the database. Important is only that the property $title is set to the desired value.

Don’t repeat yourself

If you only have one extension where you set the page title this way, the effort is fine. But imagine, you have several extensions, then it would be practical not to have to repeat this in every extension again and again. Either you can write an extension that only sets the page title (and use it for all your extensions) or you can apply the extpagetitle extension to your TYPO3 installation that does just that for you.