Created
October 24, 2023 10:58
-
-
Save sorenmalling/f0c2b95c51abaae895e182c2ef4e7a30 to your computer and use it in GitHub Desktop.
TYPO3 TAG ContentObject
This file contains hidden or 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
services: | |
_defaults: | |
autowire: true | |
autoconfigure: true | |
public: false | |
TagContentObject\: | |
resource: '../Classes/*' | |
TagContentObject\ContentObject\TagContentObject: | |
tags: | |
- name: frontend.contentobject | |
identifier: 'TAG' |
This file contains hidden or 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
5 = TAG | |
5 { | |
tagName = h1 | |
attributes { | |
class { | |
10 = TEXT | |
10.value = first | |
20 = TEXT | |
20.value = second | |
} | |
data-stuff = two | |
} | |
content = TEXT | |
content.data = page:title | |
} |
This file contains hidden or 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
<?php | |
namespace TagContentObject\ContentObject; | |
use TYPO3\CMS\Core\Context\TypoScriptAspect; | |
use TYPO3\CMS\Core\TypoScript\TypoScriptService; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
use TYPO3\CMS\Frontend\ContentObject\AbstractContentObject; | |
/** | |
* Contains TAG class object. | |
*/ | |
class TagContentObject extends AbstractContentObject | |
{ | |
protected static $SELF_CLOSING_TAGS = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr']; | |
/** | |
* Rendering the cObject, TAG | |
* | |
* @param array $conf Array of TypoScript properties | |
* @return string Output | |
*/ | |
public function render($conf = []) | |
{ | |
if (!is_array($conf)) { | |
return ''; | |
} | |
$typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class); | |
$configuration = $typoScriptService->convertTypoScriptArrayToPlainArray($conf); | |
$tagName = $configuration['tagName'] ?? 'div'; | |
$tagAttributes = []; | |
$tagContent = ''; | |
$omitClosingTag = $configuration['omitClosingTag'] ?? false; | |
$selfClosingTag = in_array($tagName, self::$SELF_CLOSING_TAGS, true) || (boolean) $configuration['selfClosingTag'] ?? false; | |
$attributes = $configuration['attributes'] ?? []; | |
foreach ($attributes as $attributeName => $attributeNameValue) { | |
if (is_string($attributeNameValue)) { | |
$tagAttributes[htmlspecialchars((string) $attributeName, ENT_COMPAT, 'UTF-8', false)][] = htmlspecialchars((string) $attributeNameValue, ENT_COMPAT, 'UTF-8', false); | |
continue; | |
} elseif (is_array($attributeNameValue)) { | |
foreach ($attributeNameValue as $attributeNameValueConfiguration) { | |
$tagAttributes[htmlspecialchars((string) $attributeName, ENT_COMPAT, 'UTF-8', false)][] = htmlspecialchars((string) $this->cObj->cObjGetSingle($attributeNameValueConfiguration['_typoScriptNodeValue'], $attributeNameValueConfiguration, ENT_COMPAT, 'UTF-8', false)); | |
} | |
} | |
} | |
if ($configuration['content'] ?? false) { | |
$tagContent = htmlspecialchars((string)$this->cObj->cObjGetSingle($configuration['content']['_typoScriptNodeValue'], $configuration['content'] ?? []), ENT_COMPAT, 'UTF-8', false); | |
} | |
return '<' . $tagName . self::renderAttributes($tagAttributes) . ($selfClosingTag ? ' /' : '') . '>' . (!$omitClosingTag && !$selfClosingTag ? $tagContent . '</' . $tagName . '>' : ''); | |
} | |
private static function renderAttributes(array $attributes): string | |
{ | |
$result = ''; | |
foreach ($attributes as $key => $values) { | |
$result .= ' ' . $key . '="' . implode(' ', $values) . '"'; | |
} | |
return rtrim($result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment