You are here

class FormElementComponent in Flexiform 8

Component class for field widgets.

Hierarchy

Expanded class hierarchy of FormElementComponent

File

src/Plugin/FormComponentType/FormElementComponent.php, line 22

Namespace

Drupal\flexiform\Plugin\FormComponentType
View source
class FormElementComponent extends FormComponentBase implements ContainerFactoryFormComponentInterface, FormComponentWithSubmitInterface, FormComponentWithValidateInterface {
  use ContextAwarePluginAssignmentTrait;
  use StringTranslationTrait;

  /**
   * Element plugin manager service.
   *
   * @var \Drupal\flexiform\FormElementPluginManager
   */
  protected $pluginManager;

  /**
   * The context handler.
   *
   * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
   */
  protected $contextHandler;

  /**
   * The element plugin associated with this componenet.
   *
   * @var \Drupal\flexiform\FormElement\FormElementInterface
   */
  protected $plugin;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $name, array $options, FlexiformEntityFormDisplay $form_display) {
    return new static($name, $options, $form_display, $container
      ->get('plugin.manager.flexiform.form_element'), $container
      ->get('context.handler'));
  }

  /**
   * {@inheritdoc}
   */
  public function __construct($name, $options, FlexiformEntityFormDisplay $form_display, FormElementPluginManager $plugin_manager, ContextHandlerInterface $context_handler) {
    parent::__construct($name, $options, $form_display);
    $this->pluginManager = $plugin_manager;
    $this->contextHandler = $context_handler;
  }

  /**
   * Get the form element plugin.
   */
  protected function getPlugin() {
    if (empty($this->plugin)) {
      $this->plugin = $this->pluginManager
        ->createInstance($this->options['form_element'], $this->options['settings']);
      if ($this->plugin instanceof ContextAwarePluginInterface) {
        $this->contextHandler
          ->applyContextMapping($this->plugin, $this
          ->getFormEntityManager()
          ->getContexts());
      }
    }
    return $this->plugin;
  }

  /**
   * Render the component in the form.
   */
  public function render(array &$form, FormStateInterface $form_state, RendererInterface $renderer) {
    $element = [
      '#parents' => $form['#parents'],
      '#array_parents' => !empty($form['#array_parents']) ? $form['#array_parents'] : [],
    ];
    $element['#parents'][] = $this->name;
    $element['#array_parents'][] = $this->name;
    $element += $this
      ->getPlugin()
      ->form($element, $form_state);
    $form[$this->name] = $element;
  }

  /**
   * {@inheritdoc}
   */
  public function extractFormValues(array $form, FormStateInterface $form_state) {
    $this
      ->getPlugin()
      ->buildEntities($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function formValidate(array $form, FormStateInterface $form_state) {
    $this
      ->getPlugin()
      ->formValidate($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function formSubmit(array $form, FormStateInterface $form_state) {
    $this
      ->getPlugin()
      ->formSubmit($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function getAdminLabel() {
    return $this->options['admin_label'];
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $sform = [];
    $plugin = $this
      ->getPlugin();
    if ($plugin instanceof ContextAwarePluginInterface) {
      $contexts = $this
        ->getFormEntityManager()
        ->getContexts();
      $sform['context_mapping'] = [
        '#parents' => array_merge($form['#parents'], [
          'settings',
          'context_mapping',
        ]),
      ] + $this
        ->addContextAssignmentElement($plugin, $contexts);
    }
    $sform += $this
      ->getPlugin()
      ->settingsForm($sform, $form_state);
    return $sform;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];
    $summary += $this
      ->getPlugin()
      ->settingsSummary();
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsFormSubmit($values, array $form, FormStateInterface $form_state) {
    $options = [];
    $options += $this
      ->getPlugin()
      ->settingsFormSubmit($values, $form, $form_state);
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContextAwarePluginAssignmentTrait::addContextAssignmentElement protected function Builds a form element for assigning a context to a given slot.
ContextAwarePluginAssignmentTrait::contextHandler protected function Wraps the context handler.
ContextAwarePluginAssignmentTrait::t abstract protected function Ensures the t() method is available.
FormComponentBase::$formDisplay protected property The form display.
FormComponentBase::$formEntityManager protected property The form entity manager.
FormComponentBase::$name protected property The name of this component.
FormComponentBase::$options protected property The options supplied for this component.
FormComponentBase::getEntityNamespace protected function Get the entity namespace from the component name.
FormComponentBase::getFormDisplay public function
FormComponentBase::getFormEntityManager public function
FormComponentBase::setFormDisplay public function
FormComponentBase::setFormEntityManager public function
FormElementComponent::$contextHandler protected property The context handler.
FormElementComponent::$plugin protected property The element plugin associated with this componenet.
FormElementComponent::$pluginManager protected property Element plugin manager service.
FormElementComponent::create public static function Creates an instance of the form componenent. Overrides ContainerFactoryFormComponentInterface::create
FormElementComponent::extractFormValues public function Extract the form values. Overrides FormComponentInterface::extractFormValues
FormElementComponent::formSubmit public function Perform submission logic. Overrides FormComponentWithSubmitInterface::formSubmit
FormElementComponent::formValidate public function Perform validation logic. Overrides FormComponentWithValidateInterface::formValidate
FormElementComponent::getAdminLabel public function Get the admin label for the component. Overrides FormComponentBase::getAdminLabel
FormElementComponent::getPlugin protected function Get the form element plugin.
FormElementComponent::render public function Render the component in the form. Overrides FormComponentInterface::render
FormElementComponent::settingsForm public function Get the settings form. Overrides FormComponentBase::settingsForm
FormElementComponent::settingsFormSubmit public function Overrides FormComponentBase::settingsFormSubmit
FormElementComponent::settingsSummary public function Get the settings summary. Overrides FormComponentBase::settingsSummary
FormElementComponent::__construct public function Overrides FormComponentBase::__construct
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