我正在创建一个自定义实体配置,除#default_values“属性不起作用外,其他一切都很好。我尝试了几种方法解决该问题,即买东西没有任何作用。
不起作用的字段是循环内的以下字段:“宽度”,“高度”和“设备”
附上我的Php代码。
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$default_entity = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#maxlength' => 255,
'#default_value' => $default_entity->label(),
'#description' => $this->t("Label for the Default entity."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $default_entity->id(),
'#machine_name' => [
'exists' => '\Drupal\publicity\Entity\DefaultEntity::load',
],
'#disabled' => !$default_entity->isNew(),
];
$form['url']=[
'#type'=>'url',
'#title'=>t('Url'),
'#description'=>'Add a custom url for your configuration',
'#default_value'=>$default_entity->get('url'),
];
$form['id_publicity']=[
'#type'=>'textfield',
'#title'=>$this->t('ID Publicity'),
'#default_value'=> $default_entity->get('id_publicity'),
'#placeholder'=>$this->t('Ej: XUY-146'),
];
$form['render_section']=[
'#type'=> 'select',
'#default_value'=> $default_entity->get('render_section'),
'#options'=>[
'Home Page','Article','Sections',
],
'#empty_option'=>'Render Sections',
];
// Gather the number of breakpoints in the form already.
$fields = $form_state->get('fields');
// We have to ensure that there is at least one name field.
if ($fields === NULL) {
$name_field = $form_state->set('fields', 1);
$fields = 1;
}
$form['#tree'] = TRUE;
$form['breakpoints'] = [
'#type' => 'fieldset',
'#title' => $this->t('RENDERED'),
'#prefix' => '<div id="breakpoints-fieldset-wrapper">',
'#suffix' => '</div>',
];
for ($i = 0; $i < $fields; $i++) { $form['breakpoints']['subfieldset'][$i] = [ '#type' => 'fieldset', '#title' => $this->t('RESPONSIVE DESIGN'), ]; $form['breakpoints']['subfieldset'][$i]['width'] = [ '#type' => 'number', '#title' => $this->t('Width'), '#placeholder'=>$this->t('Ej: 720px'), '#default_value'=> $default_entity->get('width'), ]; $form['breakpoints']['subfieldset'][$i]['height'] = [ '#type' => 'number', '#title' => $this->t('Height'), '#placeholder'=> $this->t('Ej: 1080px'), '#default_value'=> $default_entity->get('height'), ]; $form['breakpoints']['subfieldset'][$i]['device']=[ '#type'=>'select', '#options'=>[ 'Desktop','Mobile','Tablet', ], '#title'=>t('Device'), '#default_value'=> $default_entity->get('device'), '#placeholder'=>$this->t('Custom Devices'), '#empty_option'=>$this->t('Devices List'), ]; } $form['breakpoints']['actions'] = [ '#type' => 'actions', ]; $form['breakpoints']['actions']['add_name'] = [ '#type' => 'submit', '#value' => $this->t('Add one more'), '#submit' => ['::addOne'], '#ajax' => [ 'callback' => '::addmoreCallback', 'wrapper' => 'breakpoints-fieldset-wrapper', ], ]; // If there is more than one name, add the remove button. if ($fields > 1) { $form['breakpoints']['actions']['remove_name'] = [ '#type' => 'submit', '#value' => $this->t('Remove one'), '#submit' => ['::removeCallback'], '#ajax' => [ 'callback' => '::addmoreCallback', 'wrapper' => 'breakpoints-fieldset-wrapper', ], ]; } $form['breakpoints']['actions']['submit'] = [ '#type' => 'submit', '#value' => $this->t('Submit'), ]; return $form; }
*
我需要默认值属性由用户或填写配置表格的人带来所选值。