Philipp M
Philipp M

Reputation: 3498

TYPO3 Extbase - Change browser page title in single view

I'm trying to change the browser page title when in single view of my extbase extension. All my attempts failed:

/**
 * action show
 *
 * @param \Vendor\Abc\Domain\Model\Abc $record
 * @return void
 */
public function showAction(\Vendor\Abc\Domain\Model\Abc $record) {
    $this->view->assign('record', $record);

    //$GLOBALS['TSFE']->page['title'] = $record->getAbc();
    //$GLOBALS['TSFE']->indexedDocTitle = $record->getAbc();        

    //$GLOBALS['TSFE']->page['title'] = $record;
    //$GLOBALS['TSFE']->indexedDocTitle = $record;  

    //$GLOBALS['TSFE']->additionalHeaderData['CustomUserIntTitle']
    //= '<title>' . $this->getAbc($record) . '</title>';

    //$myNewTitle = 'Title';
    //$title = '<title>' . $myNewTitle . '</title>';
    //$this->response->addAdditionalHeaderData($title);

    //$GLOBALS['TSFE']->content = preg_replace('#<title>.*<\/title>#', '<title>' . $record->getTitle() . '</title>', $GLOBALS['TSFE']->content);

    //$this->response->addAdditionalHeaderData('<title>Mein eigener Title</title>');
}

I registred the action as non-cacheable (not sure if I really have to though)

Upvotes: 3

Views: 7275

Answers (3)

Paul Beck
Paul Beck

Reputation: 2683

If TYPO3 >= 9 LTS follow:

https://stackoverflow.com/a/63745294/4533462

For TYPO3 <= 8 LTS you can do it like this

The solution of Jan is a regular way of changing the depending on GET Params or Page ID.

As you tried to change the title inside the controller is depending on how the page title is set in the Typoscript. However, changing the title inside the controller is possible using the PageRenderer:

$this->objectManager->get(\TYPO3\CMS\Core\Page\PageRenderer::class)->setTitle('My title');
// For the search
$GLOBALS['TSFE']->indexedDocTitle = 'My title';

If it is not working with PageRenderer, you must have a special configuration for your page title in Typoscript or other extensions override the title.

Upvotes: 6

Oleh V Karun
Oleh V Karun

Reputation: 736

In TYPO3 9-10 new logic. The last answer doesn't work for me so I used this https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/PageTitleApi/Index.html

First. Create your own Title provider. path 'ext/Classes/PageTitle/MyRecordTitleProvider.php'

<?php
namespace Vendor\Ext\PageTitle;

use TYPO3\CMS\Core\PageTitle\AbstractPageTitleProvider;

class MyRecordTitleProvider extends AbstractPageTitleProvider
{
    /**
     * @param string $title
     */
    public function setTitle(string $title)
    {
        $this->title = $title;
    }
}

Second. In your TypoScript Setup pageTitleProviders. The main idea that classes override each other and you can set up the order (priority). Like that

config.pageTitleProviders.myext {
      provider = Vendor\Ext\PageTitle\MyRecordTitleProvider
      before = altPageTitle,record,seo
   }
}

Expl. First, check the normal page title and all normal setup like

config{
    pageTitleFirst = 1
    pageTitleSeparator = |
    pageTitleSeparator.noTrimWrap = | | |
}

Will make all page titles like that "Page title | Website title"

'Website title' will be taken from Sites -> websiteTitle Then In priority our next Provider, Will override the normal page title. Like "Ext title | Website title"

Last in our settings seo_title override.

Now all ready for our title setup in ExtBase controller. We need just add in showAction

    $GLOBALS['TSFE']->indexedDocTitle = $title;
    $titleProvider = GeneralUtility::makeInstance(MyRecordTitleProvider::class);
    $titleProvider->setTitle($title);

Upvotes: 7

Jan
Jan

Reputation: 83

Try with TS (sample is from Georg Ringers excellent ext:news):

[globalVar = TSFE:id = NEWS-DETAIL-PAGE-ID]
config.noPageTitle = 2

temp.newsTitle = RECORDS
temp.newsTitle {
  dontCheckPid = 1
    tables = tx_news_domain_model_news
    source.data = GP:tx_news_pi1|news
    source.intval = 1
    conf.tx_news_domain_model_news = TEXT
    conf.tx_news_domain_model_news {
        field = title
        htmlSpecialChars = 1
    }
    wrap = <title>|</title>
}
page.headerData.1 >
page.headerData.1 < temp.newsTitle

[global]

you just need to make some changes accordingly to your extension

Upvotes: 5

Related Questions