0

So, here's my issue. I have a university course page. I want to display a list of students currently weight listed for the course, and give the option for the student to add their name to the weight list.

I've outputted the current student weight list through a custom template file just fine. I've also made a one button form through a custom module that simply calls a submit function. Then I call that form on my node template. That part works all fine.

Then, all I need for this form to work is the current User's UID (which can be called easily from my form submit function in my module). And the NID that their name should be attached to. Where I'm having difficulty is the NID part. I can't figure out how I can add that to my custom form built through my module with Form API.

Right now, I'm setting a variable and calling that variable in the submit function.The problem there is it will screw up the system if more than one student is looking at different pages and submits it. So there needs to be way that I can set the current NID that the form sits on, then call it through my submit handler.

I've been searching google up and down for this, and I can't figure out how to set a hidden field with the current NID the form is on.

Any help would be greatly appreciated.

Edit: Here is my code currently for reference:

function wl_add_form($form_state) {


 $form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Add Name to List'),
  '#submit' => array('wl_add_form_submit_function'),
);

  return $form;
}


//Add me to weight list functions 
function wl_add_form_submit_function($form, &$form_state) {

//dpm($form);
  //Handle the student add
  global $user;
  //get the User ID
  $userID = $user->uid;
  //get the NID from the node variable we set in the node-course template: 

  $nid= variable_get($wlNID); 
  $node = node_load($nid);
  $count = count($node->field_course_weight_student);
  $node->field_course_weight_student[$count]['uid'] = $userID; // another user
  node_save($node);
  drupal_set_message(t('Your name has been added to the weight list'));



}
2

Just save the node ID in $_SESSION.

Using variable_set() has another effect, apart saving a value that is valid for all the users: It clears the cache Drupal uses for the Drupal variables, with the effect they are reloaded from the database the next time Drupal bootstraps. Compare the code used by variable_set() and variable_initialize(). (Similar code is executed by Drupal 6.)

  db_merge('variable')->key(array('name' => $name))->fields(array('value' => serialize($value)))->execute();

  cache_clear_all('variables', 'cache_bootstrap');

  $conf[$name] = $value;
  // NOTE: caching the variables improves performance by 20% when serving
  // cached pages.
  if ($cached = cache_get('variables', 'cache_bootstrap')) {
    $variables = $cached->data;
  }
  else {
    // Cache miss. Avoid a stampede.
    $name = 'variable_init';
    if (!lock_acquire($name, 1)) {
      // Another request is building the variable cache.
      // Wait, then re-run this function.
      lock_wait($name);
      return variable_initialize($conf);
    }
    else {
      // Proceed with variable rebuild.
      $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')->fetchAllKeyed());
      cache_set('variables', $variables, 'cache_bootstrap');
      lock_release($name);
    }
  }

  foreach ($conf as $name => $value) {
    $variables[$name] = $value;
  }

As for an example of how to use $_SESSION in a form, see node_filter_form(), the form builder function that handles the form that allows to filter nodes in admin/content/node.

screenshot

  $session = isset($_SESSION['node_overview_filter']) ? $_SESSION['node_overview_filter'] : array();
  $filters = node_filters();

  $i = 0;
  $form['filters'] = array(
    '#type' => 'fieldset',
    '#title' => t('Show only items where'),
    '#theme' => 'exposed_filters__node',
  );
  foreach ($session as $filter) {
    list($type, $value) = $filter;
    if ($type == 'term') {
      // Load term name from DB rather than search and parse options array.
      $value = module_invoke('taxonomy', 'term_load', $value);
      $value = $value->name;
    }
    elseif ($type == 'language') {
      $value = $value == LANGUAGE_NONE ? t('Language neutral') : module_invoke('locale', 'language_name', $value);
    }
    else {
      $value = $filters[$type]['options'][$value];
    }
    $t_args = array(
      '%property' => $filters[$type]['title'],
      '%value' => $value,
    );
    if ($i++) {
      $form['filters']['current'][] = array('#markup' => t('and where %property is %value', $t_args));
    }
    else {
      $form['filters']['current'][] = array('#markup' => t('where %property is %value', $t_args));
    }
    if (in_array($type, array('type', 'language'))) {
      // Remove the option if it is already being filtered on.
      unset($filters[$type]);
    }
  }

  $form['filters']['status'] = array(
    '#type' => 'container',
    '#attributes' => array('class' => array('clearfix')),
    '#prefix' => ($i ? '<div class="additional-filters">' . t('and where') . '</div>' : ''),
  );
  $form['filters']['status']['filters'] = array(
    '#type' => 'container',
    '#attributes' => array('class' => array('filters')),
  );
  foreach ($filters as $key => $filter) {
    $form['filters']['status']['filters'][$key] = array(
      '#type' => 'select',
      '#options' => $filter['options'],
      '#title' => $filter['title'],
      '#default_value' => '[any]',
    );
  }

Its form submission handler sets $_SESSION with the values entered by the user.

  // Apply every filter that has a choice selected other than 'any'.
  foreach ($filters as $filter => $options) {
    if (isset($form_state['values'][$filter]) && $form_state['values'][$filter] != '[any]') {
      // Flatten the options array to accommodate hierarchical/nested options.
      $flat_options = form_options_flatten($filters[$filter]['options']);
      // Only accept valid selections offered on the dropdown, block bad input.
      if (isset($flat_options[$form_state['values'][$filter]])) {
        $_SESSION['node_overview_filter'][] = array($filter, $form_state['values'][$filter]);
      }
    }
  }
  • Unfortunately, I'm a bit confused there as well. How can add a _session variable through the Form builder API? I can't seem to find any documentation on it. I'm sure this is simple, I'm just having difficulty understanding how to include this in my module. There seems to be alot of Documentation on this for Drupal 7, I'm having trouble with Drupal 6 though. – DrupalMonster Aug 15 '13 at 23:52
  • PHP session is handled by PHP; you just set $_SESSION[<variable_used_from_your_module>] and read it when necessary. The only difference with plain PHP is that Drupal saves the session content in its database (the table is sessions), when the session expires. – kiamlaluno Aug 16 '13 at 0:24
  • Hmmm, this does help. However, you bringing this up, just sparked an idea for a super easy solution for this. Hold on, let me test. – DrupalMonster Aug 16 '13 at 2:11

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.