Auto Tagging of Taxonomy terms in Drupal7

ByKarthik Kumar D Kon27th Sep 2015, 2022-12-01T08:00:00+05:30
Read Article
Auto Tagging of Taxonomy terms in Drupal7

This article help you to create the way to implement the autotagging of the taxonomy terms based on the other field of the node page. This can be implemented by using the hook_node_presave().

In our case lets consider the field_tags is the taxonomy field, and we are autotagging based on the content present in title and body fields.

All these actions are implemented inside the hook_node_presave().

1. First thing is getting the list of terms of the field_tags vocabulary, this is done in the below code.

$field = field_info_field('field_tags');
// read the field information.
$voc_name = $field["settings"]["allowed_values"][0]["vocabulary"];
// get the vocabulary name which is been configured for the tag field.
$voc = taxonomy_vocabulary_machine_name_load($voc_name);
// load the vocabulary object.
$terms = taxonomy_term_load_multiple(array(), array('vid' => $voc->vid));
// get all the terms in the vocabulary.
$termnames = array();
foreach($terms as $termskey => $termsvalue) {
    $termnames[$termskey] = $termsvalue->name;

// iterating the terms objects and getting out the terms names out of it.

2. So now we have the $termnames which needs to be cross checked with the content in the title and body fields, this is done in the below code.

// read the node title and body and stored in the $content.
$content = $node->title;
$content .= $node->body['und'][0]['value'];
// Iterate all the terms and compare with the content words, if matched then store them in seperate array.
$node_tids = array();
foreach($termnames as $termname) {
    if (strpos($content, $termname) !== false) {
        $term = taxonomy_get_term_by_name($termname);
        foreach($term as $termkey => $termvalue) {
            $node_tids[] = (array) $termvalue;

3. After this, we will have the term ids $node_tids which needs to be saved as autagged terms, this is just assinging to node object $node.

$node->field_tags["und"] = $node_tids;

Note: In case of exisisting node in be updated, we should take care of the existing terms which are saved in the node, to do this we need to add some code which does it.

$node_tids_all = array();
// check if any existing terms are their in the field_tags
if(!empty($node->field_tags)) {
       // in case not empty, merge the terms with the existing terms.
    $node_tids_all = array_merge_recursive($node->field_tags["und"], $node_tids);

// In case any duplicates exist, get out the unique ones.
$node_tids_all = array_map("unserialize", array_unique(array_map("serialize", $node_tids_all)));
// then assign the selected terms to the node object.
$node->field_tags["und"] = $node_tids_all;

So, thats it, return the $node object at the end of the hook_node_presave(). this autotags the term field with title and body field.

Thank you :)

We Need Your Consent
By clicking “Accept Cookies”, you agree to the storing of cookies on your device to enhance your site navigation experience.
I Accept Cookies