Score:0

Pass a value via URL to a node edit form and use it to redirect after saving the node

cn flag

I have a node that includes a series of embedded views. When displaying, I have to pass a value via URL arguments (http://example.org/node/x?id=y), so that the views can get the appropriate information. The user needs to be able to edit this node, but when they save and return to the node it has lost the id=y part and the display is incomplete.

I know I can use hook_form_alter() to redirect, but I can't figure out how can I redirect after the node is saved using that URL argument?

gmak avatar
cn flag
@apaderno - Thank you for editing my post to make clear.
in flag
What version of Drupal are you using?
gmak avatar
cn flag
I am using Drupal 9
Score:0
us flag

Using hook_form_alter(), you can add a submission handler that sets the redirection. The code for the submission handler would be similar to the following one.

function mymodule_node_edit_submit(array &$form, FormStateInterface $form_state) {
  $node = $form_state->getFormObject()->getEntity();
  $id = \Drupal::request()->query->get('id');
  $form_state->setRedirect('entity.node.canonical', ['node' => $node->id()], [query => ['id' => $id]]);
}

The code for hook_form_BASE_FORM_ID_alter() would simply be the following one.

function mymodule_form_node_form_alter(&$form, FormStateInterface $form_state) {
  $node = $form_state->getFormObject()->getEntity();

  // Since this hook is invoked for every node, check its content type.
  if ($node->getType() == 'the content type you are interested in') {
    $form['actions']['submit']['#submit'][] = 'mymodule_node_edit_submit';
  }
}

The hook_form_FORM_ID_alter() implementation should be named mymodule_form_node_<content_type_machine_name>_alter(). (Replace mymodule with the module machine name, <content_type_machine_name> with the content type machine name.) In this case, since the hook is invoked only for a content type, it doesn't need to check the node content type before adding the submission handler.

gmak avatar
cn flag
Something is not working. If my url is ```http://example.org/node/123?id=57```, after submitting the form it is redirected to ```http://example.org/node/123?id```. So it seems that the id query parameter is not getting added. If I do a ```dpm($id)``` in either of the functions, it shows as NULL - which makes me think that the query is not getting captured from the original node. Have I missed something?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.