You are here

class FieldWidgetComponentType in Flexiform 8

Plugin for field widget component types.

Plugin annotation


@FormComponentType(
  id = "field_widget",
  label = @Translation("Field Widget"),
  component_class = "Drupal\flexiform\Plugin\FormComponentType\FieldWidgetComponent",
)

Hierarchy

Expanded class hierarchy of FieldWidgetComponentType

File

src/Plugin/FormComponentType/FieldWidgetComponentType.php, line 25

Namespace

Drupal\flexiform\Plugin\FormComponentType
View source
class FieldWidgetComponentType extends FormComponentTypeBase implements ContainerFactoryPluginInterface {

  /**
   * The widget plugin manager.
   *
   * @var \Drupal\Core\Field\WidgetPluginManager
   */
  protected $pluginManager;

  /**
   * Field type definitions.
   *
   * @var array
   */
  protected $fieldTypes;

  /**
   * Field defintiions.
   *
   * @var array
   */
  protected $fieldDefinitions;

  /**
   * The entity field manager.
   *
   * @var array
   */
  protected $entityFieldManager;

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * Construct a new FieldWidgetComponentType object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Field\WidgetPluginManager $plugin_manager
   *   The plugin type manager.
   * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
   *   The field type plugin manager.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The 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;
  }

  /**
   * Get the field definitions.
   */
  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')) {

            // Give field definitions a clone for form entities so that
            // overrides don't copy accross two different fields.
            $component_name = !empty($namespace) ? "{$namespace}:{$field_name}" : $field_name;
            $this->fieldDefinitions[$component_name] = clone $field_definition;

            // Apply overrides.
            $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;
  }

  /**
   * Get a field definition.
   */
  protected function getFieldDefinition($component_name) {
    $defs = $this
      ->getFieldDefinitions();
    return !empty($defs[$component_name]) ? $defs[$component_name] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getComponent($name, array $options = []) {
    $component = parent::getComponent($name, $options);
    if ($field_definition = $this
      ->getFieldDefinition($name)) {
      $component
        ->setFieldDefinition($this
        ->getFieldDefinition($name));
    }
    return $component;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  protected function getDefaultRendererPlugin($component_name) {
    $type = $this
      ->getFieldDefinition($component_name)
      ->getType();
    return isset($this->fieldTypes[$type]['default_widget']) ? $this->fieldTypes[$type]['default_widget'] : NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FieldWidgetComponentType::$entityFieldManager protected property The entity field manager.
FieldWidgetComponentType::$fieldDefinitions protected property Field defintiions.
FieldWidgetComponentType::$fieldTypes protected property Field type definitions.
FieldWidgetComponentType::$pluginManager protected property The widget plugin manager.
FieldWidgetComponentType::componentRows public function Overrides FormComponentTypeBase::componentRows
FieldWidgetComponentType::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
FieldWidgetComponentType::getApplicableRendererPluginOptions protected function Get applicable renderer plugin options. Overrides FormComponentTypeBase::getApplicableRendererPluginOptions
FieldWidgetComponentType::getComponent public function Get a component object. Overrides FormComponentTypeBase::getComponent
FieldWidgetComponentType::getDefaultRendererPlugin protected function Get the default renderer plugin. Overrides FormComponentTypeBase::getDefaultRendererPlugin
FieldWidgetComponentType::getFieldDefinition protected function Get a field definition.
FieldWidgetComponentType::getFieldDefinitions protected function Get the field definitions.
FieldWidgetComponentType::__construct public function Construct a new FieldWidgetComponentType object. Overrides PluginBase::__construct
FormComponentTypeBase::$components protected property A list of components that have been constructed.
FormComponentTypeBase::$formDisplay protected property The form display.
FormComponentTypeBase::$formEntityManager protected property The form entity manager.
FormComponentTypeBase::buildComponentRow protected function Build a component row for a component of this type. 1
FormComponentTypeBase::getFormDisplay public function Get the form display. Overrides FormComponentTypeInterface::getFormDisplay
FormComponentTypeBase::getFormEntityManager public function Get the form entity manager. Overrides FormComponentTypeInterface::getFormEntityManager
FormComponentTypeBase::setFormDisplay public function Set the form display. Overrides FormComponentTypeInterface::setFormDisplay
FormComponentTypeBase::setFormEntityManager public function
FormComponentTypeBase::submitComponentRow public function 1
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.