Showing posts with label form api. Show all posts
Showing posts with label form api. Show all posts

Restricting CCK forms in Drupal 6

This article contains a programming howto for stripping unwanted fields from forms generated by the CCK module in Drupal 6. It is natural for a content type to gather as many fields as the logical data schema requires (essentially, the decision about adding a field to an existing content type or creating a new content type, is dictated by considerations about data cardinality and independence in life cycles of individual pieces of data). Real-world applications will usually require that certain subsets of these fields should not be editable by everyone, or that they should only be editable at certain times or when certain conditions are met (for example, based on another field's value within the node being edited). Understandably, CCK doesn't know anything about such special needs out-of-the-box, and so it will render all node fields as equals on a common form.

Consider a case where you have already created a complex default form for editing the entire content type, possibly with field groups and a custom theme which renders them in a fancy hierarchical way. Now let's say you have another user role which is only supposed to understand and edit a few fields from the said content type. You want a new form which displays only those fields and saves the submitted values to the node, validating them in exactly the same way as they would be in the full-node edit scenario. To make things even more interesting, suppose that one of the fields is a select box for which you want to restrict the list of allowed values when it is presented on the restricted form.

The correct solution is short in code, but rather tricky conceptually. It requires at least an intermediate level of understanding of the Drupal Form API and theming layer. The following steps can be used as a guideline:

  1. Choose a path for the new form. It is a logical choice to extend the default node edit path. That is, let /node/1234/edit refer to the default form and let /node/1234/edit/custom refer to your custom form. In your module, implement hook_menu() and copy the item from node.module which refers to /node/%node/edit. At this point you have an opportunity to turn it into a MENU_CALLBACK (if you don't want the new form to be accessible through the standard tabs) and also to restrict access by defining a new access callback. Don't forget to specify 'file path' => drupal_get_path('module', 'node'), otherwise the file node.pages.inc defined by node.module would not be found. The new item should look like so:
      $items['node/%node/edit/custom'] = array(
        'title' => 'Edit custom',
        'page callback' => 'node_page_edit',
        'page arguments' => array(1),
        'access callback' => 'yourmodule_edit_custom_access',
        'weight' => 1,
        'file' => 'node.pages.inc',
        'file path' => drupal_get_path('module', 'node'),
        'type' => MENU_CALLBACK,
      );
    
  2. Implement hook_form_alter() to disable access to unwanted fields on your custom form. You will also probably want to set a distinct theme function for the new form, so that you can arrange the wanted fields in a sensible way. The code in yourmodule_form_alter should look like so:
      if ($form_id == 'yourcontenttype_node_form' && arg(3) == 'custom') {
        $form['#theme'] = 'yourmodule_custom_form';
        $allowed_fields = array('field_first', 'field_second', 'field_third');
        foreach ($form as $key => &$field) {
          if (strpos($key, 'field_') === 0 && !in_array($key, $allowed_fields))
            $field['#access'] = FALSE;
        }
      }
    
    The original form might also contain other form elements that need to be disabled and whose names don't begin with field_. You can find out what they are visually by looking at the form and by dumping the keys of $form. One good example is the buttons key, which contains the submit, preview and delete buttons. It is possible that some form elements contributed by other modules are not yet present when your hook_form_alter() is called. In this case, you will likely have to implement another hook_form_alter() in another module and set this module's weight to a big value in the system table, to ensure that it executes last.
  3. Register the theme function yourcustom_form specified above via hook_theme() and also provide an actual implementation in a function called theme_yourmodule_custom_form, which receives $form as argument. Alternatively, you could declare it in hook_theme() as a template like so:
      'yourmodule_custom_form' => array(
        'arguments' => array('form' => NULL, 'user' => NULL),
        'template' => 'custom_form',
      ),
    
    and then create a template file custom_form.tpl.php in your theme folder.
  4. Within your theme function or template, render the whole form or individual fields like so:
      // Whole form
      // print drupal_render($form);
    
      // Individual fields
      print drupal_render($form['fieldgroup_tabs']['group_somegroup']);
      print drupal_render($form['buttons']['submit']);
    
    In general, if you cannot render the whole form at once because it would result in unwanted artifacts (e.g. unwanted group headers from the original form or some such), it is a good approach to render the wanted fields individually first and then call drupal_render($form) within a display:none div element, which will only output the as-of-yet unrendered fields. This will also ensure that any hidden fields are output at all. Mind the possible security implications; if you failed to set #access to FALSE on some fields in the step above, you'd get invisible, yet still updateable fields (the user might use Firebug to get at them). Accordingly, only rely on the display:none trick for visuals, but not as a method of concealing fields.
  5. In the problem statement a requirement to restrict allowed values for a particular field was mentioned. This can be done through the #afterbuild callback installed on the field in question, like so:
      // In hook_form_alter() if branch: 
      $form['field_first']['#after_build'][] =
         'yourmodule_field_first_after_build';
      // ...
    
      function yourmodule_field_first_after_build($element, &$form_state) {
        unset($element['value']['#options']['']);
        return $element;
      } 
    

A word of caution: if you are using a checkbox CCK field in your content type, you might to run into a bug when you set #access to FALSE, which would prevent the custom form from validating. As a workaround, two patches for optionwidgets.module were available at writing time (pick your favorite).

In general, when working with form data structures, remember that the content of these structures varies considerably depending on the current processing stage within the form engine. (Unfortunately, these variations and the separation between API and internals are rather badly documented, which makes it easy to write unstable code.) It's also good to realize that FAPI's "form elements" are hierarchical in nature and act as a mind-numbingly complicated powerful adapter layer between the database fields and HTML form inputs. In particular do realize that a single FAPI form element might expand into multiple form inputs and/or supply values for mutliple database fields.

For further information, I refer you to the following sources:

Drupal 6: Adding form fields dynamically

There are times when you want to understand how something works, but there are plent more times when you simply want to get things done. When it came to dynamically extending Drupal 6 forms per Ajax/AHAH, I found some half-baked explanations of the "ahah voodoo" (aka implementation details) on the web, references to poll.module as the place which contains such frightening "voodoo", but no simple examples which would provide the usual delights of copy-paste reuse.

The AHAH idea is very simple - call back the server to generate a HTML snippet with a few more form fields to be inserted and subsequently submitted by client. Alas, implementing even such a basic scenario in Drupal 6 is complicated. To improve the matters a little, I implemented a Simple AHAH API module (download link). Example client code for an order form with dynamically added order items is shown below. It is almost self-explanatory (for further instructions, see included README.txt). Overall, it should reduce the implementation time for the most usual case (dynamically adding form fields) from several tiresome hours of debugging to a few minutes of thoughtless fill-in-the-blanks.

function nca_ahah_init() {
  module_load_include('inc', 'nca_ahah', 'nca_ahah');
}

function nca_ahah_menu() {
  $items['nca_ahah/nca_ahah_order_item'] = array(
    'title' => 'Javascript Order Item Form',
    'page callback' => 'nca_ahah_callback',
    'page arguments' => array('nca_ahah_order_item'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  $items['nca_ahah/order'] = array(
    'title' => t('Order items example'),    
    'page callback' => 'drupal_get_form',
    'page arguments' => array('nca_ahah_order_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  
  return $items;
}

function nca_ahah_theme() {
  return array(
    'nca_ahah_order_item' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}

function nca_ahah_order_form($form_state) {
  $form = array();
  
  nca_ahah_wrapper($form, $form_state, 'nca_ahah_order_item');

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Proceed to checkout'),
    '#weight' => 2,
    );

  // Avoid submitting the form to AHAH handler on checkout. 
  $form['#action'] = url('nca_ahah/order');
  
  return $form;
}

function nca_ahah_order_form_submit($form, &$form_state) {
  if ($form_state['clicked_button']['#id'] == 'edit-submit') {
    // Real submit button was clicked.
    dpm($form_state['values']);
  }
  else {
    // "Add more" button was clicked.
  }
}

function nca_ahah_order_form_validate($form_id, &$form_state) { 
  if ($form_state['clicked_button']['#id'] == 'edit-submit') {
    // Real submit button was clicked.
  }
  else {
    // "Add more" button was clicked.
  }
}

function nca_ahah_order_item_form(&$item_form, $i, $item) {
  $item_form['item'] = array(
    '#title' => t('Item name'),
    '#type' => 'select',
    '#options' => array(0 => '', 1 => 'First Item', 2 => 'Second Item', 3 => 'Third Item'),
    '#default_value' => isset($item['item']) ? $item['item'] : 0,
    '#parents' => array('nca_ahah_order_item', $i, 'item'), // unfortunately needed
    );

  $item_form['qty'] = array(
    '#title' => t('Quantity'),
    '#type' => 'textfield',
    '#default_value' => isset($item['qty']) ? $item['qty'] : 0,
    '#size' => 5,
    '#maxlength' => 7,
    '#parents' => array('nca_ahah_order_item', $i, 'qty'), // unfortunately needed
  );
}

function theme_nca_ahah_order_item($item_form) {
  // You can do more fancy layout/HTML formatting here
  return drupal_render($item_form);
}

Drupal 6: Debugging form submissions

This post contains a few notes taken from a debugging session for a problem of the kind "submitted form is being returned to the sender without any error message" in a Drupal 6 installation. Furthermore, it contains some tips on debugging the "form values disappear into the void" symptom. These are surprisingly difficult issues to debug if you don't know where to start, as Drupal's control flow is notoriously convoluted due to indirection caused by hooks (as a matter of fact, quite a few software frameworks pay the same price for their flexibility) and because of generous use of recursion.

  1. Check if form_set_error is called (with a non-empty argument) by any module during processing.
  2. Instrument or break in function form_execute_handlers in form.inc. This is from where the validate and submit functions contributed by various modules are invoked from.
  3. If the form validates, but an expected submit handler is not in the list, it may be due to the form id not matching the function name. If you cannot change either, you can still bring them together in the form_alter hook by appending to #submit, like so:
    $form['#submit'][] = 'user_register_submit';
  4. If the form values in $form_state seem to be wrong or missing, debug the $form['#value'] assignments in _form_builder_handle_input_element (to conditionally break on a particular element, you can evaluate $form['#name']). In particular, for multistep forms pay attention to the #access attribute, which controls whether the values are populated from the form cache or from the POST request.