ExtraFieldFormManager.php in Extra Field 8.2
File
src/Plugin/ExtraFieldFormManager.php
View source
<?php
namespace Drupal\extra_field\Plugin;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfirmFormInterface;
use Drupal\Core\Form\FormStateInterface;
class ExtraFieldFormManager extends ExtraFieldManagerBase implements ExtraFieldFormManagerInterface {
const PLUGIN_SUBDIR = 'Plugin/ExtraField/Form';
const PLUGIN_INTERFACE = 'Drupal\\extra_field\\Plugin\\ExtraFieldFormInterface';
const PLUGIN_ANNOTATION_NAME = 'Drupal\\extra_field\\Annotation\\ExtraFieldForm';
const ALTER_HOOK = 'extra_field_form_info';
const CACHE_KEY = 'extra_field_form_plugins';
protected $entityTypeManager;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct(self::PLUGIN_SUBDIR, $namespaces, $module_handler, self::PLUGIN_INTERFACE, self::PLUGIN_ANNOTATION_NAME);
$this
->alterInfo(self::ALTER_HOOK);
$this
->setCacheBackend($cache_backend, self::CACHE_KEY);
$this->entityTypeManager = $entity_type_manager;
}
public function fieldInfo() {
$info = [];
$definitions = $this
->getDefinitions();
foreach ($definitions as $pluginId => $definition) {
$entityBundles = $this
->supportedEntityBundles($definition['bundles']);
foreach ($entityBundles as $entityBundle) {
$entityType = $entityBundle['entity'];
$bundle = $entityBundle['bundle'];
$fieldName = $this
->fieldName($pluginId);
$info[$entityType][$bundle]['form'][$fieldName] = [
'label' => $definition['label'],
'description' => isset($definition['description']) ? $definition['description'] : '',
'weight' => $definition['weight'],
'visible' => $definition['visible'],
];
}
}
return $info;
}
public function entityFormAlter(array &$form, FormStateInterface $formState) {
$formObject = $formState
->getFormObject();
if (!$formObject instanceof ContentEntityFormInterface || $formObject instanceof ConfirmFormInterface) {
return;
}
$display = $formObject
->getFormDisplay($formState);
if (!$display) {
return;
}
$entityType = $display
->getTargetEntityTypeId();
$entityBundleKey = $this
->entityBundleKey($entityType, $display
->get('bundle'));
foreach ($this
->getDefinitions() as $pluginId => $definition) {
if (!$this
->matchEntityBundleKey($definition['bundles'], $entityBundleKey)) {
continue;
}
$factory = $this
->getFactory();
if (empty($display
->getComponent($this
->fieldName($pluginId)))) {
continue;
}
$plugin = $factory
->createInstance($pluginId);
$fieldName = $this
->fieldName($pluginId);
$plugin
->setEntity($formObject
->getEntity());
$plugin
->setEntityFormDisplay($display);
$plugin
->setFormMode($display
->get('mode'));
$form[$fieldName] = $plugin
->formElement($form, $formState);
}
}
protected function getEntityTypeManager() {
return $this->entityTypeManager;
}
}