public function EditableFieldsForm::buildForm in Editable Fields 8
Same name and namespace in other branches
- 1.0.x src/Form/EditableFieldsForm.php \Drupal\editablefields\Form\EditableFieldsForm::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- src/
Form/ EditableFieldsForm.php, line 71
Class
- EditableFieldsForm
- Class EditableFieldsForm.
Namespace
Drupal\editablefields\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$field = $this->field_name;
$form_display = $this
->getFormDisplay();
$is_admin = $this->editablefieldsHelper
->isAdmin();
if (empty($form_display) || !$form_display
->id()) {
if ($is_admin) {
return [
'#markup' => $this
->t('Form mode @mode missing', [
'@mode' => $this->form_mode,
]),
];
}
return [];
}
// Get the field widget from the form mode.
$component = $form_display
->getComponent($field);
if (!$component) {
if ($is_admin) {
return [
'#markup' => $this
->t('The field @field is missing in the @mode', [
'@field' => $field,
'@mode' => $this->form_mode,
]),
];
}
return [];
}
// Add #parents to avoid error in WidgetBase::form.
$form['#parents'] = [];
// Get widget and prepare values for it.
$widget = $form_display
->getRenderer($field);
$items = $this->entity
->get($field);
$items
->filterEmptyItems();
// Get a widget form.
$form[$field] = $widget
->form($items, $form, $form_state);
$form[$field]['#access'] = $items
->access('edit');
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Update'),
'#ajax' => [
'callback' => [
$this,
'ajaxCallback',
],
'wrapper' => str_replace('_', '-', $this
->getFormId()),
],
];
return $form;
}