FlexiformFormEntityManager.php in Flexiform 8
File
src/FormEntity/FlexiformFormEntityManager.php
View source
<?php
namespace Drupal\flexiform\FormEntity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\Context\ContextDefinitionInterface;
use Drupal\flexiform\FlexiformEntityFormDisplayInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class FlexiformFormEntityManager {
use StringTranslationTrait;
use DependencySerializationTrait;
protected $formDisplay;
protected $contexts = [];
protected $deferredSaves = [];
public function __construct(FlexiformEntityFormDisplayInterface $form_display, array $provided = []) {
$this->formDisplay = $form_display;
$this
->initFormEntities($provided);
}
protected function getPluginManager() {
return \Drupal::service('plugin.manager.flexiform_form_entity');
}
protected function initFormEntities(array $provided = []) {
foreach ($this->formDisplay
->getFormEntityConfig() as $namespace => $configuration) {
$configuration['manager'] = $this;
$form_entity_plugin = $this
->getPluginManager()
->createInstance($configuration['plugin'], $configuration);
if (isset($provided[$namespace])) {
$this->contexts[$namespace] = FormEntityContext::createFromFlexiformFormEntity($form_entity_plugin, $provided[$namespace]);
}
else {
$this->contexts[$namespace] = FormEntityContext::createFromFlexiformFormEntity($form_entity_plugin);
}
$this->contexts[$namespace]
->setEntityNamespace($namespace);
}
}
public function getContextDefinitions() {
$context_definitions = [];
foreach ($this->contexts as $namespace => $context) {
$context_definitions[$namespace] = $context
->getContextDefinition();
}
return $context_definitions;
}
public function getContexts() {
return $this->contexts;
}
public function getContext($namespace) {
return $this->contexts[$namespace];
}
public function getFormEntities() {
$form_entities = [];
foreach ($this->contexts as $namespace => $context) {
$form_entities[$namespace] = $context
->getFormEntity();
}
return $form_entities;
}
public function getFormEntity($namespace = '') {
return $this
->getFormEntities()[$namespace];
}
public function saveFormEntities($save_base = FALSE) {
foreach ($this->contexts as $namespace => $context) {
if ($namespace == '' && !$save_base) {
continue;
}
if ($entity = $context
->getContextValue()) {
$context
->getFormEntity()
->saveEntity($entity);
$this
->clearDeferredSave($entity);
}
}
foreach ($this->deferredSaves as $entity) {
$entity
->save();
$this
->clearDeferredSave($entity);
}
}
public function deferredSave(EntityInterface $entity) {
$this->deferredSaves["{$entity->getEntityTypeId()}:{$entity->id()}"] = $entity;
}
public function clearDeferredSave(EntityInterface $entity) {
$key = "{$entity->getEntityTypeId()}:{$entity->id()}";
if (array_key_exists($key, $this->deferredSaves)) {
unset($this->deferredSaves[$key]);
}
}
public function getEntity($namespace = '') {
if (!isset($this->contexts[$namespace])) {
throw new \Exception($this
->t('No entity at namespace :namespace', [
':namespace' => $namespace,
]));
}
$context = $this->contexts[$namespace];
if ($context
->hasContextValue()) {
return $context
->getContextValue();
}
return NULL;
}
}