在 Drupal 7 中,如何显示可以编辑从不同表单中选择的数据的新表单?

发布于 2024-09-12 17:20:16 字数 2837 浏览 6 评论 0原文

我有一个表选择表单,其中列出了几个项目。当用户选择一个或多个项目并单击编辑按钮时,我希望显示一个新表单以允许他们编辑项目。

我正在生成新的表单结构,但单击编辑按钮后无法显示它。

目前,似乎什么也没有发生。我知道 tableselect 表单已正确提交,并且创建编辑术语表单的功能工作正常。我用 drupal_set_message 和 var 导出对其进行了测试。

那么,如何才能显示新表单呢?

这是我的相关代码:

/**
 * Generate form for listing terms
 */
function markit_form_terms_list()
{

    $form = array();
    $form['terms'] = array(
        '#type' => 'fieldset',
        '#title' => t('List of Terms'),
        );
    $header = array(t('Name'), t('ID'), t('SetID'));
    $form['terms']['items'] = array(
          '#type' => 'tableselect',
          '#header' => $header,
          '#title' => t('Terms'),
          '#options' => markit_get_array_terms(),//drupal_map_assoc($header, 'markit_get_array_terms'),
          '#tree' => TRUE,

    );
    $form['terms']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Edit Term'),
        '#submit' => array('markit_form_terms_list_submit'),
        );
    /*$form['terms']['delete'] = array(
        '#type' => 'submit',
        '#value' => t('Delete Term'),
        '#submit' => 'markit_form_terms_delete'
        );*/
    return $form;
}

/**
 * Generate form to edit the terms.
 * @param <type> $form
 * @param <type> $form_state
 * @return string
 */
function markit_form_term_edit($form, $form_state)
{
    $newform = array();
    $newform['termstoedit'] = array(
        '#type' => 'fieldset',
        '#title' => t('Edit Term/s'),
        );
    foreach($form_state['values']['items'] as $row)
    {
        if($row!=0)//if a row is not selected, it will be 0. So don't select rows equal to 0.
        {
            $terminfo = markit_get_markterms($row);
            drupal_set_message(var_export($terminfo,true));//['term_name']);
            drupal_set_message($terminfo[0]['term_name']);
            $newform['termstoedit'][$terminfo[0]['term_id']] = array(
              '#type' => 'textfield',
              '#title' => t('Term:'),
              '#default_value' => $terminfo[0]['term_name'],
              '#size' => 60,
              '#maxlength' => 128,
              '#required' => TRUE,
            );
        }
    }    
    $newform['termstoedit']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Edit Term'),
        '#submit' => array('markit_form_term_edit_submit'),
        );
    drupal_set_message(var_export($newform,true));
    return $newform;
}

function markit_form_terms_list_submit($form,$form_state)
{
    drupal_set_page_content(drupal_build_form('markit_form_term_edit', $form_state));
}

我相信我没有在 markit_form_terms_list_submit 函数中使用正确的代码。我尝试了几种不同的方法,但还没有奏效。我所做的谷歌搜索也没有帮助。我还广泛搜索了 Drupal API 和 Drupal Form API 站点。

无论如何,我认为这就是您可能需要帮助我的所有信息。提前致谢!

I have a tableselect form that lists several items. When a user selects one or more items, and clicks the edit button, I want a new form to show up that lets them edit the items.

I have the new form structure being generated, but I can't get it to show up after the edit button is clicked.

Currently, nothing seems to happen. I know that the tableselect form is being submitted correctly, and the function to create the edit term form is working correctly. I tested it with drupal_set_message and var export.

So, how do I get the new form to show?

Here is my relevant code:

/**
 * Generate form for listing terms
 */
function markit_form_terms_list()
{

    $form = array();
    $form['terms'] = array(
        '#type' => 'fieldset',
        '#title' => t('List of Terms'),
        );
    $header = array(t('Name'), t('ID'), t('SetID'));
    $form['terms']['items'] = array(
          '#type' => 'tableselect',
          '#header' => $header,
          '#title' => t('Terms'),
          '#options' => markit_get_array_terms(),//drupal_map_assoc($header, 'markit_get_array_terms'),
          '#tree' => TRUE,

    );
    $form['terms']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Edit Term'),
        '#submit' => array('markit_form_terms_list_submit'),
        );
    /*$form['terms']['delete'] = array(
        '#type' => 'submit',
        '#value' => t('Delete Term'),
        '#submit' => 'markit_form_terms_delete'
        );*/
    return $form;
}

/**
 * Generate form to edit the terms.
 * @param <type> $form
 * @param <type> $form_state
 * @return string
 */
function markit_form_term_edit($form, $form_state)
{
    $newform = array();
    $newform['termstoedit'] = array(
        '#type' => 'fieldset',
        '#title' => t('Edit Term/s'),
        );
    foreach($form_state['values']['items'] as $row)
    {
        if($row!=0)//if a row is not selected, it will be 0. So don't select rows equal to 0.
        {
            $terminfo = markit_get_markterms($row);
            drupal_set_message(var_export($terminfo,true));//['term_name']);
            drupal_set_message($terminfo[0]['term_name']);
            $newform['termstoedit'][$terminfo[0]['term_id']] = array(
              '#type' => 'textfield',
              '#title' => t('Term:'),
              '#default_value' => $terminfo[0]['term_name'],
              '#size' => 60,
              '#maxlength' => 128,
              '#required' => TRUE,
            );
        }
    }    
    $newform['termstoedit']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Edit Term'),
        '#submit' => array('markit_form_term_edit_submit'),
        );
    drupal_set_message(var_export($newform,true));
    return $newform;
}

function markit_form_terms_list_submit($form,$form_state)
{
    drupal_set_page_content(drupal_build_form('markit_form_term_edit', $form_state));
}

I believe I am not using the correct code in the markit_form_terms_list_submit function. I've tried several different things, but it hasn't worked yet. And the Google searches I've done haven't helped either. I also have searched the Drupal API and Drupal Form API sites extensively.

Anyway, I think that's all the info you might need in order to help me. Thanks in advance!

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

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

发布评论

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

评论(1

此刻的回忆 2024-09-19 17:20:16

我相信,如果您有一个表单函数,并且根据输出显示不同的字段,您可能会运气更好。

如果您这样做,您甚至可以使用表单 ajax 方法来自动更新您的表单。

请查看此指南,看看您认为该方法是否适合您。

I believe you may have better luck if you have one form function with differing fields being shown depending on the output.

If you do this you will even be able to use the form ajax methods to auto update your form.

Have a look at this howto to see if you think the approach will work for you.

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