1

I have a funny problem with Drupal 7 and Views.

I created programatically a simple form with two submit buttons (yes / no). This form is displayed on a full view of a node and when the 'yes' is pressed, it goes to a 'review' form which autofills the title with the one from the node. It works very well!

function mymodule_form($form, &$form_state,$nodeid) {
  global $user;
  $form['nodeid'] = array(
    '#type' => 'value',
    '#value' => $nodeid,
    '#access' => false,
  );

  $form['yes_button'] = array(
    '#type' => 'submit',
    '#value' => t('Yes'),
    '#submit' => array('yes_button_submit'),
  );

  $form['no_button'] = array(
    '#type' => 'submit',
    '#value' => t('No'),
    '#submit' => array('no_button_submit'),
  );
  return $form;
}

//Submit handlers
function yes_button_submit($form, &$form_state) {
    $nodeid = $form_state['values']['nodeid'];
    drupal_goto('node/add/review', array('query'=>array('id'=>$nodeid)));
}

I decided to add the buttons on the teasers that I display in a Views block. I call the form through a function which is called via a Global Php field. Here is the code inside the Global Php field, having added 'content: nid' as a hidden field in the view:

<?php
  $bling=mymodule_answers($row->nid);
  echo $bling;
?>

With the bit of code from mymodule_answers:

function mymodule_answers($nodeid) {
  $theform3 = drupal_get_form('mymodule_form',$nodeid);
  $output .= '<p class="text-center over">'.drupal_render($theform3).'</p>';

  return $output;
}

My problem is that if the view is something like this:

  • Node 1
  • Node 2
  • Node 3
  • Node 4

If I press 'yes' on any of the node teasers 2 to 4, then the review form is filled with the title of Node 1... meaning, the one at the top of the list!

I have thought about creating uniquely named forms, but I wouldn't know how to automatically generate the differently named submit handlers. Any ideas?

4
  • Which Drupal version you're using? Oct 29, 2016 at 9:45
  • Hi Cyclone, I added the version (7) in the post. Oct 29, 2016 at 10:01
  • Best way is to simply tag the question with the version =) Oct 29, 2016 at 10:29
  • The main question is then, how did you implement the logic for the auto-filling of the title? In general, it would be good to see some code to understand the problem a bit better. Cheers Oct 29, 2016 at 12:46

1 Answer 1

2

After looking for a while on the Internet, I found a solution.

Firstly, you can add a '#name' to your submit buttons to distinguish them, and then do a switch case in the submit handler... I was trying this for a while before realising that it was not my problem.

My problem consists in the fact that I am calling the same form multiple times on the same page. I found a previous post which highlighted the fact that each form should get a unique form_id in order for it to work. Using hook_forms was the best method for this. I followed the instructions from here. It worked despite it being for Drupal 6.

Amended code... I added this hook:

function mymodule_forms($form_id,$args){
  //Check if the form_id passed to drupal_get_form() contains the string 'mymodule_form'
  if (strpos($form_id, 'mymodule_form') !== FALSE) {
     //Lets attach mymodule_form to $forms[$form_id].  This effectively allows you to use the same form builder function to build a form with any form_id of your choice.
     $forms[$form_id] = array(
       'callback' => 'mymodule_form',
     );
     return $forms;
  }
 }

And then, the code that calls the form was amended to this:

function mymodule_answers($nodeid) {
   $theform3 = drupal_get_form('mymodule_form_'.$nodeid,$nodeid);;
   $output .= '<p class="text-center over">'.drupal_render($theform3).'</p>';

   return $output;
}

Voila!

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.