Is it possible to automatically tag a node with it's title?

For example I have NAT setup to make a term based off the title of a node and I am wondering if at the same time that node can be tagged with the freshly created term (i.e. the title)?

Doesn't seem like something that could happen simultaneously i.e. the node is created -> term is created -> node is tagged.

Anyway to make the term is created -> node is tagged step automatic?

Comments

pkej’s picture

I don't know of a direct solution, however, you could try to do it with a computed field, and do it with php, thus a solution would be template independent.

Paul K Egell-Johnsen

pkej’s picture

Doesn't NAT do that out of the box? Perhaps NAT-NG does?

Paul K Egell-Johnsen

aegreen’s picture

I thought/think so but I'm not sure how to configure it - nor what the difference between NAT and NAT-ng.

Right now I have NAT-ng installed and the settings are as follows:

Vocabularies:
(Selected)People
Creating a node of type roster_profile will automatically create a term in any selected vocabularies.

N Associate node body with term description.
Y Delete associated term if a node is deleted.
N Delete NAT-ng node when a NAT-ng term is deleted.
N Allow users to define synonyms and related terms when they create and edit nodes.
N Make NAT-ng terms in roster_profile node views point to the associated node rather than the taxonomy page.

(Y = checked box, N = unchecked box)

pkej’s picture

I think this page might give you the general idea of how to do this with token and some code. http://blog.adyax.com/2009/02/english-drupal-tutorial-auto-tag-with-token/

This line in the first code block:

    '#default_value' => variable_get('token_autotag_pattern', '[month], [language], token-autotag'),

should use the token [title] only, like thus:

    '#default_value' => variable_get('token_autotag_pattern', '[title]'),

And then you need to just use a more proper name. Unfortunately the tutorial is used to create a module, so you have to paste the code into your_module.module. In addition you will need a your_module.info file for letting drupal recognize the module.

BTW, I think this solves a similar problem I have, or at least partially. I have not tried this, I just found it by the way of a few directed searches.

Paul K Egell-Johnsen

aegreen’s picture

Thanks I had stumbled upon it before but couldn't understand it so I passed it by. I just tried implementing though as per your instructions but unfortunately I have never made/edited a module before so this is all new to me and I am stuck.

I created a folder called "autotag-token" and placed autotag-token.info and autotag-token.module in it and in my modules directory and was able to enable it. BUT I don't know where to go from there. I can't seem to find the form/settings for it in the administration menu or when I edit a content type.

This is the code in each file:

autotag-token.info

; $Id: autotag-token.info,v 1.0 2010/01/18 11:51:25 sdrycroft Exp $
name = "Taxonomy Autotagger-Token (autotag)"
description = "Automatically tags nodes with terms from a specified series of tokens"
package = "EDIT"

core = 6.x

autotag-token.module

<?php
// $Id: autotag-token.info,v 1.0 2010/01/18 11:51:25 sdrycroft Exp $

/**
* Menu callback for the token auto-tag settings form.
*/
function token_autotag_admin_settings() {
 
  $form['token_autotag']['token_autotag_pattern'] = array(
    '#type' => '<span class="misspell">textfield</span>',
    '#title' => t('Autotag pattern'),
    '#default_value' => variable_get('token_autotag_pattern', '[title]'),

  );
 
  if (module_exists('token')) {
    $form['token_autotag']['token_help'] = array(
      '#title' => t('Replacement patterns'),
      '#type' => '<span class="misspell">fieldset</span>',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
    );
 
    $form['token_autotag']['token_help']['help'] = array(
       '#value' => theme('token_help', 'node'),
    );
  }
 
  return system_settings_form($form);
}

Thanks again for all your help. I'm sorry I'm such a newbie.

Alex

pkej’s picture

We've all been newbies. I still am when it comes to writing modules, I haven't actually done so yet :)

You also need to add to the autotag-token.module the following code:



