FlexiformFormEntityBase.php in Flexiform 8
File
src/FormEntity/FlexiformFormEntityBase.php
View source
<?php
namespace Drupal\flexiform\FormEntity;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContextAwarePluginBase;
abstract class FlexiformFormEntityBase extends ContextAwarePluginBase implements FlexiformFormEntityInterface {
use DependencySerializationTrait;
protected $formEntityManager;
protected $formEntityContext;
protected $prepared;
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
if (!isset($configuration['manager'])) {
throw new \Exception('No Form Entity Manager Supplied');
}
$this->formEntityManager = $configuration['manager'];
unset($configuration['manager']);
unset($configuration['namespace']);
parent::__construct($configuration, $plugin_id, $plugin_definition);
if (!empty($configuration['context_mapping'])) {
foreach ($configuration['context_mapping'] as $key => $context_namespace) {
$context = $this->formEntityManager
->getContext($context_namespace);
$this->context[$key] = $context;
}
}
}
protected function checkBundle(EntityInterface $entity) {
return !$entity
->getEntityType()
->hasKey('bundle') || $entity
->bundle() == $this
->getBundle();
}
public function getLabel() {
return !empty($this->configuration['label']) ? $this->configuration['label'] : $this->pluginDefinition['label'];
}
public function getEntityType() {
return $this->pluginDefinition['entity_type'];
}
public function getBundle() {
return $this->pluginDefinition['bundle'];
}
public function getFormEntityContext() {
return $this->formEntityContext;
}
public function getFormEntityContextDefinition() {
return $this->formEntityContext
->getContextDefinition();
}
public abstract function getEntity();
public final function saveEntity(EntityInterface $entity) {
if (!isset($this->configuration['save_on_submit']) || $this->configuration['save_on_submit']) {
$this
->doSave($entity);
}
}
protected function doSave(EntityInterface $entity) {
$entity
->save();
}
public function configurationForm(array $form, FormStateInterface $form_state) {
$form['save_on_submit'] = [
'#type' => 'checkbox',
'#title' => t('Save this Entity when the form is submitted.'),
'#default_value' => isset($this->configuration['save_on_submit']) ? $this->configuration['save_on_submit'] : TRUE,
];
$form['context_mapping'] = [
'#type' => 'container',
'#tree' => TRUE,
];
if (empty($this->pluginDefinition['context']) || !is_array($this->pluginDefinition['context'])) {
return $form;
}
foreach ($this->pluginDefinition['context'] as $key => $context_definition) {
$matching_contexts = $this
->contextHandler()
->getMatchingContexts($this->formEntityManager
->getContexts(), $context_definition);
$context_options = [];
foreach ($matching_contexts as $context) {
$context_options[$context
->getEntityNamespace()] = $context
->getContextDefinition()
->getLabel();
}
$form['context_mapping'][$key] = [
'#type' => 'select',
'#title' => $context_definition
->getLabel(),
'#options' => $context_options,
'#default_value' => !empty($this->configuration['context_mapping'][$key]) ? $this->configuration['context_mapping'][$key] : NULL,
];
}
return $form;
}
public function configurationFormValidate(array $form, FormStateInterface $form_state) {
}
public function configurationFormSubmit(array $form, FormStateInterface $form_state) {
}
}