简体   繁体   English

试图以自定义模块形式在drupal中添加“字段组”和“添加更多”

[英]Trying to add 'group of fields' with 'add more' in drupal in a custom module form

I have been trying writing a custom module in drupal 7 with a form to have a group of fields with 'add more' option as in 'Field Collection'(don't want to use module but write the code for control). 我一直在尝试用一种形式在drupal 7中编写一个自定义模块,使其具有一组带有“添加更多”选项的字段,如“字段集合”中一样(不想使用模块,而是编写用于控制的代码)。 I tried to find a good example but no success. 我试图找到一个很好的例子,但没有成功。 Can anybody suggest how to proceed? 有人可以建议如何进行吗? Plz!! 请!

Thanks in advance. 提前致谢。

$form['education']['languages'] = array(
        '#type' => 'fieldset',
        '#title' => t('Languages'),
        '#prefix' => '<div id="div_languages-fieldset-wrapper">',
        '#suffix' => '</div>',
    );

    for ($i = 0; $i < $form_state['language_num']; $i++) 
    {   
        $form['education']['languages'][$i]['labels'] = array(
            '#prefix' => '<h1 id = "stu_form_label">',
            '#type' => 'item',
            '#title' => t('Form :'.($i+1)),
            '#suffix' => '</h1>',
        );

        // Language Drop down box
        $option_language = _get_language_mstr();
        $form['education']['languages'][$i]['language'] = array(
            '#prefix' => '<div class="container-inline bottom_space languages_drop_down">',
            '#options' => $option_language,
            '#type' => 'select',
            '#title' => t('Language'),
            '#suffix' => '</div>',
        );

        // Level Drop down box
        $option_Level = _get_level_mstr();
        $form['education']['languages'][$i]['level'] = array(
            '#prefix' => '<div class="container-inline bottom_space languages_drop_down">',
            '#options' => $option_Level,
            '#type' => 'select',
            '#title' => t('Level'),
            '#suffix' => '</div>',
        );  
    }
    if ($form_state['language_num'] > 1) 
    {
        $form['education']['languages']['remove_name'] = array(
            '#type' => 'submit',
            '#value' => t('Remove last language'),
            '#limit_validation_errors' => array(),
            '#submit' => array('remove_one_language'),              
            '#ajax' => array(
                'callback' => 'add_more_callback_language',
                'wrapper' => 'div_languages-fieldset-wrapper',
            ),
        );
    }

    $form['education']['languages']['add_name'] = array(
        '#type' => 'submit',
        '#value' => t('Add language'),
        '#limit_validation_errors' => array(),
        '#submit' => array('add_one_language'),
        '#ajax' => array(
            'callback' => 'add_more_callback_language',
            'wrapper' => 'div_languages-fieldset-wrapper',
        ),
    );
    function add_more_callback_language($form, $form_state) {
       return $form['education']['languages'];
     }`

Try this method 试试这个方法

Sample Link: URL 样本链接: URL

Click the tab education 点击标签教育

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM