How to to redirect a form to a certain node for anonymous users?

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
In a custom module, I have a form that is triggered by an option in a menu.
I'd want this form to be redirected automatically (without action of the user) to a certain node (/node/2) when the user is not authenticated.
In the public function buildForm(array $form, FormStateInterface $form_state) {, I know how to not display the fields of the form for the anonymous user:
$oCurrentUser = Drupal::currentUser();
if ($oCurrentUser->isAnonymous())
else
some fields...
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];
return $form;
I guess that redirection might be done with $form_state->setRedirect('entity.node.canonical', ['node' => 2]);
but I don't know how to make it work...
Any idea?
8 forms
add a comment |
In a custom module, I have a form that is triggered by an option in a menu.
I'd want this form to be redirected automatically (without action of the user) to a certain node (/node/2) when the user is not authenticated.
In the public function buildForm(array $form, FormStateInterface $form_state) {, I know how to not display the fields of the form for the anonymous user:
$oCurrentUser = Drupal::currentUser();
if ($oCurrentUser->isAnonymous())
else
some fields...
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];
return $form;
I guess that redirection might be done with $form_state->setRedirect('entity.node.canonical', ['node' => 2]);
but I don't know how to make it work...
Any idea?
8 forms
add a comment |
In a custom module, I have a form that is triggered by an option in a menu.
I'd want this form to be redirected automatically (without action of the user) to a certain node (/node/2) when the user is not authenticated.
In the public function buildForm(array $form, FormStateInterface $form_state) {, I know how to not display the fields of the form for the anonymous user:
$oCurrentUser = Drupal::currentUser();
if ($oCurrentUser->isAnonymous())
else
some fields...
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];
return $form;
I guess that redirection might be done with $form_state->setRedirect('entity.node.canonical', ['node' => 2]);
but I don't know how to make it work...
Any idea?
8 forms
In a custom module, I have a form that is triggered by an option in a menu.
I'd want this form to be redirected automatically (without action of the user) to a certain node (/node/2) when the user is not authenticated.
In the public function buildForm(array $form, FormStateInterface $form_state) {, I know how to not display the fields of the form for the anonymous user:
$oCurrentUser = Drupal::currentUser();
if ($oCurrentUser->isAnonymous())
else
some fields...
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];
return $form;
I guess that redirection might be done with $form_state->setRedirect('entity.node.canonical', ['node' => 2]);
but I don't know how to make it work...
Any idea?
8 forms
8 forms
asked Mar 12 at 7:48
gbmapogbmapo
437417
437417
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
The class implementing FormInterface is allowed to return an instance of a class extending the Response class, despite FormInterface::buildForm() not documenting it. See what a comment in the FormBuilder::retrieveForm() code says.
Exceptions should not be used for code flow control. However, the Form API currently allows any form builder functions to return a response.
@see https://www.drupal.org/node/2363189
This means that code similar to the following one would actually work.
public function buildForm(array $form, FormStateInterface $form_state)
// Verify the currently logged in user is an anonymous user and redirect.
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
Although, since Do not allow form builder functions to return Response objects (the bug report linked in the comment) is open for Drupal 8.8.x, that code could not work anymore, in future.
See also the following comment in the FormBuilder::buildForm() code.
Exceptions should not be used for code flow control. However, the Form API does not integrate with the HTTP Kernel based architecture of Drupal 8. In order to resolve this issue properly it is necessary to completely separate form submission from rendering.
@see https://www.drupal.org/node/2367555
The linked issue is Deprecate EnforcedResponseException support (a task, not a bug report).
A module that works also after the change described in that issue is implemented would:
- Associate a controller to the route, not a form builder
In the controller method associated with the route, redirect the anonymous users to the node page, or render the form for authenticated users
class RedirectOrFormController extends ControllerBase
public function showForm()
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
return $this->formBuilder()->getForm('Drupal\locale\Form\TranslateEditForm');
Replace 'Drupal\locale\Form\TranslateEditForm' with the fully qualified name of the form builder.
add a comment |
In buildForm() you can return a redirect response the same way as in a controller:
public function buildForm(array $form, FormStateInterface $form_state)
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
// build $form for authenticated users
return $form;
Use $form_state->setRedirect() if you want to redirect in a form submit handler.
Using buildForm() as controller by specifying _form instead of _controller is documented and widely used (not in FormInterface, but on DO: https://www.drupal.org/docs/8/api/routing-system). It's unlikely core will ask devs to replace it by their own controller.
add a comment |
Using kiamlaluno's advice, the route for the menu is now associated to /association/showMembership.
association.membership_showMembership:
path: '/association/showMembership'
defaults:
_controller: 'DrupalassociationControllerMembershipController::showMembership'
_title: 'Membership'
requirements:
_access: 'TRUE'
The controller class uses the following code.
class MembershipController extends ControllerBase
public function showMembership()
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
return $this->formBuilder()->getForm('DrupalassociationFormMembership');
The Membership form doesn't redirect users in buildForm().
add a comment |
yes, you are right. you can redirect to the node using the code that you have mentioned. There is another way to set the redirection check the below code.
use the name space :
use SymfonyComponentHttpFoundationRedirectResponse;
$response = new RedirectResponse(node url);
$response->send();
exit(0);
I don't know what you are trying to achieve hope this code may help you.
1
exits should not be needed
– Kevin
Mar 12 at 12:19
@Kevin is correct:exit()should not be used.
– kiamlaluno♦
Mar 12 at 12:35
This is also not how redirects are done in a form builder.
– kiamlaluno♦
Mar 14 at 13:38
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "220"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f277594%2fhow-to-to-redirect-a-form-to-a-certain-node-for-anonymous-users%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
The class implementing FormInterface is allowed to return an instance of a class extending the Response class, despite FormInterface::buildForm() not documenting it. See what a comment in the FormBuilder::retrieveForm() code says.
Exceptions should not be used for code flow control. However, the Form API currently allows any form builder functions to return a response.
@see https://www.drupal.org/node/2363189
This means that code similar to the following one would actually work.
public function buildForm(array $form, FormStateInterface $form_state)
// Verify the currently logged in user is an anonymous user and redirect.
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
Although, since Do not allow form builder functions to return Response objects (the bug report linked in the comment) is open for Drupal 8.8.x, that code could not work anymore, in future.
See also the following comment in the FormBuilder::buildForm() code.
Exceptions should not be used for code flow control. However, the Form API does not integrate with the HTTP Kernel based architecture of Drupal 8. In order to resolve this issue properly it is necessary to completely separate form submission from rendering.
@see https://www.drupal.org/node/2367555
The linked issue is Deprecate EnforcedResponseException support (a task, not a bug report).
A module that works also after the change described in that issue is implemented would:
- Associate a controller to the route, not a form builder
In the controller method associated with the route, redirect the anonymous users to the node page, or render the form for authenticated users
class RedirectOrFormController extends ControllerBase
public function showForm()
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
return $this->formBuilder()->getForm('Drupal\locale\Form\TranslateEditForm');
Replace 'Drupal\locale\Form\TranslateEditForm' with the fully qualified name of the form builder.
add a comment |
The class implementing FormInterface is allowed to return an instance of a class extending the Response class, despite FormInterface::buildForm() not documenting it. See what a comment in the FormBuilder::retrieveForm() code says.
Exceptions should not be used for code flow control. However, the Form API currently allows any form builder functions to return a response.
@see https://www.drupal.org/node/2363189
This means that code similar to the following one would actually work.
public function buildForm(array $form, FormStateInterface $form_state)
// Verify the currently logged in user is an anonymous user and redirect.
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
Although, since Do not allow form builder functions to return Response objects (the bug report linked in the comment) is open for Drupal 8.8.x, that code could not work anymore, in future.
See also the following comment in the FormBuilder::buildForm() code.
Exceptions should not be used for code flow control. However, the Form API does not integrate with the HTTP Kernel based architecture of Drupal 8. In order to resolve this issue properly it is necessary to completely separate form submission from rendering.
@see https://www.drupal.org/node/2367555
The linked issue is Deprecate EnforcedResponseException support (a task, not a bug report).
A module that works also after the change described in that issue is implemented would:
- Associate a controller to the route, not a form builder
In the controller method associated with the route, redirect the anonymous users to the node page, or render the form for authenticated users
class RedirectOrFormController extends ControllerBase
public function showForm()
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
return $this->formBuilder()->getForm('Drupal\locale\Form\TranslateEditForm');
Replace 'Drupal\locale\Form\TranslateEditForm' with the fully qualified name of the form builder.
add a comment |
The class implementing FormInterface is allowed to return an instance of a class extending the Response class, despite FormInterface::buildForm() not documenting it. See what a comment in the FormBuilder::retrieveForm() code says.
Exceptions should not be used for code flow control. However, the Form API currently allows any form builder functions to return a response.
@see https://www.drupal.org/node/2363189
This means that code similar to the following one would actually work.
public function buildForm(array $form, FormStateInterface $form_state)
// Verify the currently logged in user is an anonymous user and redirect.
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
Although, since Do not allow form builder functions to return Response objects (the bug report linked in the comment) is open for Drupal 8.8.x, that code could not work anymore, in future.
See also the following comment in the FormBuilder::buildForm() code.
Exceptions should not be used for code flow control. However, the Form API does not integrate with the HTTP Kernel based architecture of Drupal 8. In order to resolve this issue properly it is necessary to completely separate form submission from rendering.
@see https://www.drupal.org/node/2367555
The linked issue is Deprecate EnforcedResponseException support (a task, not a bug report).
A module that works also after the change described in that issue is implemented would:
- Associate a controller to the route, not a form builder
In the controller method associated with the route, redirect the anonymous users to the node page, or render the form for authenticated users
class RedirectOrFormController extends ControllerBase
public function showForm()
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
return $this->formBuilder()->getForm('Drupal\locale\Form\TranslateEditForm');
Replace 'Drupal\locale\Form\TranslateEditForm' with the fully qualified name of the form builder.
The class implementing FormInterface is allowed to return an instance of a class extending the Response class, despite FormInterface::buildForm() not documenting it. See what a comment in the FormBuilder::retrieveForm() code says.
Exceptions should not be used for code flow control. However, the Form API currently allows any form builder functions to return a response.
@see https://www.drupal.org/node/2363189
This means that code similar to the following one would actually work.
public function buildForm(array $form, FormStateInterface $form_state)
// Verify the currently logged in user is an anonymous user and redirect.
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
Although, since Do not allow form builder functions to return Response objects (the bug report linked in the comment) is open for Drupal 8.8.x, that code could not work anymore, in future.
See also the following comment in the FormBuilder::buildForm() code.
Exceptions should not be used for code flow control. However, the Form API does not integrate with the HTTP Kernel based architecture of Drupal 8. In order to resolve this issue properly it is necessary to completely separate form submission from rendering.
@see https://www.drupal.org/node/2367555
The linked issue is Deprecate EnforcedResponseException support (a task, not a bug report).
A module that works also after the change described in that issue is implemented would:
- Associate a controller to the route, not a form builder
In the controller method associated with the route, redirect the anonymous users to the node page, or render the form for authenticated users
class RedirectOrFormController extends ControllerBase
public function showForm()
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
return $this->formBuilder()->getForm('Drupal\locale\Form\TranslateEditForm');
Replace 'Drupal\locale\Form\TranslateEditForm' with the fully qualified name of the form builder.
edited Mar 13 at 17:53
answered Mar 12 at 12:31
kiamlaluno♦kiamlaluno
80.9k9132249
80.9k9132249
add a comment |
add a comment |
In buildForm() you can return a redirect response the same way as in a controller:
public function buildForm(array $form, FormStateInterface $form_state)
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
// build $form for authenticated users
return $form;
Use $form_state->setRedirect() if you want to redirect in a form submit handler.
Using buildForm() as controller by specifying _form instead of _controller is documented and widely used (not in FormInterface, but on DO: https://www.drupal.org/docs/8/api/routing-system). It's unlikely core will ask devs to replace it by their own controller.
add a comment |
In buildForm() you can return a redirect response the same way as in a controller:
public function buildForm(array $form, FormStateInterface $form_state)
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
// build $form for authenticated users
return $form;
Use $form_state->setRedirect() if you want to redirect in a form submit handler.
Using buildForm() as controller by specifying _form instead of _controller is documented and widely used (not in FormInterface, but on DO: https://www.drupal.org/docs/8/api/routing-system). It's unlikely core will ask devs to replace it by their own controller.
add a comment |
In buildForm() you can return a redirect response the same way as in a controller:
public function buildForm(array $form, FormStateInterface $form_state)
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
// build $form for authenticated users
return $form;
Use $form_state->setRedirect() if you want to redirect in a form submit handler.
Using buildForm() as controller by specifying _form instead of _controller is documented and widely used (not in FormInterface, but on DO: https://www.drupal.org/docs/8/api/routing-system). It's unlikely core will ask devs to replace it by their own controller.
In buildForm() you can return a redirect response the same way as in a controller:
public function buildForm(array $form, FormStateInterface $form_state)
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
// build $form for authenticated users
return $form;
Use $form_state->setRedirect() if you want to redirect in a form submit handler.
Using buildForm() as controller by specifying _form instead of _controller is documented and widely used (not in FormInterface, but on DO: https://www.drupal.org/docs/8/api/routing-system). It's unlikely core will ask devs to replace it by their own controller.
edited Mar 14 at 7:47
answered Mar 12 at 8:42
4k44k4
53.2k561105
53.2k561105
add a comment |
add a comment |
Using kiamlaluno's advice, the route for the menu is now associated to /association/showMembership.
association.membership_showMembership:
path: '/association/showMembership'
defaults:
_controller: 'DrupalassociationControllerMembershipController::showMembership'
_title: 'Membership'
requirements:
_access: 'TRUE'
The controller class uses the following code.
class MembershipController extends ControllerBase
public function showMembership()
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
return $this->formBuilder()->getForm('DrupalassociationFormMembership');
The Membership form doesn't redirect users in buildForm().
add a comment |
Using kiamlaluno's advice, the route for the menu is now associated to /association/showMembership.
association.membership_showMembership:
path: '/association/showMembership'
defaults:
_controller: 'DrupalassociationControllerMembershipController::showMembership'
_title: 'Membership'
requirements:
_access: 'TRUE'
The controller class uses the following code.
class MembershipController extends ControllerBase
public function showMembership()
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
return $this->formBuilder()->getForm('DrupalassociationFormMembership');
The Membership form doesn't redirect users in buildForm().
add a comment |
Using kiamlaluno's advice, the route for the menu is now associated to /association/showMembership.
association.membership_showMembership:
path: '/association/showMembership'
defaults:
_controller: 'DrupalassociationControllerMembershipController::showMembership'
_title: 'Membership'
requirements:
_access: 'TRUE'
The controller class uses the following code.
class MembershipController extends ControllerBase
public function showMembership()
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
return $this->formBuilder()->getForm('DrupalassociationFormMembership');
The Membership form doesn't redirect users in buildForm().
Using kiamlaluno's advice, the route for the menu is now associated to /association/showMembership.
association.membership_showMembership:
path: '/association/showMembership'
defaults:
_controller: 'DrupalassociationControllerMembershipController::showMembership'
_title: 'Membership'
requirements:
_access: 'TRUE'
The controller class uses the following code.
class MembershipController extends ControllerBase
public function showMembership()
if ($this->currentUser()->isAnonymous())
return $this->redirect('entity.node.canonical', ['node' => 2]);
return $this->formBuilder()->getForm('DrupalassociationFormMembership');
The Membership form doesn't redirect users in buildForm().
edited Mar 14 at 13:42
kiamlaluno♦
80.9k9132249
80.9k9132249
answered Mar 13 at 10:39
gbmapogbmapo
437417
437417
add a comment |
add a comment |
yes, you are right. you can redirect to the node using the code that you have mentioned. There is another way to set the redirection check the below code.
use the name space :
use SymfonyComponentHttpFoundationRedirectResponse;
$response = new RedirectResponse(node url);
$response->send();
exit(0);
I don't know what you are trying to achieve hope this code may help you.
1
exits should not be needed
– Kevin
Mar 12 at 12:19
@Kevin is correct:exit()should not be used.
– kiamlaluno♦
Mar 12 at 12:35
This is also not how redirects are done in a form builder.
– kiamlaluno♦
Mar 14 at 13:38
add a comment |
yes, you are right. you can redirect to the node using the code that you have mentioned. There is another way to set the redirection check the below code.
use the name space :
use SymfonyComponentHttpFoundationRedirectResponse;
$response = new RedirectResponse(node url);
$response->send();
exit(0);
I don't know what you are trying to achieve hope this code may help you.
1
exits should not be needed
– Kevin
Mar 12 at 12:19
@Kevin is correct:exit()should not be used.
– kiamlaluno♦
Mar 12 at 12:35
This is also not how redirects are done in a form builder.
– kiamlaluno♦
Mar 14 at 13:38
add a comment |
yes, you are right. you can redirect to the node using the code that you have mentioned. There is another way to set the redirection check the below code.
use the name space :
use SymfonyComponentHttpFoundationRedirectResponse;
$response = new RedirectResponse(node url);
$response->send();
exit(0);
I don't know what you are trying to achieve hope this code may help you.
yes, you are right. you can redirect to the node using the code that you have mentioned. There is another way to set the redirection check the below code.
use the name space :
use SymfonyComponentHttpFoundationRedirectResponse;
$response = new RedirectResponse(node url);
$response->send();
exit(0);
I don't know what you are trying to achieve hope this code may help you.
answered Mar 12 at 8:48
sekharctcsekharctc
215
215
1
exits should not be needed
– Kevin
Mar 12 at 12:19
@Kevin is correct:exit()should not be used.
– kiamlaluno♦
Mar 12 at 12:35
This is also not how redirects are done in a form builder.
– kiamlaluno♦
Mar 14 at 13:38
add a comment |
1
exits should not be needed
– Kevin
Mar 12 at 12:19
@Kevin is correct:exit()should not be used.
– kiamlaluno♦
Mar 12 at 12:35
This is also not how redirects are done in a form builder.
– kiamlaluno♦
Mar 14 at 13:38
1
1
exits should not be needed
– Kevin
Mar 12 at 12:19
exits should not be needed
– Kevin
Mar 12 at 12:19
@Kevin is correct:
exit() should not be used.– kiamlaluno♦
Mar 12 at 12:35
@Kevin is correct:
exit() should not be used.– kiamlaluno♦
Mar 12 at 12:35
This is also not how redirects are done in a form builder.
– kiamlaluno♦
Mar 14 at 13:38
This is also not how redirects are done in a form builder.
– kiamlaluno♦
Mar 14 at 13:38
add a comment |
Thanks for contributing an answer to Drupal Answers!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f277594%2fhow-to-to-redirect-a-form-to-a-certain-node-for-anonymous-users%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown