View source
<?php
namespace Drupal\flexiform\Plugin\FlexiformFormEntity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\flexiform\FormEntity\FlexiformFormEntityBase;
use Drupal\user\EntityOwnerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FlexiformFormEntityTypedDataReferencedEntity extends FlexiformFormEntityBase implements ContainerFactoryPluginInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $moduleHandler;
protected $currentUser;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, AccountProxyInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->stringTranslation = $string_translation;
$this->moduleHandler = $module_handler;
$this->currentUser = $current_user;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('string_translation'), $container
->get('module_handler'), $container
->get('current_user'));
}
public function getEntity() {
try {
$base = $this
->getContextValue('base');
if (!$base) {
return NULL;
}
if ($entity = $base->{$this->pluginDefinition['property_name']}->entity) {
return $entity;
}
elseif (!empty($this->configuration['create'])) {
return $this
->createEntity();
}
return NULL;
} catch (\Exception $e) {
return NULL;
}
}
protected function createEntity() {
$values = [];
if ($bundle_key = $this->entityTypeManager
->getDefinition($this
->getEntityType())
->getKey('bundle')) {
$values[$bundle_key] = $this
->getBundle();
}
$entity = $this->entityTypeManager
->getStorage($this
->getEntityType())
->create($values);
$this->moduleHandler
->invokeAll('flexiform_form_entity_entity_create', [
$entity,
$this,
]);
return $entity;
}
protected function doSave(EntityInterface $entity) {
if (($this->configuration['owner_context'] ?? '_none') != 'none') {
if ($entity instanceof EntityOwnerInterface && !$entity
->getOwnerId()) {
if ($this->configuration['owner_context'] == '_current_user') {
$entity
->setOwnerId($this->currentUser
->id());
}
else {
$context = $this->formEntityManager
->getContexts()[$this->configuration['owner_context']];
if ($context
->hasContextValue()) {
$entity
->setOwner($context
->getContextValue());
}
}
}
}
parent::doSave($entity);
$base = $this
->getContextValue('base');
if ($base) {
$base->{$this->pluginDefinition['property_name']}[0] = $entity;
$this->formEntityManager
->deferredSave($base);
}
}
public function configurationForm(array $form, FormStateInterface $form_state) {
$form = parent::configurationForm($form, $form_state);
$form['create'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Create New Entity'),
'#description' => $this
->t('If the property is empty, and new entity will be created.'),
'#default_value' => !empty($this->configuration['create']),
];
if ($this->entityTypeManager
->getDefinition($this
->getEntityType())
->entityClassImplements(EntityOwnerInterface::class)) {
$form['owner_context'] = [
'#type' => 'select',
'#title' => $this
->t('Set owner to'),
'#description' => $this
->t('This will not override an existing owner.'),
'#default_value' => $this->configuration['owner_context'] ?? '_none',
'#options' => [
'_current_user' => $this
->t('Current user'),
],
'#empty_option' => $this
->t('Do not set owner'),
'#empty_value' => '_none',
'#states' => [
'visible' => [
':input[name="configuration[save_on_submit]"]' => [
'checked' => TRUE,
],
':input[name="configuration[create]"]' => [
'checked' => TRUE,
],
],
],
];
$context_definition = new ContextDefinition('entity:user', NULL, TRUE, FALSE);
$matching_contexts = $this
->contextHandler()
->getMatchingContexts($this->formEntityManager
->getContexts(), $context_definition);
foreach ($matching_contexts as $context) {
$form['owner_context']['#options'][$context
->getEntityNamespace()] = $context
->getContextDefinition()
->getLabel();
}
}
return $form;
}
}