I have a taxonomy reference field with multible and hierarchical terms selected. The token [term:field-taxonomy_ref_field] is giving me the term title with leading dashes dependent on the term hierarchy. These dashes shouldn't be there.
For a vocabulary like this:
term_lvl1
  term_lvl2-01 (selected)
  term_lvl2-02 (selected)
    term_lvl3-01 (selected)
I get a value of this: -term_lvl2-01, -term_lvl2-02, --term_lvl3-01.
This seems also to depend on the kind of select widget you can use.
For checkbox, select field, or term reference tree (contrib module) I get a term name in the form of: -termname or --termname with dashes in front of the name (depending on the hierarchy level of the term).
For the autocomplete term widget (tagging) I get the correct term string like: termname.
If I use the token of the token module (with underscore) [term:field_taxonomy_ref_field] the correct values are returned term_lvl2-01, term_lvl2-02, term_lvl3-01.
So this has to be an entity token bug. I tried to debug into the code where the values are pulled but get lost somewhere in the (somewhat confusing) code.
So maybe someone more experienced with entity and token can help out.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

marcelovani’s picture

Version: 7.x-1.5 » 7.x-1.7
Component: Entity tokens » Entity property wrapper
FileSize
88.68 KB
98.93 KB
116.42 KB
67.68 KB
81.26 KB
78.08 KB
46.43 KB
40.54 KB
141.99 KB

This issue was opened 2 years ago, but the actual problem started back in 2010 https://www.drupal.org/node/1005532
I wonder how not many people is following this issue.
Here are my findings.

How to replicate the problem

  1. Install Drupal 7: minimal profile
  2. Enable the following modules
  • drush en -y entity_token
  • drush en -y token
  • drush en -y admin_menu
  • drush en -y token_filter
  • drush en -y taxonomy
  • drush en -y field_ui
  • Create a vocabulary
  • Add 3 terms (t1, t2, t3) to the vocabulary
  • Drag the terms so that it becomes a tree like this
       t1
        -t2
          -t3

  • Add a new content type (page) and add a term reference field (type Select list) for the vocabulary. Allow unlimited values


  • Open the configuration for Content authoring > text formats > plain text
  • Turn on Token filter
  • Add a page and select the 3 terms
  • Set the body to [node:field-term]
  • Save the node
  • View the node
  • The body will be t1, -t2, --t3
  • How to stop the problem

    Edit entity.wrapper.inc

    +++ includes/entity.wrapper.inc
      public function label() {
        if ($options = $this->optionsList('view')) {
          return;+
    

    I added a return here and that seems to fix the problem, but I wonder what does that code actually do and why it was introduced in https://www.drupal.org/node/1005532

    I understand adding the hyphens on the CMS, when you are editing the node, but these hyphens should not be present on the front end.

    marcelovani’s picture

    Update: Not sure if I did something wrong when I added these steps or I am doing something wrong now, but I am no longer able to reproduce the problem.

    quotesBro’s picture

    Version: 7.x-1.7 » 7.x-1.x-dev

    Confirming: when using token with underscore (e.g. [term:field_taxonomy_ref_field]), the correct label is returned.
    When using token with hyphen (minus sign) (e.g. [term:field-taxonomy-ref-field]), label for the child term starts with a minus.

    quotesBro’s picture

    Component: Entity property wrapper » Entity tokens
    quotesBro’s picture

    The problem is $node_entity_wrapper->field_taxonomy_term_reference instantiates EntityListWrapper, and its label generation via optionsList invokes taxonomy_allowed_values, that loads the full taxonomy vocabulary tree with depth and adds leading dash for each level of term depth.

    The Token module, on the other hand, implements field_tokens(), so the specific field value is used as a replacement.

    quotesBro’s picture

    quotesBro’s picture