Skip to main content

Redirection In Drupal 8 Programatcially

Redirection In Drupal 8 


In this post we can learn about the ways we can make redirect in Drupal 8.

You can make a custom redirect according to the context in many ways.

Form Redirect 

    We can use the formstate object to redirect from form. 
   Currently below two methods are available from Drupal\Core\Form  formstate to build  a   \Symfony\Component\HttpFoundation\RedirectResponse  for redirection.

1)setRedirect

 function setRedirect($route_name, array $route_parameters = [], array $options = [])
This method will call the setRedirectUrl function inside it.

1) route_parameters - optional argument

2) route_nameThe name of the route name.

3) options -    (optional) An associative array of additional URL options, with the
   *   following elements:

   *   - 'query': An array of query key/value-pairs (without any URL-encoding)
   *     to append to the URL.
   *   - 'fragment': A fragment identifier (named anchor) to append to the URL.
   *     Do not include the leading '#' character.
   *   - 'absolute': Defaults to FALSE. Whether to force the output to be an
   *     absolute link (beginning with http:). Useful for links that will be
   *     displayed outside the site, such as in an RSS feed.
   *   - 'attributes': An associative array of HTML attributes that will be
   *     added to the anchor tag if you use the \Drupal\Core\Link class to make
   *     the link.
   *   - 'language': An optional language object used to look up the alias
   *     for the URL. If $options['language'] is omitted, it defaults to the
   *     current language for the language type LanguageInterface::TYPE_URL.
   *   - 'https': Whether this URL should point to a secure location. If not
   *     defined, the current scheme is used, so the user stays on HTTP or HTTPS
   *     respectively. TRUE enforces HTTPS and FALSE enforces HTTP.

Usage:
    1) $form_state->setRedirect('<current>');
    2) $form_state->setRedirect('entity.view.edit_display_form', [
      'view' => $view->id(),
      'display_id' => $id,
    ]);

  
       2)setRedirectUrl

                    Url object needs to given for the function.

         function setRedirectUrl(Url $url)

   Usage:
        url = new Url('foo');
        url->setRedirectUrl($url)
      



Comments

Popular posts from this blog

Twig in Drupal 8

                                             Twig in Drupal 8 Introduction   In web development , every developer knows  presenting  a content in a  webpage is more important than any  functional logic inside the website whether the site is for tutoring , just a blog or its site for ecommerce products , how your site going to interact with the user plays major role for your business profit . Its a matter of minute to hold your customer/user in online to make a profit. you may thought , it is inappropriate to discuss those  with the title of this post  twig in drupal 8.   twig is used as presenter of your content in an easy way. Templating Engine in drupal 7     For those who are familiar with drupal 7 already know that php is used as templating engine . So its with an advantage of adding much m...

Why drupal is not fully object oriented ?

You can see module hooks are still in a procedural way to  utilize a dependent service  That's why we have a static  Drupal Class , it can get a service in a procedural context. How to access a service in module hooks? Drupal::service('{service id}') or  service accessors like Drupal::request(), Drupal::currentUser(), Drupal::entityManager(). It is providing a uniform way of getting any services inside hooks and it eases the transition from procedural code to injected oops code. How a container get services in drupal?  The container is built by the kernel  and passed to the static Drupal Class and the container gets all the services from the system by the below methods    1. \Drupal\Core\CoreServiceProvider    2.  the service providers of enabled modules     3. any other service providers defined in $GLOBALS['conf']['container_service_providers']. How you can improve the dependent service access from module ?  fu...