You are here

class MultipleEntityForm in Flexiform 8

Form enhancer for multiple entity forms.

Plugin annotation


@FormEnhancer(
  id = "multiple_entities",
  label = @Translation("Multiple Entities"),
)

Hierarchy

Expanded class hierarchy of MultipleEntityForm

File

src/Plugin/FormEnhancer/MultipleEntityForm.php, line 19

Namespace

Drupal\flexiform\Plugin\FormEnhancer
View source
class MultipleEntityForm extends ConfigurableFormEnhancerBase {
  use StringTranslationTrait;
  use AjaxFormTrait;

  /**
   * {@inheritdoc}
   */
  protected $supportedEvents = [
    'init_form_entity_config',
  ];

  /**
   * {@inheritdoc}
   */
  public function configurationForm(array $form, FormStateInterface $form_state) {

    // Prepare a link to add an entity to this form.
    $target_entity_type = $this->formDisplay
      ->get('targetEntityType');

    /** @var \Drupal\Core\Entity\EntityTypeInterface $target_entity_def */
    $target_entity_def = \Drupal::service('entity_type.manager')
      ->getDefinition($target_entity_type);
    $url_params = [
      'form_mode_name' => $this->formDisplay
        ->get('mode'),
    ];
    if ($target_entity_def
      ->get('bundle_entity_type')) {
      $url_params[$target_entity_def
        ->get('bundle_entity_type')] = $this->formDisplay
        ->get('bundle');
    }
    else {
      if ($target_entity_def
        ->hasKey('bundle')) {
        $url_params['bundle'] = $this->formDisplay
          ->get('bundle');
      }
    }
    $form['add'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Add Entity'),
      '#url' => Url::fromRoute("entity.entity_form_display.{$target_entity_type}.form_mode.form_entity_add", $url_params),
      '#attributes' => $this
        ->getAjaxButtonAttributes(),
      '#attached' => [
        'library' => [
          'core/drupal.ajax',
        ],
      ],
    ];
    $form['entities'] = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Entity'),
        $this
          ->t('Plugin'),
        $this
          ->t('Operations'),
      ],
      '#title' => t('Entities'),
      '#empty' => t('This form display has no entities yet.'),
    ];
    foreach ($this->formDisplay
      ->getFormEntityManager($form_state)
      ->getContexts() as $namespace => $context) {
      $operations = [];
      if (!empty($namespace)) {
        $operation_params = $url_params;
        $operation_params['entity_namespace'] = $namespace;
        $operations['edit'] = [
          'title' => $this
            ->t('Edit'),
          'weight' => 10,
          'url' => Url::fromRoute("entity.entity_form_display.{$target_entity_type}.form_mode.form_entity_edit", $operation_params),
          'attributes' => $this
            ->getAjaxButtonAttributes(),
        ];
      }
      $form['entities'][$namespace] = [
        'human_name' => [
          '#plain_text' => $context
            ->getContextDefinition()
            ->getLabel(),
        ],
        'plugin' => [
          '#plain_text' => $context
            ->getFormEntity()
            ->getLabel(),
        ],
        'operations' => [
          '#type' => 'operations',
          '#links' => $operations,
          '#attached' => [
            'library' => [
              'core/drupal.ajax',
            ],
          ],
        ],
      ];
    }
    return $form;
  }

  /**
   * Initialise the enhancer config.
   *
   * @return array
   *   The initial config for the enhancer.
   */
  public function initFormEntityConfig() {
    return !empty($this->configuration['entities']) ? $this->configuration['entities'] : [];
  }

  /**
   * Get the config for an entity namespace.
   *
   * @return array|false
   *   The entity config or FALSE if it doesn't exist.
   */
  public function getFormEntityConfig($namespace) {
    return $this->configuration['entities'][$namespace] ?? FALSE;
  }

  /**
   * Set the config for an entity namespace.
   *
   * @return $this
   *   The form enhancer.
   *
   * @todo: Rename to setFormEntityConfig()?
   */
  public function addFormEntityConfig($namespace, $configuration) {
    $this->configuration['entities'][$namespace] = $configuration;
    return $this;
  }

  /**
   * Remove an entity namespace.
   *
   * @return $this
   *   The form enhancer.
   */
  public function removeFormEntityConfig($namespace) {
    unset($this->configuration['entities'][$namespace]);
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AjaxFormTrait::getAjaxAttributes public static function Gets attributes for use with an AJAX modal.
AjaxFormTrait::getAjaxButtonAttributes public static function Gets attributes for use with an add button AJAX modal.
ConfigurableFormEnhancerBase::applies public function Whether this enhancer applies to a particular event. Overrides FormEnhancerBase::applies 1
ConfigurableFormEnhancerBase::configurationFormSubmit public function The configuration form submit callback. Overrides ConfigurableFormEnhancerInterface::configurationFormSubmit 5
ConfigurableFormEnhancerBase::configurationFormValidate public function The configuration form validation callback. Overrides ConfigurableFormEnhancerInterface::configurationFormValidate
ConfigurableFormEnhancerBase::getConfiguration public function Get the configuration. Overrides ConfigurableFormEnhancerInterface::getConfiguration
ConfigurableFormEnhancerBase::setConfiguration public function Set the configuration for the enhancer. Overrides ConfigurableFormEnhancerInterface::setConfiguration
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
FormEnhancerBase::$formDisplay protected property The form display entity.
FormEnhancerBase::getFormDisplay protected function Get the form display.
FormEnhancerBase::setFormDisplay public function Set the form display object. Overrides FormEnhancerInterface::setFormDisplay
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
MultipleEntityForm::$supportedEvents protected property An array of supported events. Overrides FormEnhancerBase::$supportedEvents
MultipleEntityForm::addFormEntityConfig public function Set the config for an entity namespace.
MultipleEntityForm::configurationForm public function The configuration form. Overrides ConfigurableFormEnhancerInterface::configurationForm
MultipleEntityForm::getFormEntityConfig public function Get the config for an entity namespace.
MultipleEntityForm::initFormEntityConfig public function Initialise the enhancer config.
MultipleEntityForm::removeFormEntityConfig public function Remove an entity namespace.
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.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
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.