Support not just values in custom template

Created on 22 September 2024, 3 months ago

Problem/Motivation

Default, Inline, Table, HTML and List template fully renders the subfields, i.e. their label is shown (and not the value), the referenced entity is shown with a link to it (and not just the referenced entity ID) and so on.
When we choose "Custom template", there is a free HTML (wonderful option) but replacements are done only on the values of the subfields.

Steps to reproduce

Create a custom field with a subfield A as a select list (value/label pairs) and another field B as an entity reference.
In manage display, choose one of the first four ones templates: A is shown with its label and B is shown with the title/label of the referenced item.
Now in Manage Display choose the Custom Template and use the replacement tokens to display A and B: only the value of A can be shown (not its label), only the entity ID referenced in B can be shown (not its label and link).

Proposed resolution

Add another token to fully show the subfield values.
THANKS!

User interface changes

N/A

API changes

N/A

Data model changes

Feature request
Status

Active

Version

3.0

Component

Code

Created by

🇮🇹Italy giuse69

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @giuse69
  • 🇺🇸United States apmsooner

    I have a working solution for images by adding some additional tokens. It might be overkill but I wanted to support image styles if we're gonna do this. I think the entity solution will be similar but tokens but by view modes. What do you think of the screenshots depicting how this would work?

  • 🇮🇹Italy giuse69

    Hi, yes, the approach for images adding new tokens seems very good to me. Besedes references, also select list should be added to display labels isof values.
    thanks!

  • 🇺🇸United States apmsooner

    Thanks for the feedback. For better UX, i'll put the tokens into collapsible fieldsets for each subfield. I should have a patch ready to review in the next few days or so hopefully.

  • 🇺🇸United States chucksimply

    Would this patch apply to other entities as well? Taxonomy, Node, etc?

    Looking forward to this feature!

  • 🇺🇸United States apmsooner

    I got a little sidetracked with day job but I expect to get back into this soon. @chucksimply, the plan would be to provide support for all field types and reference types. I left off with researching whether the token module is viable option for greatest flexibility however it may be overkill for basic needs and for example entity rendering tokens could simply be just the label and available view modes. Trying to think about this in simplest way to not overcomplicate but also provide just enough for common needs.

  • 🇺🇸United States apmsooner

    I'm close to getting this ready for patch review. I just need to finish up some of the logic but the solution for this is going to be optional advanced tokens support via the token module which will provide you full entity token access as well as global tokens. Here's a new screenshot for how this is going to look. I hope those of you following this are still interested in testing out when ready.

  • 🇮🇹Italy giuse69

    yes, I am available to test when ready

  • 🇺🇸United States chucksimply

    Yup, ready as well.

  • Merge request !73Resolve #3476033 "Enhance token support" → (Merged) created by apmsooner
  • 🇺🇸United States apmsooner

    Alright feel free to give it a go! Some things to know:

    • In order to preserve basic tokens that are already in place, i added an option to choose between: Basic OR Advanced (via token module)
    • If choosing Advanced, which you will want... you will either see token browser link provided by token module or message alerting you that token module is required
    • The token browser in this module is a custom form element which provides some settings (recursion_limit, global_types) that update the link params via ajax. This is to balance performance with being able to drill down the tree to the tokens you need. All tokens are available from the entity however the custom_field tokens are those specific to the field you have setup.
    • You should be able to build just about anything you can imagine from the given values but there are a few subfields that are still just serving raw values and could benefit from some additional fluff.
    • I tested this in views also and it worked just fine but feel free to try.
  • 🇮🇹Italy giuse69

    it seems to work great when the custom field is belonging to a node, it seems not to work (no token avilable) when the custom field is belonging to a taxonomy term, maybe it needs to be extended to taxonomy?

  • 🇺🇸United States apmsooner

    Let me test that. I think its just the naming conventions are different with taxonomy terms for the token types.

  • 🇺🇸United States apmsooner

    That commit will fix it for you. Just pull down the latest.

  • 🇮🇹Italy giuse69

    With the new fix, it works for taxonomy in entity display, but I still don't see the formatters menu choice to appear in views when picking a subfield as field in the view

  • 🇺🇸United States apmsooner

    @giuse69, The views subfield issue is unrelated and not going to be addressed in this issue. While I'm not saying its a valid request to support views subfield formatters, I don't really see a huge need to even render the subfields in views on their own. I guess I don't quite understand what you are ultimately trying to accomplish but maybe theres a better way of going about it.

  • 🇮🇹Italy giuse69

    @apmsooner: you are right, the views subfield issue is not important and I can use the overall custom field, but I can't make the overall custom field work in a specific scenario: a view on taxonomy terms where the custom field is multivalued and each instance includes a text or integer subfield AND an entity reference subfield referring to taxonomy terms as well. The view uses a relationships to connect terms of one vocabulary to terms of another vocabulary.
    If I pick up individually the two subfields to be shown in the view, their raw values are correctly shown (and no choice of formatters but no problem as you say).
    If I pick up the overall custom field, nothing is shown, even with default formatter. If I set the custom field to use the relationship, all related terms are shown (too many records as the join wouldn't be working correctly).
    I made this test several times and on a couple of views of this type with same results.
    Anything I can further test or you can check this particular situation?
    thanks!

  • 🇺🇸United States apmsooner

    @giuse69, Yeah i can't really visualize what you're trying to do so I won't recommend anything for you other than maybe try using a view of entities in a view mode where you have full control of the field output perhaps. It sounds like an edge case though that I don't think is common for other users and I'd prefer to really focus on prioritizing in other areas for now.

  • 🇮🇹Italy giuse69

    Hi @apmsooner, I made a new and simple test with a clean scenario to describe how you can reproduce the issue. I don't think it's an edge case.

    2 Vocabularies
    VOC1: country
    VOC2: organization with a custom field "items" to include countries belonging to the organization, subfields are:
    -) "item" = entity reference to taxonomy term - country
    -) "type= a string for type of participation to the organization

    View
    A simple view on taxonomy term country to show how many orgs the country belongs to
    -) Contextual filter: Taxonomy term: Term ID
    -) Relationship: field items of organization (Taxonomy term using field_organization_items_item)
    Fields to be shown:
    -) Name (of the organization) using the relationship
    -) Subfield of the custom field items:item
    -) Subfield of the custom field items:type

    This works. BUT if I use the overall custom field Items (default or any formatter) instead of the two subfields, nothing is shown.
    Could you please look at this?
    thanks a lot!

  • 🇺🇸United States apmsooner

    @giuse69, i created a separate ticket for the subfield formatter settings in views: https://www.drupal.org/project/custom_field/issues/3489629 📌 Views individual subfield formatter support Active . Let's keep that discussion out of this ticket as it is completely irrelevant to the token support in this ticket.

  • 🇮🇹Italy giuse69

    Hi, the issue is not about subfields, It is for the overall custom field when used in a view for taxonomy terms with relationship. i hoped my last description was clear about that but maybe not.
    Anyway ok for a new thread but the issue is differenti, let me know if I can change the title/description of the new one or you want me to open another one.
    Cheers

  • 🇺🇸United States apmsooner

    I believe its a subfield issue. Just follow the other ticket and I'll have a patch over there soon that you can test out. I just can't do it in this same branch as theres alot of code involved that needs to be tested separately.

  • Pipeline finished with Skipped
    22 days ago
    #350834
    • apmsooner committed 5437ee55 on 3.0.x
      Issue #3476033 by apmsooner, giuse69: Support not just values in custom...
  • 🇺🇸United States apmsooner

    @giuse69, I created a patch in https://www.drupal.org/project/custom_field/issues/3489629 📌 Views individual subfield formatter support Active that you can play with. Follow that issue and post any comments you have over there going forward. Understand the patch is not complete but it may at least help you figure out if your issue is somewhat resolved by using the new subfield formatters vs. the field one. Please post there though as this issue is now already merged and marked fixed.

Production build 0.71.5 2024