1

I need to pass the title of the current node to the title field of an add node form that pops up in a modal using. I am using auto node title which allows the use of tokens as replacement patterns but [current-page:title] results in the title 'Create User Photos', the title of the add node form. Can anyone tell me how to pass the title of the review node either through auto node title or by changing the form's title to match the review node?

I am using the form block module to render the add node form as a block then using js to display it as a modal. I am trying to pass the title to make the nodes related if you will.

I am open to other ways to relate the nodes.

A little context: I've got restaurant review nodes with photo galleries. Users can add photos to a reviews gallery via a modal form that creates a new node containing the photos. I would like the title of the new photo node to match the review node's title but I haven't been able to do that.

2
  • Hello and welcome. For the "little more detail" we don't really need to know if it's food or pet or space travel site. But we need to know how do you open that related node creation in popup and how you made these two related. Module names, code whatever it is.
    – Mołot
    Commented Dec 30, 2013 at 9:07
  • Thanks for reading! I am using the form block module to render the add node form as a block then using js to display it as a modal. Making them related is what the question is about. I'm trying to have the 'parent' node title passed to the new node so I can display all related nodes via views using a contextual filter. I could be going about this entirely the wrong way... Commented Dec 30, 2013 at 9:18

3 Answers 3

0

If your form is not loaded via ajax then you can write a form alter hook in your module and alter the photo node form and provide a default value for the title field of the photo node form. You can either query or do node_load() of the current review node to get the title. You can retrieve the nid of the current node by the arg(1) function.

When you have the title of the review node you can assign it as the default value of the photo node.

0

I've never used the Form Block module before, but since it's a form, I would use the "hook_form_alter" to update the title of the form.

function yourModule_form_alter(&$form, &$form_state, $form_id) {

   if ($form_id == "your_form_block_id" {

       // Get node details from URL arguments (normally /node/nid
       // I'm assuming this. Your node id can be arg(1) or arg(2)...
       // Changing it accordingly

       if (arg(1) && is_numeric(arg(1)) {

           $node = node_load(arg(1));
           $node_title = $node->title;

           // Replace form title
           $form["#title"] = $node_title;
       }
   }  
}
2
  • I tried something along these lines before and got the error "Trying to get property of non-object" for $node->title. Any idea as to how to fix this? Commented Dec 30, 2013 at 14:24
  • It seems you are not getting the node object, probably not getting the node ID correctly from the URL? I just updated my code. Node ID is usually on arg(1). Try var_dump($node) and see what you are getting.
    – Teknotica
    Commented Dec 30, 2013 at 14:48
0

So it turns out the right way to do this is by setting the title in hook_node_presave and using menu_get_object() to get the parent node.

    function mymodule_node_presave($node) {
        if ($node->type == 'user_photos') {
            if (!isset($node->nid)){ 
                $ref_node = menu_get_object();
                $node_title = $ref_node->title;
                $node->title = $node_title;
            }
        }
    }

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.