eZ Platform Discussions

Inject parameters in custom tags


#1

Hello to all.

I have a simple problem for once :slight_smile:

I have parameter in my conf file:
src/MyBundle/Resources/config/parameters.yml

parameters:
    my.revive.id: "ebc28dfb024fb08a0d7228ee6ebd0d79"

I need it in a custom tag:

ezpublish:
  ezrichtext:
    custom_tags:
        advertising:
            template: 'MyBundle:fields/ezrichtext/custom_tags:advertising.html.twig'
            icon: '/bundles/my/images/custom_tags/advertising.svg'
            attributes:
                title:
                    type: 'string'
                    required: true
                zone_id:
                    type: 'number'
                    required: true

So of course, I could do that: https://symfony.com/doc/current/templating/global_variables.html
But I only need this variable in one of my templates.

I try by adding a controller to my custom tag

ezpublish:
  ezrichtext:
    custom_tags:
        advertising:
            template: 'MyBundle:fields/ezrichtext/custom_tags:advertising.html.twig'
            controller: MyBundle:CustomTag:advertising
            icon: '/bundles/my/images/custom_tags/advertising.svg'
            attributes:

InvalidConfigurationException

Unrecognized option “controller” under “ezpublish.ezrichtext.custom_tags.advertising”

But either I made a typo (which happens to me often) or I’m too avant-garde…

For the moment I’m getting by with a twig function.
But I don’t think it has the right solution.

{{ revive_id() }}
            new \Twig_SimpleFunction('revive_id', [$this, 'revive_id']),
    public function revive_id()
    {
        return $this->container->getParameter('my.revive.id');
    }

Merci pour votre aide.


#2

Hello @remy_php

When designing this feature, we didn’t want to overcomplicate its implementation, so custom controllers for Custom Tags are not supported directly.

However you could always embed controller into your template, see Symfony doc on that. The last code block is all you need, assuming you already have your controller :slight_smile:


#3

Have you thought of using an attribute to pass your parameter ? It may be an issue that it is visible / editable, but maybe we can think of a “hidden” parameter type ?

ezpublish:
  ezrichtext:
    custom_tags:
        advertising:
            template: 'MyBundle:fields/ezrichtext/custom_tags:advertising.html.twig'
            icon: '/bundles/my/images/custom_tags/advertising.svg'
            attributes:
                title:
                    type: 'string'
                    required: true
                zone_id:
                    type: 'number'
                    required: true
                revive_id:
                  type: string
                  default_value: %my.revive.id%

#4

Yes. I’ve been thinking about it.

But this will be a problem if the id changes because you will have to go back to all the custom_tags inserted in the rich texts.