1

I need to create a vocabulary with this "twist": the taxonomy terms of the vocabulary are the titles of the article based on a specific content type.

This means that if I create, modify, or delete an article based on the content-type "vocabulary," the relative term in the vocabulary should be added, modified, or deleted.

Imagine I have the content type "Cars." I create different articles, one of which is "Ferrari 456", and another one is "Volkswagen Golf." I want a vocabulary called "cars" with a term for each article (e.g., one for "Ferrari 456", one for "Volkswagen Golf").

5
  • I am not clear why you need to update the content of a vocabulary basing on node titles. Would not it be easier to just add new taxonomy terms to the vocabulary?
    – avpaderno
    Commented Jul 19, 2011 at 9:02
  • No, i want the terms of the vocubalry to reflect the articles created with a specific content. Commented Jul 19, 2011 at 9:04
  • Also, are you sure you are not using a content type when you just need a vocabulary? Bear in mind that every taxonomy term has a description too; instead of using the title and the body of a node, you would use the name and the description of a taxonomy term.
    – avpaderno
    Commented Jul 19, 2011 at 9:14
  • No i'm sure, they need to be articles because they have an image and various descriptive fields Commented Jul 19, 2011 at 9:21
  • You can add fields to a taxonomy term, as taxonomy terms are entities, in Drupal 7, and they are also fieldable entities. Go to admin/structure/taxonomy/[vocabulary]/fields, where [vocabulary] is the name of a vocabulary you have, and you will notice you can add new fields, including existing ones. When adding a new field, you can select "Image" as field type.
    – avpaderno
    Commented Jul 19, 2011 at 9:28

1 Answer 1

0
  • When you are creating a new node, you call taxonomy_term_save(), passing as argument the taxonomy term object $term, and using the node title for set $term->name, and the vocabulary ID to set $term->vid.
  • When you update a node, you first you load the taxonomy term with taxonomy_term_load_multiple(), as in taxonomy_term_load_multiple(array(), array('name' => trim($name), 'vid' => $vid)), change $term->name, and save the taxonomy term with taxonomy_term_save().
  • When you delete a node, you first load the taxonomy term with taxonomy_term_load_multiple(array(), array('name' => trim($name), 'vid' => $vid)), and then delete it passing $term->tid to taxonomy_term_delete().

The functions I reported needs to use the vocabulary ID; this means that the module you are writing needs to save in a Drupal variable the ID of the vocabulary it created, or to ask to the user the vocabulary name for the vocabulary that needs to be used. In the second case, and in the case the module stores the vocabulary name, code similar to the following one should be used to get the vocabulary ID:

$vocabulary = taxonomy_vocabulary_load_multiple(array(), array('name' => trim($name)));
$vocabulary = reset($vocabulary);
$vid = $vocabulary->vid;

The code needs to be changed for Drupal 8, as the conditions (the second parameter of taxonomy_term_load_multiple(), and taxonomy_vocabulary_load_multiple()) will be removed in Drupal 8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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