3

Developing a site that allows users to post images (node type = my_image) to a specific category. Basically, we create the categories and users submit images to them.

Users do not have menu or url permissions, we want to do this for them. When a user is in a category, the drupal form is displayed on that page using drupal_render_children().

In HOOK_node_insert (type == my_image) we identify what category they were in and add it to the menu. Following this we are trying to use pathauto_create_alias to generate the alias (based on title) but have been unsuccessful thus far (HOOK_node_update would, well update the alias).

Obviously, if we grant the user permission for menu and url, the alias is created. We don't want the user to have direct access to the menu system nor aliasing.

Does anyone know the proper way to use pathauto_create_alias in the above scenario?

  • Please post an answer as an answer, not inside a question's body. – Mołot Aug 20 '13 at 14:15
5

Basically to generate URL aliases in code using the pathauto module you would have something like this:

module_load_include('inc', 'pathauto'); // include the inc file for Pathauto module

/*
   pathauto_create_alias($module, $op, $source, $data, $type, $language)
   @param $module: Your module name
   @param $op: The operation (insert, update, return, bulkupdate)
   @param $source: Internal Drupal path (i.e.: node/1)
   @param $data: Addition data used to generate a pathalias, in this case the node object
   @param $type: Type of content that will be used to match a pattern, defined in the backend url alias patterns
   @param $language: If you have a multlingual website, you need to pass the language for which you want to generate the url alias
*/

pathauto_create_alias('your_module_name', 'insert', 'node/1', array('node' => $node), $node->type, $language->language); // Language is optional
  • Mike, thanks for the post. I tried your suggestion before I posted the question and was never successful regardless of what I did. Just discovered that if the menu is valid before it gets to node_save, pathauto will work as expected. Adjusting the menu in HOOK_node_presave was the answer (see edited post above). – cowboy_ela Aug 19 '13 at 21:28
  • Ok, glad you found a solution! I will put my code in a project and make it work. I'll post it here, it could be useful in the future (and for other developers) – Mike Vranckx Aug 20 '13 at 18:47

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.