Skip to content

Instantly share code, notes, and snippets.

@sorenmalling
Created October 26, 2024 12:00
Show Gist options
  • Save sorenmalling/464b9bc2b7a65376c53982d5fb22f6a6 to your computer and use it in GitHub Desktop.
Save sorenmalling/464b9bc2b7a65376c53982d5fb22f6a6 to your computer and use it in GitHub Desktop.
services:
_defaults:
autowire: true
autoconfigure: true
public: false
BasisWeb\TagContentObject\:
resource: '../Classes/*'
BasisWeb\TagContentObject\ContentObject\TagContentObject:
tags:
- name: frontend.contentobject
identifier: 'TAG'
<?php
/*
* 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!
*/
namespace BasisWeb\TagContentObject\ContentObject;
use TYPO3\CMS\Core\Context\TypoScriptAspect;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
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 (!empty($conf['if.']) && !$this->cObj->checkIf($conf['if.'])) {
return '';
}
$tagName = $conf['tagName'] ?? 'div';
if ($conf['tagName.'] ?? false) {
$this->cObj->cObjGetSingle($tagName, $conf['tagName.'] ?? []);
}
/*
$tagName = (($conf['tagName'] ?? false) && ($conf['tagName.'] ?? false)) ?
$this->cObj->cObjGetSingle($conf['tagName'], $conf['tagName.'] ?? []) :
'div';
*/
$tagAttributes = [];
$tagContent = '';
$omitClosingTag = $conf['omitClosingTag'] ?? false;
$selfClosingTag = in_array($tagName, self::$SELF_CLOSING_TAGS, true) || (boolean) ($conf['selfClosingTag'] ?? false);
$attributes = $conf['attributes.'] ?? [];
foreach ($attributes as $attributeName => $attributeValue) {
if (str_ends_with($attributeName, '.')) {
continue;
}
$tagAttributes[htmlspecialchars((string) $attributeName, ENT_COMPAT, 'UTF-8', false)][] = htmlspecialchars(
(string) $this->cObj->cObjGetSingle(
$conf['attributes.'][$attributeName],
$conf['attributes.'][$attributeName . '.'] ?? []
),
ENT_COMPAT, 'UTF-8', false
);
}
$tagContent = (($conf['content'] ?? false) && ($conf['content.'] ?? false)) ?
$this->cObj->cObjGetSingle($conf['content'], $conf['content.'] ?? []) :
$conf['content'] ?? '';
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