class FlexiformFormEntityManager in Flexiform 8
Class for form entity managers.
Hierarchy
- class \Drupal\flexiform\FormEntity\FlexiformFormEntityManager uses DependencySerializationTrait, StringTranslationTrait
Expanded class hierarchy of FlexiformFormEntityManager
6 files declare their use of FlexiformFormEntityManager
- FlexiformEntityFormDisplay.php in src/
FlexiformEntityFormDisplay.php - FormComponentBase.php in src/
FormComponent/ FormComponentBase.php - FormComponentTypeBase.php in src/
FormComponent/ FormComponentTypeBase.php - MultipleEntityFormState.php in src/
MultipleEntityFormState.php - MultipleEntityFormStateInterface.php in src/
MultipleEntityFormStateInterface.php
File
- src/
FormEntity/ FlexiformFormEntityManager.php, line 15
Namespace
Drupal\flexiform\FormEntityView source
class FlexiformFormEntityManager {
use StringTranslationTrait;
use DependencySerializationTrait;
/**
* The form display config entity.
*
* @var \Drupal\flexiform\FlexiformEntityFormDisplayInterface
*/
protected $formDisplay;
/**
* An array of contexts.
*
* @var \Drupal\flexiform\FormEntity\FormEntityContext[]
*/
protected $contexts = [];
/**
* An array of deferred entity saves to perform.
*
* @var \Drupal\Core\Entity\EntityInterface[]
*/
protected $deferredSaves = [];
/**
* Construct a new FlexiformFormEntityManager.
*
* @param \Drupal\flexiform\FlexiformEntityFormDisplayInterface $form_display
* The form display to manage the entities for.
* @param \Drupal\Core\Entity\FieldableEntityInterface[] $provided
* Array of provided entities keyed by namespace.
*/
public function __construct(FlexiformEntityFormDisplayInterface $form_display, array $provided = []) {
$this->formDisplay = $form_display;
$this
->initFormEntities($provided);
}
/**
* Get the flexiform form entity plugin manager.
*/
protected function getPluginManager() {
return \Drupal::service('plugin.manager.flexiform_form_entity');
}
/**
* Initialize form entities.
*/
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);
}
}
/**
* Get the context definitions from the form entity plugins.
*
* @return ContextDefinitionInterface[]
*/
public function getContextDefinitions() {
$context_definitions = [];
foreach ($this->contexts as $namespace => $context) {
$context_definitions[$namespace] = $context
->getContextDefinition();
}
return $context_definitions;
}
/**
* Get the actual contexts
*
* @return \Drupal\flexiform\FormEntity\FormEntityContext[]
*/
public function getContexts() {
return $this->contexts;
}
/**
*
*/
public function getContext($namespace) {
return $this->contexts[$namespace];
}
/**
* Get the form entities.
*/
public function getFormEntities() {
$form_entities = [];
foreach ($this->contexts as $namespace => $context) {
$form_entities[$namespace] = $context
->getFormEntity();
}
return $form_entities;
}
/**
* Get the form entity at a given namespace.
*
* @param string $namespace
* The namespace for the entity to retrieve.
*
* @return \Drupal\flexiform\FormEntity\FlexiformFormEntityInterface
* The form entity for the given namespace.
*/
public function getFormEntity($namespace = '') {
return $this
->getFormEntities()[$namespace];
}
/**
* Save the form entities.
*
* @param bool $save_base
* Whether or not to save the base entity.
*/
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);
}
}
// At the end loop over any deferred saves and perform them.
foreach ($this->deferredSaves as $entity) {
$entity
->save();
$this
->clearDeferredSave($entity);
}
}
/**
* Track that we need to do a deferred save of an entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to save.
*/
public function deferredSave(EntityInterface $entity) {
$this->deferredSaves["{$entity->getEntityTypeId()}:{$entity->id()}"] = $entity;
}
/**
* Clear a deferred save requirement.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to clear.
*/
public function clearDeferredSave(EntityInterface $entity) {
$key = "{$entity->getEntityTypeId()}:{$entity->id()}";
if (array_key_exists($key, $this->deferredSaves)) {
unset($this->deferredSaves[$key]);
}
}
/**
* Get the entity at a given namespace.
*
* @param string $namespace
* The entity namespace to get.
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FlexiformFormEntityManager:: |
protected | property | An array of contexts. | |
FlexiformFormEntityManager:: |
protected | property | An array of deferred entity saves to perform. | |
FlexiformFormEntityManager:: |
protected | property | The form display config entity. | |
FlexiformFormEntityManager:: |
public | function | Clear a deferred save requirement. | |
FlexiformFormEntityManager:: |
public | function | Track that we need to do a deferred save of an entity. | |
FlexiformFormEntityManager:: |
public | function | ||
FlexiformFormEntityManager:: |
public | function | Get the context definitions from the form entity plugins. | |
FlexiformFormEntityManager:: |
public | function | Get the actual contexts | |
FlexiformFormEntityManager:: |
public | function | Get the entity at a given namespace. | |
FlexiformFormEntityManager:: |
public | function | Get the form entities. | |
FlexiformFormEntityManager:: |
public | function | Get the form entity at a given namespace. | |
FlexiformFormEntityManager:: |
protected | function | Get the flexiform form entity plugin manager. | |
FlexiformFormEntityManager:: |
protected | function | Initialize form entities. | 1 |
FlexiformFormEntityManager:: |
public | function | Save the form entities. | |
FlexiformFormEntityManager:: |
public | function | Construct a new FlexiformFormEntityManager. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |