Drupal 8

时间:2018-02-13 14:36:07

标签: drupal drupal-8 drupal-forms

我试图通过这样的方式将checkboxes元素包裹在details中:

function pf_form_user_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $config = \Drupal::config('pf.settings.notifications');
    $form['pf'] = [
        '#type' => 'details',
        '#title' => t('Notification for updates in specific languages'),
        '#description' => t('Expect getting one to two emails weekly'),
        '#open': true,
    ];
    $form['pf']['pf.notifications.checkboxes'] = [
        '#type' => 'checkboxes',
        '#title' => t('Check the ones you\'d like to recieve!'),
        '#options' => [
            'de' => t('german'),
            'en' => t('english'),
        ],
        '#default_value' => [
            'de' => $config->get('de'),
            'en' => $config->get('en'),
        ],
    ];

    $form['actions']['submit']['#submit'][] = 'pf_form_user_form_submit';
}

但在提交时,我会不断回来$values = ['de'=>0, 'en'=>0]

function pf_form_user_form_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $values = $form_state->getValue('pf.notifications.checkboxes');

    $config = \Drupal::config('pf.settings.notifications');
    $config
        ->set('de', $values['de'])
        ->set('en', $values['en'])
        ->save()
    ;
}

一旦我不使用包装details表单元素,就会出现数据(已检查元素的值==键)。像这样:

function pf_form_user_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    ....
    // $form['pf'] = [   commented out
    $form['pf.notifications.checkboxes'] = ...
    ....

使用调试器检查$form_state显示相同的内容。第一种情况为零,第二种情况为正数。

我错过了什么吗?表单元素的分组如何工作?

1 个答案:

答案 0 :(得分:0)

我通过调试器更彻底地检查了$form_state,并且我注意到一些键是原始的,带有点'pf.notifications.checkboxes',而在其他地方,替换了下划线,键是{ {1}}。

使用下划线。