3

I've added a new vocabulary to Taxonomy, and then proceeded to add several custom fields to that new vocabulary. In the taxonomy_term_data table, I see the values for the tid, vid, name, description, format, and weight fields. It also looks like there are now a couple of new tables in the database that are related to the new custom fields that I created for the new vocabulary (i.e. field_data_field_taxonomy_cip_code). The values in the new table are entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_taxonomy_cip_code_value, and field_taxonomy_cip_code_format. There's nothing that I can see on the surface that would allow me to relate the two tables together.

How would I go about pulling the additional custom field values along with the new vocabulary values?

0

If I understand your question, taxonomy_term_data.tid should relate to like field_taxonomy_cip_code_value.entity_id. In Drupal 7 Taxonomy terms are now entities.

Entity Ids and Bundle values in the database aer typically the database keys used to relate "fields" between tables (as drupal "Fields" can be reused across multiple bundles).

  • Interesting. I certainly appreciate your quick (and correct) response! This gave me precisely what I was looking for :) – cmmsites Jun 28 '12 at 17:35
  • By the way, this little query is what I used to test your answer: SELECT fdftcc.field_taxonomy_cip_code_value cip_code, ttd.name cip_desc FROM taxonomy_term_data ttd, field_data_field_taxonomy_cip_code fdftcc WHERE ttd.tid = fdftcc.entity_id AND fdftcc.deleted = 0 – cmmsites Jun 28 '12 at 17:44
8

If you load the taxonomy term with taxonomy_term_load(), you will get any custom field you added to the vocabulary.

I created a vocabulary to which I add a boolean field (field_test_boolean); I added a taxonomy term to that vocabulary, and then executed the following code. (22 is the taxonomy term ID for the taxonomy term I created.)

$taxonomy_term = taxonomy_term_load(22);
dsm($taxonomy_term);

The output shown by dsm() is the following one.

screenshot

If you want to get the field value for the language currently set for the field, you should use the following code.

$values = field_get_items('taxonomy_term', $taxonomy_term, 'field_test_boolean');

If you need to find the taxonomy terms for which the custom field contains specific values, you should use the EntityFieldQuery class.

  $query = new EntityFieldQuery();
  $entities = $query->entityCondition('entity_type', 'taxonomy_term')
    ->entityCondition('bundle', 'fields_test')
    ->fieldCondition('field_test_boolean', 'value', 1, '=')
    ->execute();

  if (!empty($entities['taxonomy_term'])) {
    $taxonomy_terms = taxonomy_term_load_multiple(array_keys($entities['taxonomy_term']));
  }

Note

As noticed in a comment, there is a bug that doesn't allow to use the EntityFieldQuery class for taxonomy terms. The code seems fixed in the Drupal 7 development snapshot, but Drupal 7.14 is affected by the bug.

  • 1
    Thank you so much for your response...this site has saved me several times already thanks to individuals such as you that are willing to share their expertise. – cmmsites Jun 28 '12 at 17:38
  • @kiamlaluno actually using EntityFieldQuery is not possible for taxonomy_term entity type because of a known bug, see here – sepehr Jun 29 '12 at 14:03
  • @sepehr It seems the code has been already fixed on both Drupal 7, and Drupal 8. It just needs a new Drupal 7 version to be created. – kiamlaluno Jun 29 '12 at 14:13
0

@kiamlaluno is right. And thereby you can join the fields to its entity in the database with:

SELECT td.* FROM taxonomy_term_data td
INNER JOIN field_data_field_taxonomy_cip_code f ON f.entity_id = td.tid AND f.entity_type = 'taxonomy_term'

Taxonomy terms are entities and their tid is their entity_id and the entity_type is taxonomy_term.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.