View source
<?php
namespace Drupal\commerce\Plugin\Commerce\InlineForm;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
class ContentEntity extends EntityInlineFormBase {
public function defaultConfiguration() {
return [
'form_mode' => 'default',
'skip_save' => FALSE,
];
}
protected function requiredConfiguration() {
return [
'form_mode',
];
}
public function buildInlineForm(array $inline_form, FormStateInterface $form_state) {
$inline_form = parent::buildInlineForm($inline_form, $form_state);
assert($this->entity instanceof ContentEntityInterface);
if ($this->entity
->isTranslatable()) {
$this->entity = $this
->ensureTranslation($this->entity, $form_state);
}
$form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->configuration['form_mode']);
$form_display
->buildForm($this->entity, $inline_form, $form_state);
return $inline_form;
}
public function validateInlineForm(array &$inline_form, FormStateInterface $form_state) {
parent::validateInlineForm($inline_form, $form_state);
assert($this->entity instanceof ContentEntityInterface);
$form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->configuration['form_mode']);
$form_display
->extractFormValues($this->entity, $inline_form, $form_state);
$form_display
->validateFormValues($this->entity, $inline_form, $form_state);
}
public function submitInlineForm(array &$inline_form, FormStateInterface $form_state) {
parent::submitInlineForm($inline_form, $form_state);
assert($this->entity instanceof ContentEntityInterface);
$form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->configuration['form_mode']);
$form_display
->extractFormValues($this->entity, $inline_form, $form_state);
if ($this->entity
->isTranslatable()) {
$this
->updateLangcode($this->entity, $inline_form, $form_state);
}
if (empty($this->configuration['skip_save'])) {
$this->entity
->save();
}
}
protected function updateLangcode(ContentEntityInterface $entity, array $inline_form, FormStateInterface $form_state) {
$form_langcode = $form_state
->get('langcode');
if (empty($form_langcode)) {
return;
}
$langcode_key = $this
->getLangcodeKey($entity);
if (isset($inline_form[$langcode_key]) && Element::isVisibleElement($inline_form[$langcode_key])) {
return;
}
$entity_langcode = $entity
->get($langcode_key)->value;
if ($entity_langcode != $form_langcode && !$entity
->hasTranslation($form_langcode)) {
$entity
->set($langcode_key, $form_langcode);
}
}
protected function ensureTranslation(ContentEntityInterface $entity, FormStateInterface $form_state) {
$form_langcode = $form_state
->get('langcode');
if (empty($form_langcode)) {
return $entity;
}
$langcode_key = $this
->getLangcodeKey($entity);
$entity_langcode = $entity
->get($langcode_key)->value;
if ($this
->isTranslating($form_state) && !$entity
->hasTranslation($form_langcode)) {
$source = $form_state
->get([
'content_translation',
'source',
]);
$source_langcode = $source ? $source
->getId() : $entity_langcode;
if (!$entity
->hasTranslation($source_langcode)) {
$entity
->addTranslation($source_langcode, $entity
->toArray());
}
$source_translation = $entity
->getTranslation($source_langcode);
$entity
->addTranslation($form_langcode, $source_translation
->toArray());
$translation = $entity
->getTranslation($form_langcode);
$translation
->set('content_translation_source', $source_langcode);
if ($entity
->getEntityType()
->isRevisionable()) {
$translation
->setRevisionTranslationAffected(NULL);
}
}
if ($entity_langcode != $form_langcode && $entity
->hasTranslation($form_langcode)) {
$entity = $entity
->getTranslation($form_langcode);
}
return $entity;
}
protected function isTranslating(FormStateInterface $form_state) {
$form_langcode = $form_state
->get('langcode');
$default_langcode = $form_state
->get('entity_default_langcode');
return $form_langcode && $form_langcode != $default_langcode;
}
protected function getLangcodeKey(ContentEntityInterface $entity) {
$entity_type = $entity
->getEntityType();
$langcode_key = $entity_type
->getKey('langcode');
if (empty($langcode_key)) {
throw new \RuntimeException(sprintf('The entity type %s did not specify a langcode key.', $entity_type
->id()));
}
return $langcode_key;
}
}