LinkHandler hook on TYPO3 8 - content-management-system

Is there any hook to handle links with the new LinkHandler for TYPO3 8.7?
On the old LinkHandler extension is possible to define a hook to process the links as we want.
I need to overwrite the parameter of typolink based on some rules. Is there a way to do this on my extension?

There are multiple points to hook into.
TypoLink post-processing
You can hook into the TypoLink post-processing to modify the typolink itself before it gets rendered.
For this, you first register your custom class in ext_tables/ext_localconf:
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_content.php']['typoLink_PostProc'][] = 'Vendor\\Name\\Service\\TypoLinkProcessingService->postProcessTypoLink';
Then, inside your TypoLinkProcessingService class (with your name of choice, of course), you'd handle it inside your own method. For visualisation purposes, in this example, I'm altering an URL if it is a link to a youtube video in order to turn off "related videos" at the end, and to modify the color used by the controls inside youtube's player.
public function postProcessTypoLink(&$parameters, ContentObjectRenderer &$parentObject)
{
if (isset($parameters['finalTagParts']['url'])) {
$urlParts = parse_url($parameters['finalTagParts']['url']);
if (stristr($urlParts['host'], 'youtube.com') !== false && stristr($urlParts['path'], 'watch') !== false) {
$parameters['finalTag'] = str_replace(
'"' . htmlspecialchars($parameters['finalTagParts']['url']) . '"',
'"' . htmlspecialchars($parameters['finalTagParts']['url'] . '&rel=0&color=ffffff') . '"',
$parameters['finalTag']
);
}
}
}
TypoLink UserFunc
Another option is to utilise userFunc and adapt links.
For this, you configure your linkhandler configuration (PageTS) to provide userFunc inside typolink. Add TypoScript as needed to later fetch the configured data.
config.recordLinks {
tx_myest {
typolink {
userFunc = Vendor\Name\UserFunc\TypolinkUserFunc->parseLinkHandlerTypolink
userFunc {
newsUid = TEXT
newsUid.data = field:uid
newsClass = TEXT
newsClass.data = parameters:class
defaultDetailPid = 53
}
}
}
}
Inside your parseLinkHandlerTypolink method, you can access configured properties and adapt as required:
class TypolinkUserFunc
{
/**
* #var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
* #inject
*/
public $cObj;
/**
* Add a method description here
*
* #param array $content
* #param array $configuration
* #return string
*/
public function parseNewsLinkHandlerTypolink(array $content, array $configuration)
{
if (!$configuration['newsUid']) {
return;
}
$params = $this->cObj->cObjGetSingle($configuration['newsClass'], $configuration['newsClass.']);
$newsUid = (int)$this->cObj->cObjGetSingle($configuration['newsUid'], $configuration['newsUid.']);
// ... your code goes here ...
$url = $this->cObj->typolink('', $typolink);
return '<a href="' . $url . '" ' . $attributes . '>';
}
}
Alternatively, this hook that has been introduced in 8.6 may also help you: https://docs.typo3.org/typo3cms/extensions/core/Changelog/8.6/Feature-79121-ImplementHookInTypolinkForModificationOfPageParams.html

Related

Change language parameters in site configuration for some pages in a site

Is there a possibility in TYPO3 10 LTS to change the option fallbackType in the site configuration for a single page?
Background:
I use for the site the option fallbackType fallback. But for a few page I need fallbackType strict. Up to TYPO3 8 this could be achieved with a simple extension template and different config configurations. Is there a way in TYPO3 10?
As far as i know there is no possibility to configurate TYPO3 to behave in this way. The only way to override the language configuration for a single page is a custom middleware.
You can find a working example here: https://github.com/qbus-agentur/typo3-dynamic-language-mode.
I changed the main function:
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$lang = $this->getCurrentSiteLanguage($request);
$pageArguments = $this->getPageArguments($request);
if ($lang === null || $pageArguments === null) {
return $handler->handle($request);
}
// get current page ID
$currentRouting = $request->getAttribute('routing');
$currentPage = $currentRouting->getPageId();
$site = $request->getAttribute('site');
$default = $site->getLanguages()[0];
if ($lang->getLanguageId() !== $default->getLanguageId()) {
// Check if page is in $pagesWhereToChange and apply a dynamic language configuration in that case
if (in_array($currentPage, $this->pagesWhereToChange) > 0) {
\Closure::bind(function() use ($lang, $newId) {
$lang->fallbackType = 'free';
$lang->fallbackLanguageIds = [];
}, null, SiteLanguage::class)();
}
}
return $handler->handle($request);
}
Where $this->pagesWhereToChange is an array of pages:
/**
* pagesWhereToChange
*
* #var array
*/
protected $pagesWhereToChange = [30,41];
It's not possible to configure the array with typoscript because TSFE is not rendered yet.

