以编程方式更改 CCK 文本字段

发布于 2024-10-07 07:47:51 字数 928 浏览 0 评论 0原文

我正在尝试将“显示”复选框添加到多个 cck 文本字段。下面的代码确实添加了该字段,但是当我提交表单时,在验证表单时我看不到 $form_state 数组中的值。

我可能做错了什么?

<?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']) as $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');
}

?>

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']) as $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');
}

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的憧憬 2024-10-14 07:47:51

即使在阅读了 hook_form_alter() 和 CCK 字段之后,仍然了解在 CCK 上使用 hook_form alter 时要注意的陷阱我仍然被难住了,因为我基本上就是在做他们提供的事情。

我最终决定尝试另一种方法,以下更改对我有用:

<?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;
}

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;
}
蓦然回首 2024-10-14 07:47:51

像 #after_build 函数中那样改变形式是一个坏主意。虽然表单元素可能会出现在页面上,但它们不会作为表单的一部分进行处理。

您应该能够通过直接的形式更改和自定义模块的高模块重量来实现您想要的目标。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文