0

I require some help in my custom module, specifically getting the value of a form element added via ajax in hook_node_submit.

First I'll explain the basic module setup needs:

  • You have created a content type called 'content'.
  • You have created a sample taxonomy vocabulary and added some sample terms to it.
  • In the content type you add a term reference field with the name 'field_terms' for the newly created vocabulary and allow unlimited values (checkboxes).

Now you enable the module and create new content of type 'content'.

Once you select 2 taxonomy terms, a select list appears with the 2 or more selected terms in it, allowing the user to select one of the above selected terms once more (to define a main term from the selected terms - if that makes any sense)

So, in terms of the module I have written this:

1) I use the function hook_form_alter() and apply it to only $form_id == 'content_node_form' (content type = 'content').

2) In hook_form_alter() I apply '#ajax' to each checkbox for the field_term checkboxes and create a blank container where the select list will be placed after the callback.

3) I have written the function _custom_callback() which gets the selected checkbox term_ids and term_names from the field_terms to create the select list options. It then outputs the select list if 2 or more options have been selected

4) Implement hook_node_submit() to grab the data from the $form_state and move it into the node object (this is where the first errors occur)

5) Implement hook_node_load() to load the values when editing the page

6) Impmeneted hook_node_insert(), hook_node_update() and hook_node_delete() to do the db stuff.

But when I submit the form I receive the following errors - due to hook_node_submit:

errors

I am unsure how to call my object in the node. My form is called

$form['container']['main_taxonomy_term']

So I presumed this would be correct:

/**
 * Implements hook_node_submit().
 */
function select_main_taxonomy_term_node_submit($node, $form, &$form_state) {

  $value = $form_state['values'];
  // Move the new data into the node object.
  $node->main_taxonomy_term['tid'] = $value['main_taxonomy_term'];
}

I have looked and tried but I just can not find any clues how to get the data from $form_state and into the node object.

Please find attached the module. maybe someone can help me out as this is driving me insane.

Here is the module:

http://www.megaupload.com/?d=181H4B5F

Many thanks in advance.

1
  • Ok, I have found out why the field would not show up at all. I defined the $form['container'] in the hook_form_alter() function, but the actual $form['container']['main_taxonomy_term'] in the callback function. I have now moved it into the hook_form_alter function and the value is displayed in $form_status. However, now I have the problem of trying to optain the options for the select menu from the selected checkboxes ...
    – tecjam
    Dec 7, 2011 at 12:56

2 Answers 2

1

So now I can answer my own question:

In my module I had $form['container'] defined in the hook_form_alter(), but the actual field $form['container']['main_taxonomy_term'] wasn't defined yet as I was adding it in my callback, so naturally Drupal didn't know about the field when rendering the form.

I restructured the module, moved the $form['container']['main_taxonomy_term'] = array(...) into the hook_form_alter(), created a new function to get the select options, calling upon it in various circumstances, added some extra checks for the edit node form as the options had to be built from the database values upon first display, but after that use $form_state else it wouldn't refresh the list upon editing the checkbox selection...

Oh well, so i finally managed to get it working without error message.

Maybe this is helpful for anyone, or maybe it isn't at all.

0

After much finagling from my post above, I found myself confronted with your original problem. I had additional fields that were dependent on the clicks of a particular checkboxes field. I was able to grab the value for a hook_node_insert function below by making the dependent fields hidden until a check was made on the original. Here is a snippet if it could help anyone:

  if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
      $form['topwrap'] = array(
            '#type'     => 'markup',
            '#prefix' => '<div id="gunge">',
            '#suffix' => '</div>',
      );
  }
  if (empty($form_state['values']['subject']) && empty($form_state['values']['grade'])) {
      $form['topwrap'] = array(
            '#type'     => 'hidden',
            '#prefix' => '<div id="gunge">',
            '#suffix' => '</div>',
      );
  }
  $form['topwrap']['topblock'] = array(
            '#type'   => 'fieldset',
            '#title'  => 'Topics',
  ); 
  $form['topwrap']['topblock']['topics'] = array(
        '#type'     => 'checkboxes',
        '#options' => grab_topics($subjids, $gradids, $strandids),
        '#prefix' => '<div style="background:#FFAA00;">',
        '#suffix' => '</div>',
  );

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.