You are here

class ComplexEntityFormModes in Form mode manager 8.2

Route controller factory specific for each entities using bundles.

This Factory are specific to work with entities using bundles and need more, specific code to implement it. The best example of that specific things are, "type" keys or add pages to chooses what kind of entity we need to create. This controller can be a good base to implement our custom things in our, custom entities using bundles.

Hierarchy

Expanded class hierarchy of ComplexEntityFormModes

1 file declares its use of ComplexEntityFormModes
FormModeManagerEntityController.php in src/Controller/FormModeManagerEntityController.php

File

src/ComplexEntityFormModes.php, line 20

Namespace

Drupal\form_mode_manager
View source
class ComplexEntityFormModes extends AbstractEntityFormModesFactory {

  /**
   * {@inheritdoc}
   *
   * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
   *   A render array for a list of the entity types that can be added; however,
   *   if there is only one entity type defined for the site, the function
   *   will return a RedirectResponse to the entity add page for that one entity
   *   type.
   */
  public function addPage(RouteMatchInterface $route_match) {
    $entity_type_id = $route_match
      ->getRouteObject()
      ->getOption('_form_mode_manager_entity_type_id');
    $entity_bundle_name = $route_match
      ->getRouteObject()
      ->getOption('_form_mode_manager_bundle_entity_type_id');
    $form_mode_name = $route_match
      ->getParameter('form_mode_name');
    $entity_routes_infos = $this->entityRoutingMap
      ->createInstance($entity_type_id, [
      'entityTypeId' => $entity_type_id,
    ])
      ->getPluginDefinition();
    $entity_type_cache_tags = $this->entityTypeManager
      ->getDefinition($entity_bundle_name)
      ->getListCacheTags();
    $build = [
      '#theme' => 'entity_add_list',
      '#bundles' => [],
      '#add_bundle_message' => $this
        ->t('There is no @entity_type yet.', [
        '@entity_type' => $entity_type_id,
      ]),
      '#cache' => [
        'tags' => Cache::mergeTags($entity_type_cache_tags, $this->formModeManager
          ->getListCacheTags()),
      ],
    ];
    $entity_type_definitions = $this->entityTypeManager
      ->getStorage($entity_bundle_name)
      ->loadMultiple();
    $entity_add_form = $entity_routes_infos['operations']['add_form'] . ".{$form_mode_name}";
    foreach ($entity_type_definitions as $bundle) {
      $bundle_name = $bundle
        ->id();
      if ($access = $this
        ->accessIsAllowed($entity_type_id, $bundle_name, $form_mode_name)) {
        $description = method_exists($bundle, 'getDescription') ? $bundle
          ->getDescription() : '';
        $build['#bundles'][$bundle_name] = [
          'label' => $bundle
            ->label(),
          'description' => $description,
          'add_link' => Link::createFromRoute($bundle
            ->label(), $entity_add_form, [
            $entity_bundle_name => $bundle
              ->id(),
          ]),
        ];
        $this->renderer
          ->addCacheableDependency($build, $access);
      }
    }

    // Bypass the entity/add listing if only one content type is available.
    if (1 == count($build['#bundles'])) {
      $bundle = current($build['#bundles']);
      $options = [
        'absolute' => TRUE,
      ];
      return new RedirectResponse(Url::fromRoute($entity_add_form, $bundle['add_link']
        ->getUrl()
        ->getRouteParameters(), $options)
        ->toString(), 302);
    }
    return $build;
  }

  /**
   * Evaluate if current user has access to this bundle AND form mode.
   *
   * @param string $entity_type_id
   *   The id of current entity.
   * @param string $bundle_name
   *   The name of current bundle need to access.
   * @param string $form_mode_name
   *   The form mode name to use.
   *
   * @return bool
   *   True if you can access to this entity type as given form mode.
   */
  public function accessIsAllowed($entity_type_id, $bundle_name, $form_mode_name) {
    $access = $this->entityTypeManager
      ->getAccessControlHandler($entity_type_id)
      ->createAccess($bundle_name, $this->account, [], TRUE);
    return $access
      ->isAllowed() && $this->formModeManager
      ->isActive($entity_type_id, $bundle_name, $form_mode_name);
  }

  /**
   * {@inheritdoc}
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The entity loaded form route_match.
   *
   * @throws \Exception
   *   If an invalid entity is retrieving from the route object.
   */
  public function getEntity(RouteMatchInterface $route_match) {

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $this
      ->getEntityFromRouteMatch($route_match);

    // If we can't retrieve the entity from the route match get load,
    // it by their storage with correct route bundle key.
    if (empty($entity)) {
      $route_entity_type_info = $this
        ->getEntityTypeFromRouteMatch($route_match);

      /** @var \Drupal\Core\Entity\EntityInterface $entity */
      $entity = $this->entityTypeManager
        ->getStorage($route_entity_type_info['entity_type_id'])
        ->create([
        $route_entity_type_info['entity_key'] => $route_entity_type_info['bundle'],
      ]);
    }
    return $entity;
  }

