我正在使用drupal 8,我有一个Entity,我想在Entity Form中添加一个隐藏的类型字段。我如何添加隐藏字段类型?如下所示
<form>
<input type='hidden' name='my_hidden' />
</form>
代码生成表格如下:
public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
{
$fields = parent::baseFieldDefinitions($entity_type);
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Timeslot entity.'))
->setReadOnly(TRUE);
return $fields;
}
答案 0 :(得分:4)
在drupal 8实体表单中隐藏字段有两个步骤。
答案 1 :(得分:0)
如果您只使用UI,则可以在&#34;管理表单显示&#34;中禁用字段的显示。但我认为你要求自己编码的表格......就像那样:
$form['my_hidden'] = array(
'#type' => 'hidden',
'#value' => $my_hidden_value,
);
答案 2 :(得分:0)
你可以试试这个:
public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
{
$fields = parent::baseFieldDefinitions($entity_type);
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Timeslot entity.'))
->setReadOnly(TRUE)
->setDisplayOptions('form', array(
'type' => 'hidden',
'weight' => -5,
);
return $fields;
}
答案 3 :(得分:0)
您可以像定义任何其他字段一样创建要隐藏的字段,然后在实体表单的buildForm方法中设置以下内容
$form['field_name']['widget'][0]['value']['#type'] = 'hidden';
这将使该字段成为隐藏字段
答案 4 :(得分:0)
将 '#access' => FALSE,
添加到表单元素数组中。