1
votes

I am trying to make a twig extension which will be using one of services of mine.

The error I got is:

Uncaught exception 'Symfony\Component\DependencyInjection\Exception\InvalidArgumentException' with message 'A "tags" entry must be an array for service "tagformater_extension"

My services.yml:

nobookchoosen_service:
    class: AppBundle\Service\nobookchoosenService
    arguments:
        - @request_stack
        - @router

logger_service:
    class: AppBundle\Service\loggerService
    arguments: ["@doctrine.orm.entity_manager", "@security.context"]

tags_service:
    class:  AppBundle\Service\tagsService
    arguments: [%tags%]

documents_service:
    class:  AppBundle\Service\documentsService
    arguments: ["@tags_service"]

tagformater_extension:
    class: AppBundle\Twig\tagformaterExtension
    arguments: ["@tags_service"]
    public: false
    tags:
        { name: twig.extension }

And the extension file:

<?php
namespace AppBundle\Twig;

class tagformaterExtension extends \Twig_Extension
{

    protected $tagsService;

    public function __construct($tagsService)
    {
        $this->tagsService = $tagsService
    }

    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('tag', array($this, 'tagFilter')),
        );
    }

    public function tagFilter($tagname)
    {
        $label = "label label-default";
        if(array_key_exists($tagname, $this->tagsConfigArray))
        {
            if($this->tagsConfigArray[$tag]['label']) $label = $this->tagsService->gettaglabel($tagname);
        }

        return "<SPAN CLASS=\"".$label."\">".$tagname."</A>";
    }

    public function getName()
    {
        return 'tagformater_extension';
    }
}

Please advice what am I doing wrong.

1

1 Answers

4
votes

tags must be an array. just put a - before { name: ...}

tagformater_extension:
    class: AppBundle\Twig\tagformaterExtension
    arguments: ["@tags_service"]
    public: false
    tags:
        - { name: twig.extension }