0

I use the easyAdminBundle, I would like to know if it's possible to use a static function or const (define anywhere in my app) to set the choices option of a choice type as such :

- { property: tag, type: choice, type_options: { choices: 'App\Entity\News::getTags' }

With a getTags function like :

class News 
{
    const TAGS = ['toto','tutu'];

    static public function getTags()
    {
        return $this::TAGS;
    }
}

It is already possible to do that with query_builder but I didn't find any trace of it in the documentation.

Actually I get the following error which lead me to think it's not possible (but maybe someone here do):

An error has occurred resolving the options of the form "Symfony\Component\Form\Extension\Core\Type\ChoiceType": The option "choices" with value "App\Entity\News::getTags" is expected to be of type "null" or "array" or "\Traversable", but is of type "string".

Pierrick Rambaud
  • 1,726
  • 1
  • 20
  • 47

2 Answers2

0

Yes, set const TAGS to static and inside of getTags(), replace $this by self.

AND

{ choices: App\Entity\News::getTags() }

Remove the apostrophes and add () to actually call the function

David Alvarez
  • 1,226
  • 11
  • 23
  • according to [this](https://stackoverflow.com/questions/3389380/can-you-use-static-constants-in-php) (and my compiler) you cannot set `static const` in php. anyways that's the reason of the `static function`. I replaced the `$this` with `self` as suggested and the `()`to the call in the yaml file. I still got the same error. As if it was not interpreted as a function.... – Pierrick Rambaud Feb 23 '20 at 08:10
  • I’m not 100% of that but if you use an attribute which is not static you have to create an instance of News to access it. But here you access News statically – David Alvarez Feb 23 '20 at 08:29
  • `const`are accecible statically. but just to make sure I've tried with a `static private $TAGS` and I still get the same error – Pierrick Rambaud Feb 23 '20 at 08:31
  • 1
    Try to directly access the constant instead of using a function : https://stackoverflow.com/questions/1685922/php-5-const-vs-static#1685933 – David Alvarez Feb 23 '20 at 08:32
  • I am almost sure that the error telle you that you give a string because you put the apostrophes, which is a string. What error do you have when removing apostrophes ? – David Alvarez Feb 23 '20 at 08:36
  • 1
    the same, but following your advice I'v been to the [symfony website](https://symfony.com/blog/new-in-symfony-3-2-php-constants-in-yaml-files) and find a way to access directly the constants from yaml. works like a charm. I'm posting an answer – Pierrick Rambaud Feb 23 '20 at 08:48
0

Following the advice of @David Alvarez, I've tried to access the const property directly from the yaml file. This is possible thanks to this symfony update available since the 3.2 version.

so to access a const property as :

class News 
{
    const TAGS = ['toto' => 'toto','tutu' => 'tutu'];
}

I'll write :

{ choices: !php/const App\Entity\News::TAGS }

and it works like a charm

Pierrick Rambaud
  • 1,726
  • 1
  • 20
  • 47