2

I have a content type with a Taxonomy for locations, these locations can be placed inside parent locations (f.ex United Kingdom -> England -> London), and it is set-up such that the end user can add taxonomy terms that do not currently exist when creating this content.

What I would like to achieve is upon creating a node that ALSO adds a NEW taxonomy term, is to redirect the page to the list of terms page so that the end user can categorise the new location appropriately.

Is this possible?

1 Answer 1

1

I'm not positive I understand the why, but the redirect-on-saving-new-node-with-new-terms bit should be reasonably straightforward.

I've suggested two possible methods below because the timing of db inserts/updates etc around node creation is a bit intricate, and you may need to shift the task to an earlier or later stage in the process. For example, the API docs mention that the db write cannot be presumed to be complete during hook_node_insert(), and it definitely won't be on hook_node_submit(). So your mileage may vary :)

You'll need to create a custom module (here's a stackexchange question on creating modules if you don't already know how) that either:

  1. implements hook_node_submit()

    This is the very likely to be the simpler/more reliable of the two methods.

  2. implements hook_node_insert()

    In this method, it is possible that drupal_goto() will conflict with the redirect specified in the node form's $form_state['redirect'], so it's probably the worse of the two possibilities.

Method 1: implement hook_node_submit()

From inside yourmodule_node_submit(), you'll need to:

  1. Make sure that a node with this nid does not already exist!
  2. find out what terms (if any) are in the new node ($node is passed into the function)
  3. load all the terms for the relevant vocabulary, probably with taxonomy_vocabulary_load()
  4. check to see if any of the node terms are not in the full list
  5. modify $form_state['redirect'] if required

See drupal_redirect_form() for a bit more information on what happens with $form_state['redirect'].

Method 2: implement hook_node_insert()

From inside yourmodule_node_insert(), you'll need to:

  1. find out what terms (if any) are in the new node ($node is passed into the function)
  2. load all the terms for the relevant vocabulary, probably with taxonomy_vocabulary_load()
  3. check to see if any of the node terms are not in the full list
  4. if that's true, use drupal_goto() to redirect accordingly
1
  • Thanks for the detailed response. I'll give this a shot and see how things go - and in case you were curious as to the why, the taxonomies are used to search a database by location, and categorising them into a hierarchy means that it's possible for the user to search a wider 'area'. Is there a module I'm not aware of that would make this a lot easier? What I'm doing presently works but obviously when a new location is added it cannot be automatically added to its correct position in the hierarchy.
    – Drif.io
    Commented Dec 18, 2014 at 9:39

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.