FluidTYPO3 / vhs

TYPO3 extension VHS: Fluid ViewHelpers
https://fluidtypo3.org
Other
190 stars 229 forks source link

v:content info not working in v11 without ContentUid set #1799

Closed Neowar closed 11 months ago

Neowar commented 1 year ago

Hi,

when using the ViewHelper "v:content.info" without "contentUid" set, the VewHelper returns an empty array under TYPO3 v11.5. Under TYPO3 v10.4 and smaller it correctly returns a data array of the current content item. This is used e.g. for TYPO3 Form in connection with the Ajax-Submit (https://www.comuno.net/blog/detail/formular-mit-typoscript-rendering-per-ajax-verschicken/), because here the current Uid of the content element is needed.

In the ViewHelper there is the following code, which determines the record from the current cObject if the ContentUid is not set (which is not known):

https://github.com/FluidTYPO3/vhs/blob/development/Classes/ViewHelpers/Content/InfoViewHelper.php#L75

if (0 === $contentUid) {
    $cObj = $this->configurationManager->getContentObject();
    $record = $cObj->data;
}

However, as of version 11.5, TYPO3 does not seem to provide the data of the current content element in the context of the ViewHelper in the getContentObject() called. Can anyone confirm this?

We have now helped ourselves a bit (since we are currently lifting a project to TYPO3 v11.5 and it is needed there) and adapted the call in the VerHelper a bit. So we get the current FE-record as "tt_content:UID", read the UID from it and set it for the following determination as "contentUid".

if (0 === $contentUid) {
    $cObj = $this->configurationManager->getContentObject();
    if($cObj->data)
        $record = $cObj->data;
    else {
        list($tableName, $uid) = $this->resolveTableNameAndUidFromContextString($cObj->getTypoScriptFrontendController(), $cObj->getTypoScriptFrontendController()->currentRecord);
        $contentUid = (int)$uid;
    }
}

Plus a small helper function in the ViewHelper:

/**
 * Resolves the table name and uid for the record the rendering is based upon.
 * Falls back to current page if none is available
 *
 * @param TypoScriptFrontendController $frontendController
 * @param string $currentRecord
 * @return string[] table name as first and uid as second index of the array
 */
protected function resolveTableNameAndUidFromContextString(TypoScriptFrontendController $frontendController, string $currentRecord): array
{
    $tableNameAndUid = explode(':', $currentRecord);
    if (count($tableNameAndUid) !== 2 || empty($tableNameAndUid[0]) || empty($tableNameAndUid[1]) || !MathUtility::canBeInterpretedAsInteger($tableNameAndUid[1])) {
        $tableNameAndUid = ['pages', $frontendController->id];
    }

    return $tableNameAndUid;
}

Best regards Kay

Translated with www.DeepL.com/Translator (free version)

t3touch commented 1 year ago

Will this adjustment eventually make its way into the regular version as well?

NamelessCoder commented 11 months ago

FYI: The ViewHelper does work in a standard content rendering context, but your speciifc context is rendering content with a delegated TS RECORDS object which does not fill the ContentObjectRenderer with record data. I added the additional lookup.