Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to display dropdown values with twig #3759

Closed
ghost opened this issue Sep 5, 2018 · 4 comments
Closed

Add a way to display dropdown values with twig #3759

ghost opened this issue Sep 5, 2018 · 4 comments

Comments

@ghost
Copy link

ghost commented Sep 5, 2018

Expected behavior

When I create some dropdown in the backend, I want to be able to easily display the key and / or the value of that dropdown in my templates, without writing PHP as it should be an easy and common task.

Actual behavior

Actually, you can only display the key by using {{ model.my_dropdown }}, there is no way (directly with twig) to display the dropdown value (which is a very basic task normally). I suggest to add a twig filter in order to deal with dropdown the way we want. For example :

{{ model.my_dropdown|key }} or {{ model.my_dropdown|value }}

Reproduce steps
  • Create a new model (use the builder plugin for quick and easy model creation)
  • Add a dropdown to that model (not a relation, a simple dropdown)
  • in yout template, try to print {{ model.dropdown }}, you will see that the printed element is the dropdown key.
October build

All

@w20k
Copy link
Contributor

w20k commented Sep 5, 2018

Hi @Alex360hd ,

You could create your own plugin for Twig dropdown support, like this one from @LukeTowers -> https://github.com/LukeTowers/oc-twigpcre-plugin. And add to the market, so that everyone could use it or help you anyhow. Or provide a PR to support this feature.

@LukeTowers
Copy link
Contributor

@Alex360hd that's not really the responsibility of the frontend to know what you have labelled that key as in the backend. I'd agree with @w20k, implement it yourself as a separate plugin.

@ghost
Copy link
Author

ghost commented Sep 6, 2018

Hello,

It sound strange to me that a formWidget provided by core (dropdown) could not be displayed in frontend with ease and without external plugins, so I would prefer a PR solution directly to the core in order to support that.

But if you still think it's not the job of the core, there is the excellent Twig extension plugin that I install on every project. Could be a nice addition to that plugin cause I don't want to create a plugin just to add a twig filter (Traumatized about Drupal and dozens of needed module in order to make it just usable).

Thank you for the answer.

Alex

Edit : for people that want to follow this, I created an issue on the twigextensions plugin. It's the best place to add the feature for me.

@LukeTowers
Copy link
Contributor

@Alex360hd it's not the job of the formwidget to display anything on the frontend, it's simply a tool of convenience for the backend users. Adding something in the core to do that would (in my opinion) be far too opinionated about how these things are used and setup. Remember, there are a lot of different ways that the options for a dropdown can be defined which means a lot of different places that the proposed solution would have to look for the desired strings that the option keys represented.

You could do a simplistic version of what you want with the following:

{{ mymodel.getMyDropdownOptions()[mymodel.mydropdown] }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants