Last active
November 22, 2022 10:18
-
-
Save fsuter/5803411 to your computer and use it in GitHub Desktop.
The code samples below demonstrate how to register a custom soft reference parser for TYPO3 CMS. This consists of 3 steps: 1) register the soft reference parser key globally
2) add the soft reference parser key to the appropriate fields in the TCA
3) code the soft reference parser The example is taken from extension "templatedisplay".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['softRefParser'][$_EXTKEY] = 'Tesseract\Templatedisplay\Service\SoftReferenceParser'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'template' => array( | |
'exclude' => 0, | |
'label' => 'LLL:EXT:templatedisplay/Resources/Private/Language/locallang_db.xml:tx_templatedisplay_displays.template', | |
'config' => array( | |
'type' => 'text', | |
'cols' => '40', | |
'rows' => '4', | |
'softref' => 'templatedisplay' | |
) | |
), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SoftReferenceParser implements \TYPO3\CMS\Core\SingletonInterface { | |
/** | |
* Parses the template field for a "file:xxx" pattern. If found it will try to match it to a sys_file | |
* entry and return the relevant reference. | |
* | |
* @param string $table Database table name | |
* @param string $field Field name for which processing occurs | |
* @param integer $uid UID of the record | |
* @param string $content The content/value of the field | |
* @param string $spKey The softlink parser key. In this case, will always be "templatedisplay". | |
* @param array $parameters Parameters of the softlink parser (none, in this case). | |
* @param string $structurePath If running from inside a FlexForm structure, this is the path of the tag. | |
* @return array Result array on positive matches, see description in \TYPO3\CMS\Core\Database\SoftReferenceIndex. Otherwise FALSE. | |
* @see \TYPO3\CMS\Core\Database\SoftReferenceIndex | |
*/ | |
public function findRef($table, $field, $uid, $content, $spKey, $parameters, $structurePath = '') { | |
$elements = array(); | |
try { | |
$elements[] = $this->parseForSysFile($content); | |
} | |
catch (\Exception $e) { | |
// Nothing to do | |
} | |
// If at least one reference was found, return the list of references | |
// Otherwise return false | |
if (count($elements) > 0) { | |
return array( | |
'content' => $content, | |
'elements' => $elements | |
); | |
} else { | |
return FALSE; | |
} | |
} | |
/** | |
* Searches the given content for a pattern like "file:xxx" to define a reference based on it. | |
* | |
* @param string $content The content to parse | |
* @return array Reference to the found element | |
* @throws \Exception | |
*/ | |
protected function parseForSysFile($content) { | |
// If the content starts with "FILE:" (or "file:"), we may have a file reference | |
if (stripos($content, 'FILE:') === 0) { | |
// Remove the "FILE:" key and cast the rest to int | |
$sysFileId = intval(str_ireplace('FILE:', '' , $content)); | |
// If it's a positive number, prepare information for registering a reference | |
if ($sysFileId > 0) { | |
return array( | |
'matchString' => $content, | |
'subst' => array( | |
'type' => 'db', | |
'recordRef' => 'sys_file:' . $sysFileId | |
) | |
); | |
} | |
} | |
throw new \Exception('No system file reference found', 1371471101); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment