Roberto Torresani
Roberto Torresani

Reputation: 182

find page by title in extension

In my extension I want to find a page by title. I try:

/**
 * PageRepository
 *
 * @var \TYPO3\CMS\Frontend\Page\PageRepository
 * @inject
 */
protected $pageRepository = NULL;

and then

$this->pageRepository->findByTitle('my title');

I get the error:

Oops, an error occurred: Call to undefined method TYPO3\CMS\Frontend\Page\PageRepository::findByTitle()

How do I find a page by title?

Upvotes: 3

Views: 2659

Answers (2)

Pravin Vavadiya
Pravin Vavadiya

Reputation: 3207

Method findByTitle() is not provide by pageRepository. You can use getPage() methods using page Uid like below.

$this->pageRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\PageRepository::class);
$page = $this->pageRepository->getPage($pageId, false);

Upvotes: 3

Ravi Sachaniya
Ravi Sachaniya

Reputation: 1633

This getRecordsByField() method of \TYPO3\CMS\Frontend\Page\PageRepository class returns the array of page records.

You need to pass page title into 3rd argument of the method as below.

$pagesArr = $this->pageRepository->getRecordsByField('pages', 'title', 'my title');

Here is the documentation about the : PageRepository Class Reference https://api.typo3.org/typo3cms/current/html/class_t_y_p_o3_1_1_c_m_s_1_1_frontend_1_1_page_1_1_page_repository.html#a7943e29e2820497d6e30b1414120075e

Upvotes: 2

Related Questions