You are here

class ExtraFieldComponentType in Flexiform 8

Plugin for extra field components.

Plugin annotation


@FormComponentType(
  id = "extra_field",
  label = @Translation("Extra Field"),
  component_class = "Drupal\flexiform\Plugin\FormComponentType\ExtraFieldComponent",
)

Hierarchy

Expanded class hierarchy of ExtraFieldComponentType

File

src/Plugin/FormComponentType/ExtraFieldComponentType.php, line 21

Namespace

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

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $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\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityFieldManager = $entity_field_manager;
  }

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

  /**
   * {@inheritdoc}
   */
  public function componentRows(EntityDisplayFormBase $form_object, array $form, FormStateInterface $form_state) {
    $rows = [];
    foreach ($this
      ->getExtraFields() as $component_name => $extra_field) {
      $rows[$component_name] = $this
        ->buildComponentRow($form_object, $component_name, $form, $form_state);
    }
    return $rows;
  }

  /**
   * {@inheritdoc}
   */
  public function submitComponentRow($component_name, $values, array $form, FormStateInterface $form_state) {
    $options = $this
      ->getFormDisplay()
      ->getComponent($component_name);
    $options['weight'] = $values['weight'];
    $options['region'] = $values['region'];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildComponentRow(EntityDisplayFormBase $form_object, $component_name, array $form, FormStateInterface $form_state) {
    $display_options = $this
      ->getFormDisplay()
      ->getComponent($component_name);
    $extra_field = $this
      ->getExtraField($component_name);
    $regions = array_keys($form_object
      ->getRegions());
    $extra_field_row = [
      '#attributes' => [
        'class' => [
          'draggable',
          'tabledrag-leaf',
        ],
      ],
      '#row_type' => 'extra_field',
      '#region_callback' => [
        $form_object,
        'getRowRegion',
      ],
      '#js_settings' => [
        'rowHandler' => 'field',
      ],
      'human_name' => [
        '#markup' => $extra_field['label'],
      ],
      'weight' => [
        '#type' => 'textfield',
        '#title' => t('Weight for @title', [
          '@title' => $extra_field['label'],
        ]),
        '#title_display' => 'invisible',
        '#default_value' => $display_options ? $display_options['weight'] : 0,
        '#size' => 3,
        '#attributes' => [
          'class' => [
            'field-weight',
          ],
        ],
      ],
      'parent_wrapper' => [
        'parent' => [
          '#type' => 'select',
          '#title' => t('Parents for @title', [
            '@title' => $extra_field['label'],
          ]),
          '#title_display' => 'invisible',
          '#options' => array_combine($regions, $regions),
          '#empty_value' => '',
          '#attributes' => [
            'class' => [
              'js-field-parent',
              'field-parent',
            ],
          ],
          '#parents' => [
            'fields',
            $component_name,
            'parent',
          ],
        ],
        'hidden_name' => [
          '#type' => 'hidden',
          '#default_value' => $component_name,
          '#attributes' => [
            'class' => [
              'field-name',
            ],
          ],
        ],
      ],
      'region' => [
        '#type' => 'select',
        '#title' => t('Region for @title', [
          '@title' => $extra_field['label'],
        ]),
        '#title_display' => 'invisible',
        '#options' => $form_object
          ->getRegionOptions(),
        '#default_value' => $display_options ? $display_options['region'] : 'hidden',
        '#attributes' => [
          'class' => [
            'field-region',
          ],
        ],
      ],
      'plugin' => [
        'type' => [
          '#type' => 'hidden',
          '#value' => $display_options ? 'visible' : 'hidden',
          '#parents' => [
            'fields',
            $component_name,
            'type',
          ],
          '#attributes' => [
            'class' => [
              'field-plugin-type',
            ],
          ],
        ],
      ],
      'settings_summary' => [],
      'settings_edit' => [],
    ];
    return $extra_field_row;
  }

  /**
   * Get extra fields.
   */
  protected function getExtraFields() {
    if ($form_entity = $this
      ->getFormEntityManager()
      ->getFormEntity('')) {
      $extra_fields = $this->entityFieldManager
        ->getExtraFields($form_entity
        ->getEntityType(), $form_entity
        ->getBundle());
      return isset($extra_fields['form']) ? $extra_fields['form'] : [];
    }
    return [];
  }

  /**
   * Get extra field.
   */
  protected function getExtraField($component_name) {
    $extra_fields = $this
      ->getExtraFields();
    return isset($extra_fields[$component_name]) ? $extra_fields[$component_name] : [];
  }

}

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
ExtraFieldComponentType::buildComponentRow public function Build a component row for a component of this type. Overrides FormComponentTypeBase::buildComponentRow
ExtraFieldComponentType::componentRows public function Overrides FormComponentTypeBase::componentRows
ExtraFieldComponentType::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
ExtraFieldComponentType::getComponent public function Get a component object. Overrides FormComponentTypeBase::getComponent
ExtraFieldComponentType::getExtraField protected function Get extra field.
ExtraFieldComponentType::getExtraFields protected function Get extra fields.
ExtraFieldComponentType::submitComponentRow public function Overrides FormComponentTypeBase::submitComponentRow
ExtraFieldComponentType::__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::getApplicableRendererPluginOptions protected function Get applicable renderer plugin options. 1
FormComponentTypeBase::getDefaultRendererPlugin protected function Get the default renderer plugin. 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
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.