Same name and namespace in other branches
  1. 8.x-2.x entity_redirect.module \entity_redirect_submit() 1 comment

Submit function to handle the redirection per entity create/edit action.

1 string reference to 'entity_redirect_submit'
entity_redirect_form_alter in ./entity_redirect.module
Implements hook_form_alter().

File

./entity_redirect.module, line 141

Code

function entity_redirect_submit($form, FormStateInterface $form_state) {
    $info = $form_state->getBuildInfo();
    if ($info['base_form_id'] === 'node_form') {
        $type = 'node_type';
        $route = 'node';
    }
    elseif ($info['base_form_id'] === 'media_form') {
        // If media_entity is enabled, the entity type we are looking for is
        // 'media_bundle', if not, it means we are using the core media module
        // so the entity type is 'media_type'.
        $type = \Drupal::moduleHandler()->moduleExists('media_entity') ? 'media_bundle' : 'media_type';
        $route = 'media';
    }
    elseif ($info['base_form_id'] === 'taxonomy_term_form') {
        $type = 'taxonomy_vocabulary';
        $route = 'taxonomy_term';
    }
    elseif ($info['base_form_id'] === 'commerce_product_form') {
        $type = 'commerce_product_type';
        $route = 'commerce_product';
    }
    $entity = $form_state->getFormObject()
        ->getEntity();
    $bundle = $entity->bundle();
    $entity_type = \Drupal::service('entity_type.manager')->getStorage($type)
        ->load($bundle);
    if ($entity_type->getThirdPartySetting('entity_redirect', 'personalizable')) {
        if ($user_id = \Drupal::currentUser()->id() && \Drupal::currentUser()->hasPermission('use personalized redirect options')) {
            $personalization = $entity_type->getThirdPartySetting('entity_redirect', 'personalization');
            if (isset($personalization[$user_id])) {
                if ($personalization[$user_id]['destination'] !== 'default') {
                    $destination = $personalization[$user_id]['destination'];
                    if (isset($personalization[$user_id]['url'])) {
                        $url = $personalization[$user_id]['url'];
                    }
                    if (isset($personalization[$user_id]['external'])) {
                        $external = $personalization[$user_id]['external'];
                    }
                }
            }
        }
    }
    if (isset($destination) || ($destination = $entity_type->getThirdPartySetting('entity_redirect', 'destination'))) {
        if ($destination === 'add_form') {
            $route_provider = \Drupal::service('router.route_provider');
            $routes = array_keys($route_provider->getRoutesByNames([
                "{$route}.add",
                "entity.{$route}.add_form",
            ]));
            $form_state->setRedirect($routes[0], [
                $type => $bundle,
            ]);
        }
        elseif ($destination === 'created') {
            $form_state->setRedirect("entity.{$route}.canonical", [
                $route => $entity->id(),
            ]);
        }
        elseif ($destination === 'url') {
            if (isset($url) || ($url = $entity_type->getThirdPartySetting('entity_redirect', 'url'))) {
                $form_state->setRedirectUrl(Url::fromUri('internal:' . $url));
            }
        }
        elseif ($destination === 'external') {
            if (isset($external) || ($external = $entity_type->getThirdPartySetting('entity_redirect', 'external'))) {
                $response = new TrustedRedirectResponse($external);
                $form_state->setResponse($response);
            }
        }
    }
}