Drupal-将表单字段类型从单选更改为复选框

时间:2019-04-15 03:14:20

标签: php drupal drupal-8 drupal-modules

目前,我正在从事Drupal 8项目。我必须将现有的表单字段类型从单选(单选)更改为多选(复选框)。

我在下面更改了

$form['job_type'] = [
 '#type' => 'radio',
  '#title' => $this->t('I am looking for'),
  '#options' => [
    'Full-time' => $this->t('Full Time'),
    'Part-time' => $this->t('Part Time'), 
    'Casual' => $this->t('Casual'),
    'All-of-the-above'=>$this->t('All of the above')
  ],
  '#weight' => '0',
  '#required'=>true,
];

$form['job_type'] = [
 '#type' => 'checkboxes',
  '#title' => $this->t('I am looking for'),
  '#options' => [
    'Full-time' => $this->t('Full Time'),
    'Part-time' => $this->t('Part Time'), 
    'Casual' => $this->t('Casual'),
    'All-of-the-above'=>$this->t('All of the above')
  ],
  '#weight' => '0',
  '#required'=>true,
];

这是保存代码的样子

$profile->set('field_job_type', $form_state->getValue('job_type'));
$profile->save();

提交表单后,$form_state->getValue('job_type')打印正确的选择,但是当我使用$profile->get('field_job_type')->getValue()获得值时,它将返回空数组。

任何帮助将不胜感激!

0 个答案:

没有答案