I tried the 'taxonomy-term-reference' property and it works great! I can't wait for further entity types to be available.

Currently, however, it only returns the label. I'd assume it would also have the link to the term page.
It would also be helpful to have the entity ID to get further values/fields from the entity.

It is currently not possible to get the entity ID in the components Twig template, right?

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

thomas.frobieter created an issue. See original summary.

anybody’s picture

I guess it's the target_id that would be helpful to expose to the SDC. I agree that there are cases where you for example need to get additional information from the term (e.g. through twig_tweak) where that would be super helpful! I'm also interested if that's possible!

anybody’s picture

Title: Get further values from the (Taxonomy Term) entity » Get further values (or tid instead of name) from the (Taxonomy Term) entity

I guess we'd need to make

public const PROP_EXPRESSION = "ℹ︎entity_reference␟entity␜␜entity:taxonomy_term␝name␞␟value";

https://git.drupalcode.org/project/canvas_entity_reference/-/blob/1.0.x/...

configurable per component or in the general settings?
For example to return the TID instead of the name it might be

public const PROP_EXPRESSION = "ℹ︎entity_reference␟entity␜␜entity:taxonomy_term␝tid␞␟value";

is that correct?

Is it also possible to pass multiple values to the SDC?
Otherwise at least being able to set which property it should return (with tid as most flexible one) would be great!

thomas.frobieter’s picture

public const PROP_EXPRESSION = "ℹ︎entity_reference␟entity␜␜entity:taxonomy_term␝tid␞␟value";

is that correct?

Yes, just tested and I get the TID instead the term name.

anybody’s picture

We have a similar requirement for images (from Media library): #3588802: How to get the Media ID (target_id) of a Image Object in a SDC? in Canvas itself.

zeeshan_khan’s picture

This has been implemented in the 1.0.x branch.

What was done

  • Added a new x-entity-field JSON Schema annotation.
    Component authors can specify which field on the referenced entity is returned
    as the prop value.
  • If x-entity-field is omitted, the module falls back to the
    entity type's label key (e.g. title for nodes, name
    for taxonomy terms and users).
  • Works on both single-value props and array (type: array)
    props — the annotation can be placed on the root schema or on
    items.

Usage examples

Return the node title (default, no annotation needed):

                                                     
  article:                                                                      
    type: string
    x-entity-type: node                                                         
  

Return the numeric node ID instead:

  article_id:                                                                   
    type: integer                                           
    x-entity-type: node
    x-entity-field: nid
  

Return a taxonomy term's tid instead of its name:

  tag_id:                                                                       
    type: integer                                           
    x-entity-type: taxonomy_term                                                
    x-entity-field: tid                                     
  

The annotation can reference any field machine name available on the entity
type. The schema type should match what the field value actually
returns (e.g. integer for ID fields, string for
text fields).

@anybody @thomas.frobieter - I have added new release with all the changes 1.0.1

zeeshan_khan’s picture

Status: Active » Needs review

anybody’s picture

Status: Needs review » Needs work

Thank you @zeeshan_khan - that looks great and very flexible!

Some ideas:
1. In https://git.drupalcode.org/project/canvas_entity_reference/-/blob/1.0.x/... you should log an error and not mute it entirely, so administrators are able to find such errors and fix it! A helpful error message would be great for the logs.

2. I guess same should be done in https://git.drupalcode.org/project/canvas_entity_reference/-/blob/1.0.x/... with at least a warning or notice, because otherwise if the isset(EntityReferenceConstants::REF_URI_TO_ENTITY_TYPE[$items_ref]) is false, it will silenty proceed?

3. Are there other places like this, where logging an error or warning similarly makes sense for DX?

4. The documentation on the module page / README should be updated to reflect the new possibilities? Maybe after #3588752: Allow using other entity widgets and also contrib widgets on entity_reference (Tagify, Entity Browser, ...) is also resolved?

zeeshan_khan’s picture

Thanks for the suggestions - below are the answers to your questions

Some ideas:
1. In https://git.drupalcode.org/project/canvas_entity_reference/-/blob/1.0.x/... you should log an error and not mute it entirely, so administrators are able to find such errors and fix it! A helpful error message would be great for the logs.
Ans - Will fix in upcoming release

2. I guess same should be done in https://git.drupalcode.org/project/canvas_entity_reference/-/blob/1.0.x/... with at least a warning or notice, because otherwise if the isset(EntityReferenceConstants::REF_URI_TO_ENTITY_TYPE[$items_ref]) is false, it will silenty proceed?
Ans - Will fix in upcoming release

3. Are there other places like this, where logging an error or warning similarly makes sense for DX?
Ans - I couldn't find any - please let me know if you find any.

4. The documentation on the module page / README should be updated to reflect the new possibilities? Maybe after #3588752: Allow using other entity widgets and also contrib widgets on entity_reference (Tagify, Entity Browser, ...) is also resolved?
Ans - #3588752 - already resolved
I did modify documentation a bit on module's page but forgot to update Readme.md will do that soon

Thanks again!

anybody’s picture

Thank you @zeeshan_khan! Let me know when ready and leave the commit here, then I'll review the changes.

Didn't find further places yet either.

zeeshan_khan’s picture

(Note: All changes live in dev release).

Addressed all the feedback points:

  1. The catch block in resolveFieldKey() now logs an error with the entity type and exception message, so administrators can identify misconfigured props in the Drupal logs.
  2. In resolveEntityTypeId(), when an array items schema contains a $ref URI that is not recognised by this module, a warning is now logged pointing to the fix — either add the URI to REF_URI_TO_ENTITY_TYPE or use x-entity-type instead.
  3. No other silent failure points found in the current codebase.
  4. README.md has been fully rewritten to cover all current features: x-entity-type, x-entity-field, x-entity-type-bundles, x-entity-widget, entity_render(), multi-entity-type support, and updated troubleshooting notes.
zeeshan_khan’s picture

Status: Needs work » Needs review
anybody’s picture

Version: 1.0.0 » 1.0.x-dev

PS: Another benefit of MRs is that we can comment them in GitLab, which makes reviews and communication easier.

One suggestion:
At 'Canvas Entity Reference: could not resolve label key for entity type "@type": @message' you could add "Falling back to "name" property." or something like that to also tell what happened in result :)

  • zeeshan_khan committed f7ce9b2f on feature/add-support-to-all-entities
    Issue #3588651: Improve fallback error message in resolveFieldKey()...
anybody’s picture

Status: Needs review » Reviewed & tested by the community
zeeshan_khan’s picture

MR https://git.drupalcode.org/project/canvas_entity_reference/-/merge_reque...
Good suggestion — updated the error message to:

Canvas Entity Reference: could not resolve label key for entity type "@type": @message Falling back to "name" property.

Administrators can now see both what failed and what the module did as a result, without needing to look at the source. Also noted your point about MRs — !4 is already open for this branch and future work will use MRs where possible.

zeeshan_khan’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.