Problem/Motivation

In 7.x, entity view modes were defined in code and in a hook, so they were rebuild after every cache clear. Token adds a view mode for every entity type that has fields so that it can be configured how they look when displayed through a token.

In 8.x, that was ported to token_install() where it creates config entities, but that has a number of flaws:
- It only works for entity types that already exist at the moment token is installed
- It does not yet consider configuration syncing, so if token is installed as part of a configuration deployment or config installer, it will lead to errors due to creating those config entities twice
- It creates view modes for a few entities that have view builders and can be viewed, but aren't actually content/fieldable entities like blocks.

Proposed resolution

All those issues could be solved with more code. There's a new entity type hook that modules can react to, a way to check for config sync, we could exclude non-content entities, ..

But my personal suggestion is to simply drop this code and instead document (README.txt and/or hook_help()) that this needs to be created by the user. Drupal 8 provides a UI for this by default, no additional modules are needed. The API when viewing a field in a given view mode does not care of the view mode or view display exists anyway (In fact, it doesn't even *know* about view modes, only view displays).

Remaining tasks

User interface changes

API changes

Comments

LKS90’s picture

Status: Active » Needs review
StatusFileSize
new1.6 KB

Removed token_install(), documented the removal and mentioned alternate ways to set up view modes for tokens.

mglaman’s picture

+1 to this, does the job.

s_leu’s picture

Status: Needs review » Needs work

Little bit more details about how to setup the token view modes would be nice here. It's not easy to figure it out for someone that never configured/added a view mode in D8. Links/paths in the readme would definitely help here.

Besides this i don't like the hardcoded view mode token in the code. It should at least be possible to override that view mode using a config variable.

darol100’s picture

@s_leu,

Can you provide a patch base on what the changes that you request ? Or add more information about what this patch should have.

juampynr’s picture

How are token view modes used? Can anyone provide a use case? Then I could help on this.

berdir’s picture

The token view mode/display allows you to configure the formatter + settings that is used when you use a field token. That allows you to display them differently than on the default view display/output. E.g, you might hide some taxonomy term references, but if you do that, then it also won't generate a token by default.

Before finishing this, we should figure out #2603652: Fix token default formatter usage, this might impact what we end up doing here.

hussainweb’s picture

Now that #2603652: Fix token default formatter usage is decided, how should we proceed here? Should we still remove this from token_install? I was trying to find a hook that would act on all entities but could not find anything except hook_entity_type_alter, which I think might be too heavy. I couldn't find any hook for entity types being added or modified.

berdir’s picture

There is an event that is triggered when entity types are added. We *could* listen to that. But I think that's it's not worth the trouble, especially now that we fixed #2603652: Fix token default formatter usage.

The current docs are not sufficient yet, but if we explain that in a bit more detail in a chapter called "Customize field token output" or so, then I think we can save us a fair amount of trouble and all those view modes that will, in many cases, never be used.

hussainweb’s picture

Component: Code » Documentation

I agree. Before I started working on this port, I never knew about the view mode. :)

I think this is mostly a documentation issue then, right?

jeffm2001’s picture

StatusFileSize
new1.61 KB

Re-rolled the patch in #1 against the latest dev.

berdir’s picture

Thanks.

Lets include something about this on the help page, because users will likely not look at the README.txt but only at the help page.

Title "Configure output of [entity:field_example] tokens"

And then explain that the output uses the field formatter configuration of the token view display if existing and otherwise the default view display. Then provide an example for configuring the tags field for the article node type. that would be going to the view mode UI first and creating the view mode. Then going to the article view display settings, enable the token view mode and then configuring it there.

chucksimply’s picture

Hey all... I've read this thread, but am still confused here.

I installed the Media module after Token was installed. I created a Media Type, and per my interpretation of the instructions above, created a new custom View Mode for Media called Token.

Unfortunately, formatters for fields in this new Token View Mode aren't respected in the token field's output. Additionally, even Default View Mode formatters aren't respected.

Am I missing something? Thanks!

berdir’s picture

What tokens exactly are we talking about?

chucksimply’s picture

Specifically, it's a decimal field. The token is like this [media:field_decimal_example]. The output result is just the "default" formatter.

berdir’s picture

Yeah, that should work. You're certain that the machine name is just "token", not with some prefix or something, and that you enabled and configured the display then?

Note that it doesn't fall back to default, it only falls back to the default formatter settings of that field type, see field_tokens(), you might need to debug through that.

chucksimply’s picture

I stand corrected, after changing a display formatter in the Media Token view mode and saving... the token output displayed as it should. Maybe changing it cleared some default stuck value? Anyway, all works as you mention above. Thanks again @Berdir for the quick reply.

manuel.adan’s picture

Component: Documentation » Code
Status: Needs work » Needs review
StatusFileSize
new4.1 KB

Re-roll of #10. In addition to it, I added some optional view mode configurations for the core most common content entity types that will be added to the active config regardless of the modules installation order. It would also guide users on how to setup the token view modes.

thalles’s picture

What is the address of this configuration page?