View source
<?php
namespace Drupal\paragraphs_table\Form;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ActionEditFieldForm extends FormBase {
protected $moduleHandler;
protected $nodeTypeStorage;
public function __construct(ModuleHandlerInterface $module_handler, EntityStorageInterface $node_type_storage) {
$this->moduleHandler = $module_handler;
$this->nodeTypeStorage = $node_type_storage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('module_handler'), $container
->get('entity_type.manager')
->getStorage('node_type'));
}
public function getFormId() {
return 'paragraphs_table_edit';
}
public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $entity = NULL, $field_name = NULL) {
if (!$form_state
->has('entity')) {
$this
->init($form_state, $entity, $field_name);
}
$form_state
->get('form_display')
->buildForm($entity, $form, $form_state);
if ($entity instanceof EntityChangedInterface) {
$form['changed_field'] = [
'#type' => 'hidden',
'#value' => $entity
->getChangedTime(),
];
}
$form[$field_name]["widget"]["add_more"]["#access"] = FALSE;
$form[$field_name]['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Save'),
];
$form['#disable_inline_form_errors'] = TRUE;
$this
->simplify($form, $form_state);
return $form;
}
protected function init(FormStateInterface $form_state, EntityInterface $entity, $field_name) {
if ($entity
->getEntityTypeId() == 'node') {
$node_type = $this->nodeTypeStorage
->load($entity
->bundle());
$entity
->setNewRevision($node_type
->shouldCreateNewRevision());
$entity->revision_log = NULL;
}
$form_state
->set('entity', $entity);
$form_state
->set('field_name', $field_name);
$display = EntityFormDisplay::collectRenderDisplay($entity, 'default');
foreach ($display
->getComponents() as $name => $options) {
if ($name != $field_name) {
$display
->removeComponent($name);
}
}
$form_state
->set('form_display', $display);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$entity = $this
->buildEntity($form, $form_state);
$form_state
->get('form_display')
->validateFormValues($entity, $form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity = $this
->buildEntity($form, $form_state);
$form_state
->set('entity', $entity);
if ($entity
->getEntityType()
->hasKey('revision')) {
$entity
->setNewRevision(FALSE);
}
$entity
->save();
$form_state
->disableRedirect(FALSE);
$form_state
->setRedirectUrl(Url::fromRoute('<current>'));
}
protected function buildEntity(array $form, FormStateInterface $form_state) {
$entity = clone $form_state
->get('entity');
$field_name = $form_state
->get('field_name');
$form_state
->get('form_display')
->extractFormValues($entity, $form, $form_state);
if ($entity
->getEntityTypeId() == 'node' && $entity
->isNewRevision() && $entity->revision_log
->isEmpty()) {
$entity->revision_log = t('Updated the %field-name field through in-place editing.', [
'%field-name' => $entity
->get($field_name)
->getFieldDefinition()
->getLabel(),
]);
}
return $entity;
}
protected function simplify(array &$form, FormStateInterface $form_state) {
$field_name = $form_state
->get('field_name');
$widget_element =& $form[$field_name]['widget'];
$num_children = count(Element::children($widget_element));
if ($num_children == 0 && $widget_element['#type'] != 'checkbox') {
$widget_element['#title_display'] = 'invisible';
}
if ($num_children == 1 && isset($widget_element[0]['value'])) {
$widget_element[0]['value']['#title_display'] = 'invisible';
}
if (isset($widget_element[0]['value']['#type']) && $widget_element[0]['value']['#type'] == 'textarea') {
$lines = count(explode("\n", $widget_element[0]['value']['#default_value']));
$widget_element[0]['value']['#rows'] = $lines + 1;
}
}
}