Currently taxonomy_entity_index only provides "indexing" of core term reference fields. As entityreference fields can sometimes better be used to reference terms (e.g. multiple bundles supported), it was great we could support those fields in taxonomy_entity_index.

I'll give it a try now-

Comments

derhasi’s picture

Status: Active » Needs review
StatusFileSize
new7.44 KB

And there is a working version:

  • Makes taxonomy_entity_index "pluggable" via hook_field_info()
  • Provides implementation for entityreference
saltednut’s picture

Title: Provide implementation for entityreference fields. » Make taxonomy_entity_index "pluggable" and provide example implementation using entityreference fields.

There seems to be a lot going on here. Is it standard to provide a DEVELOPERS.txt? Perhaps that info should get moved into a README.txt - we need one of those anyway :)

dave reid’s picture

Is there really any other field aside from entityreference that can refer to taxonomy terms? Of anything, I think those most useful code here is taxonomy_entity_index_get_terms_from_taxonomy_term_reference() and taxonomy_entity_index_get_terms_from_entityreference(). I'm not super sure that we actually need to abstract much else though.

derhasi’s picture

@Dave, I think the abstraction does not cost anything in here, and it would ease the work for custom fields building upon taxonomy term references.

fox mulder’s picture

Issue summary: View changes

There is a small error in the function taxonomy_entity_index_get_terms_from_entityreference()

function taxonomy_entity_index_get_terms_from_entityreference($entity_type, $entity, $field) {
  // We only collect term relations from entityreferences with really direct
  // to a taxonomy term.
  if (!empty($field['settings']['target_type']) && $field['settings']['target_type'] == 'taxonomy_term') {
    if ($items = field_get_items($entity_type, $entity, $field['field_name'])) {
      foreach ($items as $delta => $item) {
        $terms[] = array(
          'tid' => $item['target_id'],
          'delta' => $delta,
        );
      }
    }
  }
  return $terms;
}

if the first if statement returns FALSE than $terms variable does not exist.
Solution 1.:

function taxonomy_entity_index_get_terms_from_entityreference($entity_type, $entity, $field) {
  // We only collect term relations from entityreferences with really direct
  // to a taxonomy term.
  $terms = array();
  if (!empty($field['settings']['target_type']) && $field['settings']['target_type'] == 'taxonomy_term') {
    if ($items = field_get_items($entity_type, $entity, $field['field_name'])) {
      foreach ($items as $delta => $item) {
        $terms[] = array(
          'tid' => $item['target_id'],
          'delta' => $delta,
        );
      }
    }
  }
  return $terms;
}

or solution 2.:

function taxonomy_entity_index_get_terms_from_entityreference($entity_type, $entity, $field) {
  // We only collect term relations from entityreferences with really direct
  // to a taxonomy term.
  if (!empty($field['settings']['target_type']) && $field['settings']['target_type'] == 'taxonomy_term') {
    if ($items = field_get_items($entity_type, $entity, $field['field_name'])) {
      foreach ($items as $delta => $item) {
        $terms[] = array(
          'tid' => $item['target_id'],
          'delta' => $delta,
        );
      }
    }
    return $terms;
  }
}
fox mulder’s picture

same situation in taxonomy_entity_index_get_terms_from_taxonomy_term_reference() function too...

scotwith1t’s picture

Super happy to find that someone else has already run into this and written a patch. Anxious to try but wondering how likely this is to get in? We often use entityreference fields instead of term reference fields for the reasons mentioned and I would love to see an official implementation in this module so we don't have to keep it in our "hacked" folder :-/ Will report back if I have success with the patch though, thanks!

scotwith1t’s picture

Well, first try at this didn't work for me. I suspect it has to do with the fact that the use case for us is an og_vocab field, which uses a more complicated OG-based widget, so it probably just won't cooperate with this. Haven't had time to dig into it much, but hope to see some progress on this issue regardless as it makes perfect sense to catch entityreference fields using the Taxonomy entity type, which are essentially Term Reference fields.

dpacassi’s picture

Seems to work for me, I've updated the patch to also fix the small errors found by fox mulder in #5