View source
<?php
namespace Drupal\inline_entity_form\Form;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Render\Element;
use Drupal\inline_entity_form\InlineFormInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityInlineForm implements InlineFormInterface {
use StringTranslationTrait;
protected $entityFieldManager;
protected $entityTypeManager;
protected $entityType;
protected $moduleHandler;
public function __construct(EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, EntityTypeInterface $entity_type) {
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->entityType = $entity_type;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($container
->get('entity_field.manager'), $container
->get('entity_type.manager'), $container
->get('module_handler'), $entity_type);
}
public function getEntityType() {
return $this->entityType;
}
public function getEntityTypeLabels() {
$lowercase_label = $this->entityType
->getSingularLabel();
return [
'singular' => $lowercase_label,
'plural' => $this
->t('@entity_type entities', [
'@entity_type' => $lowercase_label,
]),
];
}
public function getEntityLabel(EntityInterface $entity) {
return $entity
->label();
}
public function getTableFields($bundles) {
$definitions = $this->entityFieldManager
->getBaseFieldDefinitions($this->entityType
->id());
$label_key = $this->entityType
->getKey('label');
$label_field_label = $this
->t('Label');
if ($label_key && isset($definitions[$label_key])) {
$label_field_label = $definitions[$label_key]
->getLabel();
}
$bundle_key = $this->entityType
->getKey('bundle');
$bundle_field_label = $this
->t('Type');
if ($bundle_key && isset($definitions[$bundle_key])) {
$bundle_field_label = $definitions[$bundle_key]
->getLabel();
}
$fields = [];
$fields['label'] = [
'type' => 'label',
'label' => $label_field_label,
'weight' => 1,
];
if (count($bundles) > 1) {
$fields[$bundle_key] = [
'type' => 'field',
'label' => $bundle_field_label,
'weight' => 2,
'display_options' => [
'type' => 'entity_reference_label',
'settings' => [
'link' => FALSE,
],
],
];
}
return $fields;
}
public function isTableDragEnabled($element) {
$children = Element::children($element);
if (count($children) == 1) {
return FALSE;
}
foreach ($children as $key) {
if (!empty($element[$key]['form'])) {
return FALSE;
}
}
return TRUE;
}
public function entityForm(array $entity_form, FormStateInterface $form_state) {
$entity = $entity_form['#entity'];
$form_display = $this
->getFormDisplay($entity, $entity_form['#form_mode']);
$form_display
->buildForm($entity, $entity_form, $form_state);
$entity_form['#ief_element_submit'][] = [
get_class($this),
'submitCleanFormState',
];
$langcode_key = $this->entityType
->getKey('langcode');
if ($langcode_key && isset($entity_form[$langcode_key])) {
$entity_form[$langcode_key]['#access'] = FALSE;
}
if (!empty($entity_form['#translating'])) {
foreach ($entity
->getFieldDefinitions() as $field_name => $definition) {
if (isset($entity_form[$field_name]) && $field_name != $langcode_key) {
$entity_form[$field_name]['#access'] = $definition
->isTranslatable();
}
}
}
if ($this->entityType instanceof ContentEntityTypeInterface) {
if ($log_message_key = $this->entityType
->getRevisionMetadataKey('revision_log_message')) {
$entity_form[$log_message_key]['#access'] = FALSE;
}
}
$children_before = Element::children($entity_form);
$this->moduleHandler
->alter('inline_entity_form_entity_form', $entity_form, $form_state);
$children_after = Element::children($entity_form);
$children_diff = array_diff($children_after, $children_before);
foreach ($children_diff as $child) {
if (!isset($entity_form[$child]['#tree'])) {
$entity_form[$child]['#tree'] = $entity_form['#tree'];
}
if (!isset($entity_form[$child]['#parents'])) {
$entity_form[$child]['#parents'] = $entity_form[$child]['#tree'] && $entity_form['#tree'] ? array_merge($entity_form['#parents'], [
$child,
]) : [
$child,
];
}
$array_parents = $entity_form['#array_parents'];
$array_parents[] = $child;
$entity_form[$child]['#array_parents'] = $array_parents;
if (isset($entity_form[$child]['#group']) && isset($entity_form[$entity_form[$child]['#group']])) {
$entity_form[$child]['#group'] = implode('][', $entity_form[$entity_form[$child]['#group']]['#parents']);
}
}
return $entity_form;
}
public function entityFormValidate(array &$entity_form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
if (!empty($triggering_element['#ief_submit_trigger'])) {
$entity = $entity_form['#entity'];
$this
->buildEntity($entity_form, $entity, $form_state);
$form_display = $this
->getFormDisplay($entity, $entity_form['#form_mode']);
$form_display
->validateFormValues($entity, $entity_form, $form_state);
$entity
->setValidationRequired(FALSE);
foreach ($form_state
->getErrors() as $name => $message) {
$form_state
->setError($triggering_element, $message);
}
}
}
public function entityFormSubmit(array &$entity_form, FormStateInterface $form_state) {
$form_state
->cleanValues();
$entity = $entity_form['#entity'];
$this
->buildEntity($entity_form, $entity, $form_state);
}
public function save(EntityInterface $entity) {
$entity
->save();
}
public function delete($ids, $context) {
$storage_handler = $this->entityTypeManager
->getStorage($this->entityType
->id());
$entities = $storage_handler
->loadMultiple($ids);
$storage_handler
->delete($entities);
}
protected function buildEntity(array $entity_form, ContentEntityInterface $entity, FormStateInterface $form_state) {
$form_display = $this
->getFormDisplay($entity, $entity_form['#form_mode']);
$form_display
->extractFormValues($entity, $entity_form, $form_state);
if (isset($entity_form['#entity_builders'])) {
foreach ($entity_form['#entity_builders'] as $function) {
call_user_func_array($function, [
$entity
->getEntityTypeId(),
$entity,
&$entity_form,
&$form_state,
]);
}
}
}
public static function submitCleanFormState(&$entity_form, FormStateInterface $form_state) {
$entity = $entity_form['#entity'];
$bundle = $entity
->bundle();
$instances = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity_form['#entity_type'], $bundle);
foreach ($instances as $instance) {
$field_name = $instance
->getName();
if (!empty($entity_form[$field_name]['#parents'])) {
$parents = $entity_form[$field_name]['#parents'];
array_pop($parents);
if (!empty($parents)) {
$field_state = [];
WidgetBase::setWidgetState($parents, $field_name, $form_state, $field_state);
}
}
}
}
protected function getFormDisplay(ContentEntityInterface $entity, $form_mode) {
return EntityFormDisplay::collectRenderDisplay($entity, $form_mode);
}
}