Skip to content

Commit

Permalink
EZP-28110: As a developer, I want to define attributes for custom tags (
Browse files Browse the repository at this point in the history
#2219)

* Deprecated old SiteAccess-aware RichText custom tags configuration

* [CoreBundle] Defined SA-aware RichText Custom Tags list configuration

* [MVC] Injected RichText Custom Tags configuration into RichText Renderer

* Injected new configuration
* Used configured template to render site template for Custom Tag
* Kept BC layer if new configuration is not available

* [CoreBundle] Defined Semantic Configuration for RichText CustomTags

* [Tests] Created tests for RichText Custom Tags

* [RichText] Created Custom Tags user input validator

* [CoreBundle] Disallowed merging list of choices for Custom Tag attribute
  • Loading branch information
alongosz authored and Łukasz Serwatka committed Feb 20, 2018
1 parent cf5cfde commit 77d9d35
Show file tree
Hide file tree
Showing 21 changed files with 1,003 additions and 89 deletions.
104 changes: 104 additions & 0 deletions eZ/Bundle/EzPublishCoreBundle/DependencyInjection/Configuration.php
Expand Up @@ -11,11 +11,14 @@
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface;
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Configuration as SiteAccessConfiguration;
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Suggestion\Collector\SuggestionCollectorInterface;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

class Configuration extends SiteAccessConfiguration
{
const CUSTOM_TAG_ATTRIBUTE_TYPES = ['number', 'string', 'boolean', 'choice'];

/**
* @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface
*/
Expand All @@ -25,6 +28,7 @@ class Configuration extends SiteAccessConfiguration
* @var Configuration\Suggestion\Collector\SuggestionCollectorInterface
*/
private $suggestionCollector;

/**
* @var \eZ\Bundle\EzPublishCoreBundle\SiteAccess\SiteAccessConfigurationFilter[]
*/
Expand Down Expand Up @@ -57,6 +61,7 @@ public function getConfigTreeBuilder()
$this->addHttpCacheSection($rootNode);
$this->addPageSection($rootNode);
$this->addRouterSection($rootNode);
$this->addRichTextSection($rootNode);

// Delegate SiteAccess config to configuration parsers
$this->mainConfigParser->addSemanticConfig($this->generateScopeBaseNode($rootNode));
Expand Down Expand Up @@ -464,4 +469,103 @@ private function addRouterSection(ArrayNodeDefinition $rootNode)
->end()
->end();
}

/**
* Define global Semantic Configuration for RichText.
*
* @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $rootNode
*/
private function addRichTextSection(ArrayNodeDefinition $rootNode)
{
$this->addCustomTagsSection(
$rootNode->children()->arrayNode('ezrichtext')->children()
)->end()->end()->end();
}

/**
* Define RichText Custom Tags Semantic Configuration.
*
* The configuration is available at:
* <code>
* ezpublish:
* ezrichtext:
* custom_tags:
* </code>
*
* @param \Symfony\Component\Config\Definition\Builder\NodeBuilder $ezRichTextNode
*
* @return \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition
*/
private function addCustomTagsSection(NodeBuilder $ezRichTextNode)
{
return $ezRichTextNode
->arrayNode('custom_tags')
// workaround: take into account Custom Tag names when merging configs
->useAttributeAsKey('tag')
->arrayPrototype()
->children()
->scalarNode('template')
->isRequired()
->end()
->scalarNode('icon')
->defaultNull()
->end()
->arrayNode('attributes')
->useAttributeAsKey('attribute')
->isRequired()
->arrayPrototype()
->beforeNormalization()
->always(
function ($v) {
// Workaround: set empty value to be able to unset it later on (see validation for "choices")
if (!isset($v['choices'])) {
$v['choices'] = [];
}

return $v;
}
)
->end()
->validate()
->ifTrue(
function ($v) {
return $v['type'] === 'choice' && !empty($v['required']) && empty($v['choices']);
}
)
->thenInvalid('List of choices for required choice type attribute has to be non-empty')
->end()
->validate()
->ifTrue(
function ($v) {
return !empty($v['choices']) && $v['type'] !== 'choice';
}
)
->thenInvalid('List of choices is supported by choices type only.')
->end()
->children()
->enumNode('type')
->isRequired()
->values(static::CUSTOM_TAG_ATTRIBUTE_TYPES)
->end()
->booleanNode('required')
->defaultFalse()
->end()
->scalarNode('default_value')
->defaultNull()
->end()
->arrayNode('choices')
->scalarPrototype()->end()
->performNoDeepMerging()
->validate()
->ifEmpty()->thenUnset()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
;
}
}

0 comments on commit 77d9d35

Please sign in to comment.