0

I have a content type that requires me to assemble a URL from several sources - the node title, a taxonomy value, and some fields of a product that are referenced from a product display that this content type references. So:

mynode type
- title
- taxonomy value
- entity ref -> product display -> product field

The entity ref can have one or more products, so there is some logic I need to crunch to assemble the right URL. I didn't have luck doing this through pathauto, so it looks like I need to handle this on hook_node_presave(), which does set my alias, but I get a 404 on that alias, so it seems I'm missing a step.

My code is:

mymodule_node_presave($node) {
    if (empty($node->path['alias'])) {
        $node->path['alias'] = _some_fx_that_figures_out_url();
        $node->path['pid'] = 0;  
        $node->path['language'] = LANGUAGE_NONE;  
    }
}

I guess I'm missing something?

1 Answer 1

1

This seems to be a duplicate of this issue

Try using hook_node_insert($node) for new content and/or hook_node_update($node) for existing content:

function mymodule_node_insert($node) {
  if ($node->type == 'mytype') {
    $node->path['alias'] = _some_awesome_pattern_maker($node);
  }
}

function mymodule_node_update($node) {
  if ($node->type == 'mytype') {
    $node->path['alias'] = _some_awesome_pattern_maker($node);
  }
}

_some_awesome_pattern_maker($node) {
  // massage node and return string
}
5
  • For what it's worth, I needed to do the same thing WITH Pathauto, like: function mymodule_pathauto_alias_alter(&$alias, array &$context) { if (isset($context['data']['node']) && $context['data']['node']->type == 'my_content_type') { $node = $context['data']['node']; $alias = _awesome_alias_maker($node); } }
    – Nels
    Commented Jun 20, 2014 at 1:28
  • That doesn't work. My alias does display in my node edit page, but I get a 404. When I look at my node in devel, I don't have an alias value set, but I do have a pid value set.
    – vintorg
    Commented Jun 20, 2014 at 1:47
  • Can you provide an example of what your pattern-maker function returns? I just tried hook_node_presave($node) on my local dev box and it seemed to behave as expected, and I did not get a 404 trying the url. Does there happen to be anything helpful in the logs regarding the 404? Perhaps the symptom is misleading.
    – Nels
    Commented Jun 20, 2014 at 2:16
  • It looks like I wasn't setting the language val. When I set it in the DB, it works. Investigating further...
    – vintorg
    Commented Jun 20, 2014 at 2:18
  • I can't seem to get the language value set in my path array. It makes the entry into the aliases table, but doesn't set the language. When I go set the language (in my case, 'und') by hand, it works. I need to figure out why the language val isn't being set in the aliases table.
    – vintorg
    Commented Jun 20, 2014 at 15:12

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.