diff --git a/Classes/PackageFactory/AtomicFusion/FusionObjects/AugmenterImplementation.php b/Classes/PackageFactory/AtomicFusion/FusionObjects/AugmenterImplementation.php new file mode 100644 index 0000000..69e14cf --- /dev/null +++ b/Classes/PackageFactory/AtomicFusion/FusionObjects/AugmenterImplementation.php @@ -0,0 +1,66 @@ + + * Martin Ficzel + * + * This package is Open Source Software. For the full copyright and license + * information, please view the LICENSE file which was distributed with this + * source code. + */ + +use Neos\Flow\Annotations as Flow; +use Neos\Fusion\FusionObjects\ArrayImplementation; +use Neos\Neos\Service\HtmlAugmenter; + +/** + * A Fusion Augmenter-Object + * + * The fusion object can be used to add html-attributes to the rendererd content + * + * @api + */ +class AugmenterImplementation extends ArrayImplementation +{ + + /** + * @var HtmlAugmenter + * @Flow\Inject + */ + protected $htmlAugmenter; + + /** + * Properties that are ignored + * + * @var array + */ + protected $ignoreProperties = ['__meta', 'fallbackTagName', 'content']; + + /** + * @return void|string + */ + public function evaluate() + { + $content = $this->fusionValue('content'); + $fallbackTagName = $this->fusionValue('fallbackTagName'); + + $sortedChildFusionKeys = $this->sortNestedFusionKeys(); + + $attributes = []; + foreach ($sortedChildFusionKeys as $key) { + if ($fusionValue = $this->fusionValue($key)) { + $attributes[$key] = $fusionValue; + } + } + + if ($attributes && is_array($attributes) && count($attributes) > 0) { + return $this->htmlAugmenter->addAttributes($content, $attributes, $fallbackTagName); + } else { + return $content; + } + } +} diff --git a/README.md b/README.md index 1d8447a..d7c9cc4 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ and afterwards evaluetes the `renderer` - `PackageFactory.AtomicFusion:ClassNames`: create conditional class-names from fusion-keys - `PackageFactory.AtomicFusion:Editable`: create and editable tag for a property - `PackageFactory.AtomicFusion:Content`: component base-prototype for inline editable content nodes +- `PackageFactory.AtomicFusion:Augmenter`: add html-attributes to the rendered children ## Usage @@ -93,6 +94,52 @@ prototype(Vendor.Site:ExampleContent) < prototype(PackageFactory.AtomicFusion:Co } ``` +### 3. Content Augmentation + +The Augmenter-component can be used as processor or as a standalone prototype + +``` +# +# Standalone-Augmenter +# +augmentedContent = PackageFactory.AtomicFusion:Augmenter { + + # + # The content that shall be augmented. + # + content = Neos.Fusion:Tag { + tagName = 'h2' + content = 'Lorem Ipsum' + } + + # + # If more than one tag is found the content is wrapped in the + # fallback-Tag before augmentation wich has "div" as default + # + fallbackTagName = 'div' + + # + # All other fusion properties are added to the html-content + # as html-attributes. + # + class="foo" + data-example="data" + +} + +# +# Processor-Augmenter +# +augmentedContent = Neos.Fusion:Tag { + tagName = 'h2' + content = 'Lorem Ipsum' + @process.augment = PackageFactory.AtomicFusion:Augmenter { + class = "foo" + data-example="data" + } +} +``` + ## Installation PackageFactory.AtomicFusion is available via packagist. Just run `composer require packagefactory/atomicfusion`. diff --git a/Resources/Private/Fusion/Prototypes/Augmenter.fusion b/Resources/Private/Fusion/Prototypes/Augmenter.fusion new file mode 100644 index 0000000..15e5c94 --- /dev/null +++ b/Resources/Private/Fusion/Prototypes/Augmenter.fusion @@ -0,0 +1,6 @@ +prototype(PackageFactory.AtomicFusion:Augmenter) < prototype(PackageFactory.AtomicFusion:Component) { + @class = 'PackageFactory\\AtomicFusion\\FusionObjects\\AugmenterImplementation' + + fallbackTagName = "div" + content = ${value} +}