View source
<?php
namespace Drupal\editablefields\Form;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\BaseFormIdInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EditableFieldsForm extends FormBase implements BaseFormIdInterface {
protected $entity;
protected $field_name;
protected $form_mode;
protected $editablefieldsHelper;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->editablefieldsHelper = $container
->get('editablefields.helper');
return $instance;
}
public function getFormId() {
return $this
->getBaseFormId() . '_' . $this
->prepareUniqueFormId();
}
public function getBaseFormId() {
return 'editablefields_form';
}
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 [];
}
$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 [];
}
$form['#parents'] = [];
$widget = $form_display
->getRenderer($field);
$items = $this->entity
->get($field);
$items
->filterEmptyItems();
$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;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state
->setRebuild(TRUE);
$entity = $this->entity;
$field = $this->field_name;
$form_display = $this
->getFormDisplay();
if (!$form_display || !$form_display
->id()) {
return;
}
if ($component = $form_display
->getComponent($field)) {
$widget = $form_display
->getRenderer($field);
$items = $entity
->get($field);
$items
->filterEmptyItems();
$widget
->extractFormValues($items, $form, $form_state);
$entity
->save();
}
}
public function ajaxCallback(array &$form, FormStateInterface $form_state) {
if (!$form_state
->getErrors()) {
$form['confirm_message'] = [
'#markup' => $this
->t('Updated'),
];
}
return $form;
}
public function getFormDisplay() {
return $this->editablefieldsHelper
->getFormDisplay($this->entity, $this->form_mode);
}
public function setDefaults(EntityInterface $entity, $field_name, $form_mode = 'default') {
$this->entity = $entity;
$this->field_name = $field_name;
$this->form_mode = $form_mode;
}
public function prepareUniqueFormId() {
$entity = $this->entity;
$parts = [
$entity
->getEntityTypeId(),
$entity
->bundle(),
$entity
->id(),
$this->field_name,
$this->form_mode,
];
return implode('_', $parts);
}
}