TYPO3: Render a plugin via Typoscript or ViewHelper and change settings

I would like to load a plugin dynamically according to some data. First I tried to do it with Typoscript, but after some research I figured out, that it is not possible to change the settings of the plugin (see old forum entry).
I need to change settings.simplepoll.uid according to the passed data:
This is the Typoscript I tried:
lib.loadSimplepoll = USER
lib.loadSimplepoll {
userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
extensionName = Simplepoll
pluginName = Polllisting
vendorName = Pixelink
switchableControllerActions {
SimplePoll {
1 = list
}
}
settings < plugin.tx_simplepoll.settings
settings {
simplepoll {
uid.current = 1
}
}
}
The call in the template looks like that:
<f:cObject typoscriptObjectPath="lib.loadSimplepoll">{newsItem.simplepoll}</f:cObject>
After figuring out, that changing the settings is not possible, I tried a viewhelper:
<?php
namespace Vendor\Extension\ViewHelpers;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class LoadSimplepollViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
{
/**
* #param int $uid Uid of poll
* #return string
*/
public function render($uid) {
$cObj = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer');
$configurationManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
$simplepollTs = $configurationManager->getConfiguration(
\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'simplepoll',
'Polllisting'
);
$ttContentConfig = array(
'tables' => 'tt_content',
'source' => 1030,
'dontCheckPid' => 1
);
// returning this works perfectly!
// but I need to change the "settings.simplepoll.uid"
$data = $cObj->RECORDS($ttContentConfig);
$cObj->start($data, 'tx_simplepoll_domain_model_simplepoll');
$renderObjName = '<tt_content.list.20.simplepoll_polllisting';
$renderObjConf = $GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.']['simplepoll_polllisting.'];
$renderObjConf['persistence']['storagePid'] = 394; // This does not work!
$renderObjConf['settings'] = $simplepollTs;
$renderObjConf['settings']['simplepoll']['uid'] = $uid;
return $cObj->cObjGetSingle($renderObjName, $renderObjConf);
}
}
The viehelper is called like this:
{vh:LoadSimplepoll(uid: '{newsItem.simplepoll}')}
Now I am able to change the uid of the poll with this line:
$renderObjConf['settings']['simplepoll']['uid'] = $uid;
My problem is now, that it loads the poll, but not the answers. I tracked this down to the fact, that the plugin somehow does not know the Record Storage Page anymore. The line $renderObjConf['persistence']['storagePid'] = 394; does not help.
How can I tell the plugin the Storage Pid?
Or is there another/better way to load a plugin with changing data?
Why shouldn't it be possible to modify settings.simplepoll.uid in typoscript?
because the extension simplepoll does not handle any stdWrap functionality to its typoscript settings.
Have a look into the code:
this special setting is used here:
$simplePoll = $this->simplePollRepository->findByUid($this->settings['simplepoll']['uid']);
no stdWrap, just plain usage.
compare it to ext:news:
before any settings is used it is processed. A dedicated join of typoscript settings with the settings in the plugin. And if necessary there is a stdWrap possible: here
$this->originalSettings = $originalSettings;
// Use stdWrap for given defined settings
if (isset($originalSettings['useStdWrap']) && !empty($originalSettings['useStdWrap'])) {
$typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
$typoScriptArray = $typoScriptService->convertPlainArrayToTypoScriptArray($originalSettings);
$stdWrapProperties = GeneralUtility::trimExplode(',', $originalSettings['useStdWrap'], true);
foreach ($stdWrapProperties as $key) {
if (is_array($typoScriptArray[$key . '.'])) {
$originalSettings[$key] = $this->configurationManager->getContentObject()->stdWrap(
$typoScriptArray[$key],
$typoScriptArray[$key . '.']
);
}
}
}
As you can see:
extbase does not support you with typoscript stdWrap functionality.
You (and every extension author) need to do it by hand. But that was so even before extbase.
In this way: as you can not configure your value you only can trick TYPO3 (and the plugin):
if you have a small number of uids you can have one variant for each uid
lib.loadSimplepoll123 < lib.loadSimplepoll
lib.loadSimplepoll123.settings.simplepoll.uid = 123
lib.loadSimplepoll234 < lib.loadSimplepoll
lib.loadSimplepoll234.settings.simplepoll.uid = 234
lib.loadSimplepoll345 < lib.loadSimplepoll
lib.loadSimplepoll345.settings.simplepoll.uid = 345
lib.loadSimplepoll456 < lib.loadSimplepoll
lib.loadSimplepoll456.settings.simplepoll.uid = 456
and call it like
<f:cObject typoscriptObjectPath="lib.loadSimplepoll{newsItem.simplepoll}" />
or you build a pull request implementing the stdWrap functionality and send it to the extension author.
Why shouldn't it be possible to modify settings.simplepoll.uid in typoscript?
you just need the correct construction to modify it.
For a single value you can use current, but use it properly. It is a stdWrap function which needs to be evaluated.
If there is no stdWrap evaluation by default it might work with a cObject of type TEXT
settings.simplepoll.uid.cObject = TEXT
settings.simplepoll.uid.cObject.current = 1
or to indicate a stdWrap you need to use stdWrap literally:
settings.simplepoll.uid.stdWrap.current = 1
another variant of data transfer are named parameters. Just build an associative array as data parameter and access the values individual:
fluid:
<f:cObject typoscriptObjectPath="lib.arraytest" data="{a:'abc',b:'xyz'}" >
inside text
</f:cObject>
and the typoscript:
lib.arraytest = COA
lib.arraytest {
10 = TEXT
10.field = a
10.wrap = /|/
20 = TEXT
20.field = b
20.wrap = \|\
}
which results in an output of /abc/\xyz\. Be aware: the inner text of the f:cobject tag will be lost as the data parameter has priority about inner children.
In the meantime I got the Viewhelpter to work:
Viewhelper:
<?php
namespace Vendor\Extension\ViewHelpers;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class LoadSimplepollViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
{
/**
* #return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('simplepollUid', 'int', 'Uid of simplepoll', false);
}
/**
* #return string
*/
public function render()
{
$configurationManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
$simplepollTs = $configurationManager->getConfiguration(
\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'simplepoll',
'Polllisting');
$cObj = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer');
$renderObjName = '<tt_content.list.20.simplepoll_polllisting';
$renderObjConf = $GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.']['simplepoll_polllisting.'];
$renderObjConf['settings'] = $simplepollTs;
$renderObjConf['settings']['simplepoll']['uid'] = (int)$this->arguments['simplepollUid'];
return $cObj->cObjGetSingle($renderObjName, $renderObjConf);
}
}
Call the viewhelper in the template (don't forget to register the namespace):
{vh:LoadSimplepoll(simplepollUid: '{newsItem.ipgSimplepoll}')}
That's it.

TYPO3 8 show layout selection in backend preview for textmedia

I try to use and customize the CTypes of fluid_styled_content as much as possible. Therefore the select-field "Layout" is very useful to have a possibility to select some different styles (like red box, shadow, or image-stuff). But if you have some possibilities to select it is not shown in backend preview an every element is looking the same.
Is there a way to show the selected value the layout field in backend preview for textmedia?
To get this done register a hook (path: yourextension/Classes/Hooks/PageLayoutView/TextMediaCustomPreviewRenderer.php) like that:
<?php
namespace Vendor\Yourextension\Hooks\PageLayoutView;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use \TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface;
use \TYPO3\CMS\Backend\View\PageLayoutView;
/**
* Contains a preview rendering for the page module of CType="textmedia"
*/
class TextMediaCustomPreviewRenderer implements PageLayoutViewDrawItemHookInterface
{
/**
* Preprocesses the preview rendering of a content element of type "Text Media"
*
* #param \TYPO3\CMS\Backend\View\PageLayoutView $parentObject Calling parent object
* #param bool $drawItem Whether to draw the item using the default functionality
* #param string $headerContent Header content
* #param string $itemContent Item content
* #param array $row Record row of tt_content
*
* #return void
*/
public function preProcess(
PageLayoutView &$parentObject,
&$drawItem,
&$headerContent,
&$itemContent,
array &$row
)
{
$pageTs = \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($row['pid']);
if ($row['CType'] === 'textmedia') {
$itemContent .= '<p>Layoutversion: ' . $pageTs['TCEFORM.']['tt_content.']['layout.']['types.']['textmedia.']['addItems.'][$row['layout']] . '</p>';
if ($row['bodytext']) {
$itemContent .= $parentObject->linkEditContent(
$parentObject->renderText($row['bodytext']),
$row
) . '<br />';
}
if ($row['assets']) {
$itemContent .= $parentObject->thumbCode($row, 'tt_content', 'assets') . '<br />';
}
$drawItem = false;
}
}
}
And in your ext_localconf.php you put like that:
// Register for hook to show preview of tt_content element of CType="textmedia" in page module
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem']['textmedia'] = \Vendor\Yourextension\Hooks\PageLayoutView\TextMediaCustomPreviewRenderer::class;
In my case I offer the different options of the select in pageTsconfig like that:
TCEFORM.tt_content.layout.types.textmedia.addItems {
50 = Textbox grau, Bildergalerie oben
60 = Textbox grau, Bildergalerie unten
110 = Blau, rechtsbündig
210 = Hellblau, linksbündig
220 = Rot, linksbündig
310 = Akkordeon
}
It is the better way to use correct language handling by locallang.xlf for that. If you do it like that maybe you have to change the code example a bit...
This was the result of a thread at "TYPO3 Fragen, Antworten, inoffizielle Gruppe" on Facebook. Thanks a lot to every helping hand specially to Wolfgang Klinger :-)

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)
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.
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);
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

