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

Things You Must Know Before Trying Forms In Drupal 8.

 You Should Invest In Forms                      What does your customer think?  What do they looking for? will decide the success of any business.    In a digital marketing, Most of the customer requirement is gathered through forms. So its   essential  to spend some  time with forms Example : a user can search a particular product using the search form on an e-commerce website. Form API in Drupal 8          As we know already Drupal use forms for most of the administrative site building. Drupal 8 follows object-oriented style of programming with the integration of symfony . Based on the purpose of the form, it can inherit the basic properties and methods of the below classes. 1.Config Base 2.FormBase 3.ConfirmBase   Note : Routing configuration is done through the yml files in drupal 8 When the user hits the va...

What is Drupal ? What is Content Management System??

Before  Diving into Drupal 8 Learning ,                                                One who must know what is drupal . What is Drupal ? Drupal  is one among the Content Managment System . Then next rising  question is What is   Content Management System  . ?   To explain about Content Mangement System lets start with an example If you want to sell your products in online , you can sell by passing some of the information about your product in web by giving Name of the Product Image of the Product  Price of the Product ....etc As a whole you are describing your content For managing large amount of data like this you need a system for flexibility  and ease of use So you may think like developing a system from scratch to sell a product Ohhhhh !!!!!!!!!!!!!!!!!!!!!!!Nooooooooooooooooo!!!! ...