FieldWidgetComponentType.php in Flexiform 8
File
src/Plugin/FormComponentType/FieldWidgetComponentType.php
View source
<?php
namespace Drupal\flexiform\Plugin\FormComponentType;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Entity\EntityDisplayBase;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Field\WidgetPluginManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\field_ui\Form\EntityDisplayFormBase;
use Drupal\flexiform\FormComponent\FormComponentTypeBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldWidgetComponentType extends FormComponentTypeBase implements ContainerFactoryPluginInterface {
protected $pluginManager;
protected $fieldTypes;
protected $fieldDefinitions;
protected $entityFieldManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.field.widget'), $container
->get('plugin.manager.field.field_type'), $container
->get('entity_field.manager'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, WidgetPluginManager $plugin_manager, FieldTypePluginManagerInterface $field_type_manager, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->fieldTypes = $field_type_manager
->getDefinitions();
$this->pluginManager = $plugin_manager;
$this->entityFieldManager = $entity_field_manager;
}
protected function getFieldDefinitions() {
if (empty($this->fieldDefinitions)) {
foreach ($this
->getFormEntityManager()
->getFormEntities() as $namespace => $form_entity) {
foreach ($this->entityFieldManager
->getFieldDefinitions($form_entity
->getEntityType(), $form_entity
->getBundle()) as $field_name => $field_definition) {
if ($this
->getFormDisplay()
->getMode() === EntityDisplayBase::CUSTOM_MODE || $field_definition
->getDisplayOptions('form')) {
$component_name = !empty($namespace) ? "{$namespace}:{$field_name}" : $field_name;
$this->fieldDefinitions[$component_name] = clone $field_definition;
$component_options = $this
->getFormDisplay()
->getComponent($component_name);
if (!empty($component_options['third_party_settings']['flexiform']['field_definition'])) {
$def_overrides = $component_options['third_party_settings']['flexiform']['field_definition'];
if (!empty($def_overrides['label'])) {
$this->fieldDefinitions[$component_name]
->setLabel($def_overrides['label']);
}
if (!empty($def_overrides['settings'])) {
$settings = $this->fieldDefinitions[$component_name]
->getSettings();
$settings = NestedArray::mergeDeep($settings, $def_overrides['settings']);
$this->fieldDefinitions[$component_name]
->setSettings($settings);
}
}
}
}
}
}
return $this->fieldDefinitions;
}
protected function getFieldDefinition($component_name) {
$defs = $this
->getFieldDefinitions();
return !empty($defs[$component_name]) ? $defs[$component_name] : NULL;
}
public function getComponent($name, array $options = []) {
$component = parent::getComponent($name, $options);
if ($field_definition = $this
->getFieldDefinition($name)) {
$component
->setFieldDefinition($this
->getFieldDefinition($name));
}
return $component;
}
public function componentRows(EntityDisplayFormBase $form_object, array $form, FormStateInterface $form_state) {
$rows = [];
foreach ($this
->getFieldDefinitions() as $component_name => $field_definition) {
if ($field_definition
->isDisplayConfigurable('form')) {
$rows[$component_name] = $this
->buildComponentRow($form_object, $component_name, $form, $form_state);
}
}
return $rows;
}
protected function getApplicableRendererPluginOptions($component_name) {
$field_definition = $this
->getFieldDefinition($component_name);
if (!$field_definition) {
print "NAME: " . $component_name;
}
$options = $this->pluginManager
->getOptions($field_definition
->getType());
$applicable_options = [];
foreach ($options as $option => $label) {
$plugin_class = DefaultFactory::getPluginClass($option, $this->pluginManager
->getDefinition($option));
if ($plugin_class::isApplicable($field_definition)) {
$applicable_options[$option] = $label;
}
}
return $applicable_options;
}
protected function getDefaultRendererPlugin($component_name) {
$type = $this
->getFieldDefinition($component_name)
->getType();
return isset($this->fieldTypes[$type]['default_widget']) ? $this->fieldTypes[$type]['default_widget'] : NULL;
}
}