0

I have a need to automatically create an Article node type each time a Basic-Page node type is created - I am using Rules successfully to achieve that effect.

My problem is a Menu link also needs to be created for each Article node created. This means the Provide a menu link checkbox needs to be automatically enabled in the process of creating each Article node - but it isn't.

enter image description here

The image above shows the checkbox I'm trying to automatically enable.

When creating an Article node directly, i:e not using Rules to create the new Article node, the box is checked and things work as normal.

I really need to find a solution which will enable the checkbox as I auto create my node types.

1 Answer 1

0

I don't think that it's possible through the UI. But here is how I would do programmatically:

function mymodule_node_presave($node) {
  if ($node->type == 'article') {
    $node->menu['enabled'] = 1;
  }
}

It would make your "Provide a menu link" checkbox as checked automatically.

You can also set all these values if you need to:

enter image description here

EDIT: I'm so sorry I forgot the node_save line in the code. Here is the correction:

function mymodule_node_presave($node) {
  if ($node->type == 'article') {
    $node->menu['enabled'] = 1;
    node_save($node);
  }
}
4
  • Thanks Djouuuuh but it didn't work. I have tried the exact same segment of code but in hook_node_insert(). I don't know if the difference in hooks makes any difference but neighter solved the problem anyway.
    – sisko
    Sep 25, 2014 at 15:25
  • Please see my update in answer.
    – Djouuuuh
    Sep 26, 2014 at 7:56
  • Good morning Djouuuuh. Thank you for the update. Sorry to say I had tried the ndoe_save function but it just results in a loop i:e the Basic-Page node is perpectually "loading". This goes on for a while and I'm yet to see it actually finish :-)
    – sisko
    Sep 26, 2014 at 10:30
  • This is similar to this (stackoverflow.com/questions/17546839/…) and their code works :-(
    – Djouuuuh
    Sep 26, 2014 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.