  /**
   * {@inheritdoc}
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   A new entity object build with given route_match.
   */
  public function getEntityFromRouteMatch(RouteMatchInterface $route_match) {
    $entity_type_id = $route_match
      ->getRouteObject()
      ->getOption('_form_mode_manager_entity_type_id');
    $bundle_entity_type_id = $route_match
      ->getRouteObject()
      ->getOption('_form_mode_manager_bundle_entity_type_id');
    $entity = $route_match
      ->getParameter($entity_type_id);
    if (empty($entity)) {
      $entity = $this->entityTypeManager
        ->getStorage($entity_type_id)
        ->create([
        'type' => $route_match
          ->getRawParameter($bundle_entity_type_id),
      ]);
    }
    return $entity;
  }

  /**
   * {@inheritdoc}
   *
   * @return array
   *   The entity object as determined from the passed-in route match.
   */
  public function getEntityTypeFromRouteMatch(RouteMatchInterface $route_match) {
    $route = $route_match
      ->getRouteObject();
    $entity_type_id = $route
      ->getOption('_form_mode_manager_entity_type_id');
    $bundle_entity_type_id = $route
      ->getOption('_form_mode_manager_bundle_entity_type_id');
    $form_mode = $this->formModeManager
      ->getFormModeMachineName($route
      ->getDefault('_entity_form'));
    $bundle = $route_match
      ->getRawParameter($bundle_entity_type_id);
    $form_mode_definition = $this->formModeManager
      ->getActiveDisplaysByBundle($entity_type_id, $bundle);
    $entity_type_key = $this->entityTypeManager
      ->getDefinition($entity_type_id)
      ->getKey('bundle');
    return [
      'bundle' => $bundle,
      'bundle_entity_type' => $bundle_entity_type_id,
      'entity_key' => $entity_type_key,
      'entity_type_id' => $entity_type_id,
      'form_mode' => isset($form_mode_definition[$entity_type_id][$form_mode]) ? $form_mode_definition[$entity_type_id][$form_mode] : NULL,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractEntityFormModesFactory::$account protected property The current user.
AbstractEntityFormModesFactory::$dateFormatter protected property The date formatter service.
AbstractEntityFormModesFactory::$entityFormBuilder protected property The entity form builder service.
AbstractEntityFormModesFactory::$entityRoutingMap protected property The Routes Manager Plugin.
AbstractEntityFormModesFactory::$entityTypeManager protected property The entity type manager service.
AbstractEntityFormModesFactory::$formBuilder protected property The form builder.
AbstractEntityFormModesFactory::$formModeManager protected property The entity display repository.
AbstractEntityFormModesFactory::$renderer protected property The renderer service.
AbstractEntityFormModesFactory::addPageTitle public function Overrides EntityFormModeManagerInterface::addPageTitle
AbstractEntityFormModesFactory::checkAccess public function Overrides EntityFormModeManagerInterface::checkAccess 1
AbstractEntityFormModesFactory::editPageTitle public function Overrides EntityFormModeManagerInterface::editPageTitle
AbstractEntityFormModesFactory::entityAdd public function Provides the entity add submission form. Overrides EntityFormModeManagerInterface::entityAdd
AbstractEntityFormModesFactory::entityEdit public function Provides the entity 'edit' form. Overrides EntityFormModeManagerInterface::entityEdit
AbstractEntityFormModesFactory::getBundleEntityTypeId public function
AbstractEntityFormModesFactory::getForm public function Gets the built and processed entity form for the given entity.
AbstractEntityFormModesFactory::getFormModeOperationName public function Retrieve the operation (form mode) name in edit context.
AbstractEntityFormModesFactory::getOperation public function Return the correct form mode name for given contexts ($op).
AbstractEntityFormModesFactory::pageTitle public function The _title_callback for the entity.add routes.
AbstractEntityFormModesFactory::__construct public function Constructs a EntityFormModeController object.
ComplexEntityFormModes::accessIsAllowed public function Evaluate if current user has access to this bundle AND form mode.
ComplexEntityFormModes::addPage public function Overrides AbstractEntityFormModesFactory::addPage
ComplexEntityFormModes::getEntity public function Overrides AbstractEntityFormModesFactory::getEntity
ComplexEntityFormModes::getEntityFromRouteMatch public function Overrides AbstractEntityFormModesFactory::getEntityFromRouteMatch 2
ComplexEntityFormModes::getEntityTypeFromRouteMatch public function Overrides AbstractEntityFormModesFactory::getEntityTypeFromRouteMatch
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.