You are here

function multiple_registration_entity_form_display_alter in Multiple Registration 8.2

Same name and namespace in other branches
  1. 3.x multiple_registration.module \multiple_registration_entity_form_display_alter()

Implements form hook display alter to override user edit form per role.

Parameters

\Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display: Form display object.

array $context: Current context array.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

./multiple_registration.module, line 355
Contains multiple_registration.module.

Code

function multiple_registration_entity_form_display_alter(EntityFormDisplayInterface &$form_display, array $context) {
  $route = \Drupal::routeMatch()
    ->getRouteObject();
  if ($route !== NULL) {
    $is_user_register_form = $route
      ->getPath() === '/user/register/{rid}' && $context['form_mode'] === 'register';
    $is_user_edit_form = $route
      ->getPath() === '/user/{user}/edit' && $context['form_mode'] === 'default';
    if ($context['entity_type'] === 'user' && ($is_user_register_form || $is_user_edit_form)) {
      $config = \Drupal::config('multiple_registration.create_registration_page_form_config');
      switch ($route
        ->getPath()) {
        case '/user/register/{rid}':
          $rid = \Drupal::routeMatch()
            ->getParameter('rid');
          $custom_form_mode = $config
            ->get('multiple_registration_form_mode_register_' . $rid) ?: 'register';
          break;
        case '/user/{user}/edit':

          // Current user id from the route context.
          $user_entity_from_route = \Drupal::routeMatch()
            ->getParameters()
            ->all()['user'];

          /** @var \Drupal\user\Entity\User $user_entity_from_route */
          $uid = $user_entity_from_route
            ->id();
          $query = \Drupal::database()
            ->select('multiple_registration', 'mr');
          $query
            ->condition('mr.uid', $uid);
          $query
            ->fields('mr', [
            'rid',
          ]);
          $rid = $query
            ->execute()
            ->fetchField();
          $custom_form_mode = $config
            ->get('multiple_registration_form_mode_edit_' . $rid) ?: 'default';
          break;
      }
      if ($custom_form_mode) {

        // Load the right entity form display.
        $id = $context['entity_type'] . '.' . $context['bundle'] . '.' . $custom_form_mode;
        $storage = \Drupal::entityTypeManager()
          ->getStorage('entity_form_display');
        $change_display = $storage
          ->load($id);

        // If form mode is activated, replace the given one with ours.
        if ($change_display) {
          $form_display = $change_display;
        }
      }
    }
  }
}