abstract class FormEntityBaseForm in Flexiform 8
Provides a base class for entity forms.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait- class \Drupal\flexiform\Form\FormEntityBaseForm
 
Expanded class hierarchy of FormEntityBaseForm
File
- src/Form/ FormEntityBaseForm.php, line 22 
Namespace
Drupal\flexiform\FormView source
abstract class FormEntityBaseForm extends FormBase {
  /**
   * The form display.
   *
   * @var \Drupal\flexiform\FlexiformEntityFormDisplay
   */
  protected $formDisplay;
  /**
   * The form entity.
   *
   * @var \Drupal\flexiform\FormEntity\FlexiformFormEntityInterface
   */
  protected $formEntity;
  /**
   * The form entity plugin manager.
   *
   * @var \Drupal\flexiform\FlexiformFormEntityPluginManager
   */
  protected $pluginManager;
  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;
  /**
   * The router service.
   *
   * @var \Symfony\Component\Routing\RouterInterface
   */
  protected $router;
  /**
   * FormEntityBaseForm constructor.
   *
   * @param \Drupal\flexiform\FlexiformFormEntityPluginManager $plugin_manager
   *   The form entity plugin manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Symfony\Component\Routing\RouterInterface $router
   *   The router service.
   */
  public function __construct(FlexiformFormEntityPluginManager $plugin_manager, EntityTypeManagerInterface $entity_type_manager, RouterInterface $router) {
    $this->pluginManager = $plugin_manager;
    $this->entityTypeManager = $entity_type_manager;
    $this->router = $router;
  }
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.flexiform_form_entity'), $container
      ->get('entity_type.manager'), $container
      ->get('router'));
  }
  /**
   * Get the form entity manager.
   *
   * @return \Drupal\flexiform\FormEntity\FlexiformFormEntityManager
   *   The form entity manager.
   */
  protected function formEntityManager(FormStateInterface $form_state) {
    return $this->formDisplay
      ->getFormEntityManager($form_state);
  }
  /**
   * Build the plugin configuration form.
   */
  protected function buildConfigurationForm(array $form, FormStateInterface $form_state, FlexiformFormEntityInterface $form_entity = NULL, $namespace = '') {
    $form_state = MultipleEntityFormState::createForForm($form, $form_state);
    $this->formEntity = $form_entity;
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#description' => $this
        ->t('A label for this entity. This is only used in the admin UI.'),
      '#required' => TRUE,
      '#default_value' => $form_entity
        ->getLabel(),
    ];
    if (empty($namespace)) {
      $form['namespace'] = [
        '#type' => 'machine_name',
        '#title' => $this
          ->t('Namespace'),
        '#description' => $this
          ->t('Internal namespace for this entity and its fields.'),
        '#machine_name' => [
          'exists' => [
            $this,
            'namespaceExists',
          ],
          'label' => $this
            ->t('Namespace'),
        ],
      ];
    }
    else {
      $form['namespace'] = [
        '#type' => 'value',
        '#value' => $namespace,
      ];
    }
    $form['configuration'] = [
      '#type' => 'container',
      '#parents' => [
        'configuration',
      ],
      '#tree' => TRUE,
    ];
    $form['configuration'] += $form_entity
      ->configurationForm($form['configuration'], $form_state);
    $form['actions'] = [
      '#type' => 'actions',
      'submit' => [
        '#type' => 'submit',
        '#value' => $this
          ->t('Save Configuration'),
        '#validate' => [
          [
            $this,
            'validateForm',
          ],
        ],
        '#submit' => [
          [
            $this,
            'submitForm',
          ],
        ],
      ],
      '#ajax' => [
        'callback' => [
          $this,
          'ajaxSubmit',
        ],
        'event' => 'click',
      ],
    ];
    return $form;
  }
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, FlexiformEntityFormDisplayInterface $form_display = NULL) {
    $this->formDisplay = $form_display;
    return $form;
  }
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $form_state = MultipleEntityFormState::createForForm($form, $form_state);
    if (!empty($this->formEntity)) {
      $this->formEntity
        ->configurationFormValidate($form['configuration'], $form_state);
    }
  }
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state = MultipleEntityFormState::createForForm($form, $form_state);
    $namespace = $form_state
      ->getValue('namespace');
    $configuration = [
      'label' => $form_state
        ->getValue('label'),
      'plugin' => $this->formEntity
        ->getPluginId(),
    ];
    $this->formEntity
      ->configurationFormSubmit($form['configuration'], $form_state);
    if ($plugin_conf = $form_state
      ->getValue('configuration')) {
      $configuration += $plugin_conf;
    }
    $this->formDisplay
      ->getFormEnhancer('multiple_entities')
      ->addFormEntityConfig($namespace, $configuration);
    $this->formDisplay
      ->save();
    $params = [
      'form_mode_name' => $this->formDisplay
        ->get('mode'),
    ];
    $entity_type_id = $this->formDisplay
      ->get('targetEntityType');
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    if ($route_name = $entity_type
      ->get('field_ui_base_route')) {
      $route = $this->router
        ->getRouteCollection()
        ->get($route_name);
      $path = $route
        ->getPath();
      if (strpos($path, '{' . $entity_type
        ->getBundleEntityType() . '}') !== FALSE) {
        $params[$entity_type
          ->getBundleEntityType()] = $this->formDisplay
          ->get('bundle');
      }
      elseif (strpos($path, '{bundle}') !== FALSE) {
        $params['bundles'] = $this->formDisplay
          ->get('bundle');
      }
    }
    $form_state
      ->setRedirect("entity.entity_form_display.{$entity_type_id}.form_mode", $params);
  }
  /**
   * Ajax the plugin selection.
   */
  public function ajaxSubmit(array $form, FormStateInterface $form_state) {
    // Prepare redirect parameters.
    $params = [
      'form_mode_name' => $this->formDisplay
        ->get('mode'),
    ];
    $entity_type_id = $this->formDisplay
      ->get('targetEntityType');
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    if ($route_name = $entity_type
      ->get('field_ui_base_route')) {
      $route = $this->router
        ->getCollection()
        ->get($route_name);
      $path = $route
        ->getPath();
      if (strpos($path, '{' . $entity_type
        ->getBundleEntityType() . '}') !== FALSE) {
        $params[$entity_type
          ->getBundleEntityType()] = $this->formDisplay
          ->get('bundle');
      }
      elseif (strpos($path, '{bundle}') !== FALSE) {
        $params['bundles'] = $this->formDisplay
          ->get('bundle');
      }
    }
    $response = new AjaxResponse();
    $response
      ->addCommand(new CloseModalDialogCommand());
    $response
      ->addCommand(new RedirectCommand((new Url("entity.entity_form_display.{$entity_type_id}.form_mode", $params))
      ->toString()));
    return $response;
  }
  /**
   * Check whether the namespace already exists.
   */
  public function namespaceExists($namespace, $element, FormStateInterface $form_state) {
    $entities = $this->formDisplay
      ->getFormEntityConfig();
    return !empty($entities[$namespace]);
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| DependencySerializationTrait:: | protected | property | An array of entity type IDs keyed by the property name of their storages. | |
| DependencySerializationTrait:: | protected | property | An array of service IDs keyed by property name used for serialization. | |
| DependencySerializationTrait:: | public | function | 1 | |
| DependencySerializationTrait:: | public | function | 2 | |
| FormBase:: | protected | property | The config factory. | 1 | 
| FormBase:: | protected | property | The request stack. | 1 | 
| FormBase:: | protected | property | The route match. | |
| FormBase:: | protected | function | Retrieves a configuration object. | |
| FormBase:: | protected | function | Gets the config factory for this form. | 1 | 
| FormBase:: | private | function | Returns the service container. | |
| FormBase:: | protected | function | Gets the current user. | |
| FormBase:: | protected | function | Gets the request object. | |
| FormBase:: | protected | function | Gets the route match. | |
| FormBase:: | protected | function | Gets the logger for a specific channel. | |
| FormBase:: | protected | function | Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: | |
| FormBase:: | public | function | Resets the configuration factory. | |
| FormBase:: | public | function | Sets the config factory for this form. | |
| FormBase:: | public | function | Sets the request stack object to use. | |
| FormEntityBaseForm:: | protected | property | The entity type manager. | |
| FormEntityBaseForm:: | protected | property | The form display. | |
| FormEntityBaseForm:: | protected | property | The form entity. | |
| FormEntityBaseForm:: | protected | property | The form entity plugin manager. | |
| FormEntityBaseForm:: | protected | property | The router service. | |
| FormEntityBaseForm:: | public | function | Ajax the plugin selection. | 1 | 
| FormEntityBaseForm:: | protected | function | Build the plugin configuration form. | |
| FormEntityBaseForm:: | public | function | Form constructor. Overrides FormInterface:: | 2 | 
| FormEntityBaseForm:: | public static | function | Instantiates a new instance of this class. Overrides FormBase:: | |
| FormEntityBaseForm:: | protected | function | Get the form entity manager. | |
| FormEntityBaseForm:: | public | function | Check whether the namespace already exists. | |
| FormEntityBaseForm:: | public | function | Form submission handler. Overrides FormInterface:: | 1 | 
| FormEntityBaseForm:: | public | function | Form validation handler. Overrides FormBase:: | |
| FormEntityBaseForm:: | public | function | FormEntityBaseForm constructor. | |
| FormInterface:: | public | function | Returns a unique string identifying the form. | 236 | 
| LinkGeneratorTrait:: | protected | property | The link generator. | 1 | 
| LinkGeneratorTrait:: | protected | function | Returns the link generator. | |
| LinkGeneratorTrait:: | protected | function | Renders a link to a route given a route name and its parameters. | |
| LinkGeneratorTrait:: | public | function | Sets the link generator service. | |
| LoggerChannelTrait:: | protected | property | The logger channel factory service. | |
| LoggerChannelTrait:: | protected | function | Gets the logger for a specific channel. | |
| LoggerChannelTrait:: | public | function | Injects the logger channel factory. | |
| MessengerTrait:: | protected | property | The messenger. | 29 | 
| MessengerTrait:: | public | function | Gets the messenger. | 29 | 
| MessengerTrait:: | public | function | Sets the messenger. | |
| RedirectDestinationTrait:: | protected | property | The redirect destination service. | 1 | 
| RedirectDestinationTrait:: | protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
| RedirectDestinationTrait:: | protected | function | Returns the redirect destination service. | |
| RedirectDestinationTrait:: | public | function | Sets the redirect destination service. | |
| StringTranslationTrait:: | protected | property | The string translation service. | 1 | 
| StringTranslationTrait:: | protected | function | Formats a string containing a count of items. | |
| StringTranslationTrait:: | protected | function | Returns the number of plurals supported by a given language. | |
| StringTranslationTrait:: | protected | function | Gets the string translation service. | |
| StringTranslationTrait:: | public | function | Sets the string translation service to use. | 2 | 
| StringTranslationTrait:: | protected | function | Translates a string to the current language or to a given language. | |
| UrlGeneratorTrait:: | protected | property | The url generator. | |
| UrlGeneratorTrait:: | protected | function | Returns the URL generator service. | |
| UrlGeneratorTrait:: | public | function | Sets the URL generator service. | |
| UrlGeneratorTrait:: | protected | function | Generates a URL or path for a specific route based on the given parameters. | 
