Welcome to our blog, a regular dose of fresh news, events and products about drupal development and not only.
The idea is that when an anonymous user opens a restricted page, he got redirected to the login screen and after he fills up his credentials, he is redirected back to the page he wanted to see in the first place. This can be achieved by using Page Manager module from the CTools package.
Let's play with the Page content type to achieve this.
First of all we need to add a boolean field to define what page has restricted access.
Next we must create a page with restricted access.
Now is the time to set up some Page Manager settings. So in the Admin → Structure → Pages page (admin/structure/pages) we need to enable “Node template”.
Now in the settings of the Page we need to use HTTP Response and Selection rules to attribute this rule to anonymous users only.
After “Create variant” you need to add a selection rule for “anonymous users” only.
Now we add a rule to use our Restricted Access field as following
After you click “Save”, use the “Continue” button to get to the settings of HTTP Response. We must use the 301 Redirect response code. We redirect users to the user/login page with destination variable to the current page. That means that after login form, the submission user will be redirected back to the current page.
Then you “Create variant” and you are all set. Of course people who develop the code could do simple redirect fairly easy in a small custom code, but why we need to if we have these nice Page Manager settings? Hope you have enjoyed it.