开发者

Programmatically altering CCK textfield

I'm trying to add a 'Display' check box to multiple cck textfield. The code below does add the field, but when I submit the form I do not see the values in the $form_state array when I'm validating the form.

What could I be doing wrong?

<?php

/**
 * Implementation of hook_form_FORM_ID_alter().
 */
function leghist_form_paleghist_node_form_alter(&$form, &$form_state) {

  //Add function to manipulate cck node form
  $form['#after_build'][] = 'leghist_cck_after_build';
}


/**
 * Implementation of hook after_build();
 */
function leghist_cck_after_build ($form, &$form_state) {

  //Add display option to titles
  foreach (element_children($form['field_lg_pop_names']) a开发者_如何转开发s $key) {

    $form['field_lg_pop_names'][$key]['display'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display'),
      '#process' =>  array('leghist_display_process')
    );

  }

  return $form;
}

/**
 * Implementation of hook element_process
 */
function leghist_display_process($element) {
  dsm('yuk');
}

?>


Even after reading hook_form_alter() and CCK fields on the pitfalls to watch-out for when using hook_form alter on CCK I was still stumped, because I'm essentially doing exactly what they offer.

I finally decided to try another approach, and the following changes worked for me:

<?php

//Changed from hook_form_FORM_ID_alter to  hook_form_alter
/**
 * Implementation of hook_form_FORM_ID_alter().
 */
function leghist_form_alter(&$form, &$form_state, $form_id) {

  if ($form_id == 'paleghist_node_form') {
    //Add function to manipulate cck node form
    leghist_cck_alter($form);

  }

}


/**
 * Adds Element to $form
 */
function leghist_cck_alter (&$form) {

  //Add display option to titles
  foreach (element_children($form['field_lg_pop_names']) as $key) {

    $form['field_lg_pop_names'][$key]['display'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display'),
      '#process' =>  array('leghist_display_process')
    );

  }

}

/**
 * Implementation of hook element_process
 */
function leghist_display_process ($element) {

  return $element;
}


Altering the form like that in an #after_build function is a bad idea. While the form elements may appear on the page, they will not be processed as part of the form.

You should be able to achieve what you want with a straight form alter and a high module weight for your custom module.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