/**
* Implementation of hook_<span class="misspell">nodeapi</span>().
*/
function token_autotag_nodeapi($node, $op) {
  if ($op == 'presave') {
    if (module_exists('token') && $node-&gt;taxonomy['tags'][1] == '') {
      $node->taxonomy['tags'][1] = token_replace(variable_get('token_autotag_pattern', '[title]'), 'node', $node);
    }
  }
}

I do not know where the form will reside, try the following paths:

/admin/build/token_autotag
/admin/settings/token_autotag

But, it doesn't really matter, because the above code would probably replace it with title anyway! At least at a cursory glance I think so.

Paul K Egell-Johnsen

pkej’s picture

I will test this on my own develop server tonight, there is a bug in the code as noted in the comments on the original page, but I can't see how the supplied bug fix will work in all instances.

Paul K Egell-Johnsen

pkej’s picture

I never found the form which is created :P
autotag_with_title.module

<?php
// $Id: autotag-token.info,v 1.0 2010/01/18 11:51:25 sdrycroft Exp $

/**
* Menu callback for the token auto-tag settings form.
*/
function autotag_with_title_admin_settings() {

  $form['autotag_with_title']['token_autotag_pattern'] = array(
    '#type' => '<span class="misspell">textfield</span>',
    '#title' => t('Autotag pattern'),
    '#default_value' => variable_get('token_autotag_pattern', '[title]'),

  );

  if (module_exists('token')) {
    $form['autotag_with_title']['token_help'] = array(
      '#title' => t('Replacement patterns'),
      '#type' => '<span class="misspell">fieldset</span>',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
    );

    $form['autotag_with_title']['token_help']['help'] = array(
       '#value' => theme('token_help', 'node'),
    );
  }

  return system_settings_form($form);
}

/**
* Implementation of hook_<span class="misspell">nodeapi</span>().
*/
function autotag_with_title_nodeapi($node, $op) {
  if ($op == 'presave') {
    if (module_exists('token') && $node->taxonomy['tags'][1] == '') {
      $node->taxonomy['tags'][1] = token_replace(variable_get('token_autotag_pattern', '[title]'), 'node', $node);
    }
  }
}

autotag_with_title.info

; $Id: autotag_with_title.info,v 1.0 2010/01/18 11:51:25 sdrycroft Exp $
name = "Autotag a Node with its own Title (autotag_with_title)"
description = "Automatically tags nodes with terms from its title field. You need to download the <a href="http://drupal.org/project/token">Token</a>-module to use this."
package = "Taxonomy"

core = 6.x
dependencies[] = taxonomy
dependencies[] = token

It only works with the FIRST taxonomy (id 1).

Improvements would be:

a) add the form and settings to the Taxonomy type.
b) add the option to configure which specific node types should be autotagged
c) add the option to autotag with different tokens for different node types (to solve a different Use-Case where nodes referencing the created node can add the name of the referenced node as the tag, and not their own titles).
d) add a form for configuring per nodetype and per taxonomy (a matrix, in other words), much like pathauto.

Paul K Egell-Johnsen

aegreen’s picture

Thanks for all the help Paul. I tried it but alas I still can't figure it out - I think I can't get autotagging to work at all actually.

BUT I found a solution!

I made a Trigger Rule. It will load the content type 'Roster Profile' when it is saved, then load the vocabulary that I have made - 'People', then add a term from the contents Title and then add it to the content. This avoids the need for NAT at all (except it doesn't remove the term - but I don't see a problem with that).