Fluidtypo3: Use custom field as title of FCE

I don't want to use the default header in my FCE's, but only custom flux fields. In the backend list views my FCE's are shown as "[no title]" because the default header is not filled. This leads to much confusion for editors.
How can I define one of my custom flux fields to be used as title for the FCE in TYPO3 Backend list views etc.?
You can't just use a field from the flexform, because all fields from the FCE are stored in the same field in the database (pi_flexform).
What you can do is to render the content element title with a user function. It is registered with a line like this in the TCA config:
$GLOBALS['TCA']['tt_content']['ctrl']['label_userFunc'] = 'Vendor\\Extkey\\Utility\\ContentElementLabelRenderer->getContentElementTitle';
The user function itself could look like this:
<?php
namespace Vendor\Extkey\Utility;
/**
* This class renders a human readable title for FCEs,
* so one is able to find a content element by its headline.
*/
class ContentElementLabelRenderer implements \TYPO3\CMS\Core\SingletonInterface {
/**
* #var \TYPO3\CMS\Extbase\Service\FlexFormService
* #inject
*/
protected $flexFormService = null;
/**
* Returns the content element title for a given content element
*/
public function getContentElementTitle(&$params) {
if (null === $this->flexFormService) {
$this->flexFormService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Service\\FlexFormService');
}
if (ctype_digit($params['row']['uid']) && 'fluidcontent_content' === $params['row']['CType']) {
// If this is a FCE, parse the flexform and template name and generate the
// title in a template specific way.
$row = $params['row'];
$additionalRowData = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('pi_flexform, tx_fed_fcefile', 'tt_content', 'uid = ' . $row['uid']);
$flexFormContent = $this->flexFormService->convertFlexFormContentToArray($additionalRowData['pi_flexform']);
$lastColonPosition = strrpos($additionalRowData['tx_fed_fcefile'], ':');
$contentElementType = (FALSE === $lastColonPosition) ? 'invalidtype' : substr($additionalRowData['tx_fed_fcefile'], $lastColonPosition + 1);
switch ($contentElementType) {
case 'Image.html':
$params['title'] = 'Image: "' . ($flexFormContent['title'] ?: $flexFormContent['subtitle']) . '"';
break;
default:
$params['title'] = 'Unknown content element type';
break;
}
}
else {
// If this is not a FCEm, print out "normal"
// title. Not the real thing, but comes pretty close, hopefully.
$params['title'] = $params['row']['header'] ?: ($params['row']['subheader'] ?: $params['row']['bodytext']);
}
}
}
This produces a maintainance problem though: Every time you add or change a content element, you have to update this file.