array (
  'rules' => 
  array (
    'rules_2' => 
    array (
      '#type' => 'rule',
      '#set' => 'event_node_insert',
      '#label' => 'Tag Roster Profile',
      '#active' => 1,
      '#weight' => '0',
      '#categories' => 
      array (
      ),
      '#status' => 'custom',
      '#conditions' => 
      array (
        0 => 
        array (
          '#weight' => 0,
          '#info' => 
          array (
            'label' => 'Created content is Roster Profile',
            'arguments' => 
            array (
              'node' => 
              array (
                'type' => 'node',
                'label' => 'Content',
              ),
            ),
            'module' => 'Node',
          ),
          '#name' => 'rules_condition_content_is_type',
          '#settings' => 
          array (
            'type' => 
            array (
              'roster_profile' => 'roster_profile',
            ),
            '#argument map' => 
            array (
              'node' => 'node',
            ),
          ),
          '#type' => 'condition',
        ),
      ),
      '#actions' => 
      array (
        0 => 
        array (
          '#weight' => -10,
          '#info' => 
          array (
            'label' => 'Load a vocabulary',
            'new variables' => 
            array (
              'taxonomy_vocab' => 
              array (
                'type' => 'taxonomy_vocab',
                'label' => 'Taxonomy vocabulary',
              ),
            ),
            'eval input' => 
            array (
              0 => 'vocabulary|vocab_text',
            ),
            'module' => 'Taxonomy',
          ),
          '#name' => 'rules_action_taxonomy_load_vocab',
          '#settings' => 
          array (
            'vocabulary' => 
            array (
              'vocab_select' => '5',
              'vocab_text' => '',
            ),
            '#argument map' => 
            array (
              'taxonomy_vocab' => 'taxonomy_vocab',
            ),
          ),
          '#type' => 'action',
        ),
        3 => 
        array (
          '#weight' => -7,
          '#info' => 
          array (
            'label' => 'Add a new term to vocabulary',
            'arguments' => 
            array (
              'taxonomy_vocab' => 
              array (
                'type' => 'taxonomy_vocab',
                'label' => 'Taxonomy vocabulary',
              ),
            ),
            'new variables' => 
            array (
              'title_term' => 
              array (
                'label' => 'title term',
                'label callback' => false,
                'type' => 'taxonomy_term',
              ),
            ),
            'eval input' => 
            array (
              0 => 'term|name',
              1 => 'term|description',
            ),
            'module' => 'Taxonomy',
          ),
          '#name' => 'rules_action_taxonomy_add_term',
          '#settings' => 
          array (
            'term' => 
            array (
              'name' => '[node:title]',
              'description' => '',
            ),
            '#argument map' => 
            array (
              'taxonomy_vocab' => 'taxonomy_vocab',
              'taxonomy_term' => 'title_term',
            ),
            '#eval input' => 
            array (
              'token_rules_input_evaluator' => 
              array (
                'term|name' => 
                array (
                  0 => 'node',
                ),
              ),
            ),
          ),
          '#type' => 'action',
        ),
        2 => 
        array (
          '#weight' => 0,
          '#info' => 
          array (
            'label' => 'Assign a term to content',
            'arguments' => 
            array (
              'node' => 
              array (
                'type' => 'node',
                'label' => 'Content',
              ),
              'taxonomy_term' => 
              array (
                'type' => 'taxonomy_term',
                'label' => 'Taxonomy term',
              ),
            ),
            'module' => 'Taxonomy',
          ),
          '#name' => 'rules_action_taxonomy_term_assign_to_content',
          '#settings' => 
          array (
            '#argument map' => 
            array (
              'node' => 'node',
              'taxonomy_term' => 'title_term',
            ),
          ),
          '#type' => 'action',
        ),
      ),
      '#version' => 6003,
    ),
  ),
)
pkej’s picture

Cool. I will look into doing this, and perhaps it will solve some other use-cases.

Paul K Egell-Johnsen

GreyHawk’s picture

It was attached to the main thread instead of here...

http://drupal.org/node/864674#comment-4063828

...essentially, thanks for the info and idea about the rule. I successfully adapted it and used it to redefine some site workings on a client site, enabling me to utilize taxonomy instead of a couple of CCK fields. Much cleaner outcome. Thanks!

GreyHawk’s picture

I just adapted it to a site and eliminated the need for a couple of CCK fields that I can now better put to use via Taxonomy.