0

I have a node form exposed in the front-end to allow visitors to add their organisation to the list of partners. Below the form, I have exposed view block with the list of existing partners.

When a visitor enters a new organisation to the list I want the form just to add a new node type "partner" and reload the page/block without redirecting to the node content (node/[node ID]).

At the beginning I ed taxonomy terms instead and it worked as it should, now I want to achieve the same with node entity.

I have tried various solutions but none of it works correctly and I didn't find much by googling it.

For example, this is not functioning:

function THEME_form_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id) {
  if ($form_id == 'node_partners_form') {
    $form_state->disableRedirect(); // This is not functioning
  }
}

This does not work either:

function THEME_form_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id) {
  if ($form_id == 'node_partners_form') {
    foreach (array_keys($form['actions']) as $action) {
      if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'THEME_node_form_submit';
      }
    }
  }
}

function THEME_node_form_submit($form, &$form_state) {
  $form_state->setRedirect(Url::fromUri('internal:/node/1')->toString());
}

=> Error: Route /partners does not exist.

Value of attribute action in the exposed node form is correct and therefore I think the proper solution should be elsewhere then altering form properties - probably it has more with the Drupal behaviour after adding a new node.

<form action="/en/partners">...</form>
1

2 Answers 2

0

Thank you for the comment 4k4. Unfortunately, I didn't have enough time to find a proper solution so I just changed the redirection URL to the same content where the form is exposed.

function THEME_node_form_submit($form, FormStateInterface $form_state)  {
    $node_id = 1;
    $form_state->setRedirect('entity.node.canonical', array('node' => $node_id));
}

I'll try to find a more appropriate solution if I'll have some extra time before deadline :)

0

I don't know you form code in particular, but another thought in finding a solution might be to modify the form action on-the-fly like this to get what you want:

public function submitForm(array &$form, FormStateInterface $form_state) {
    global $base_url;
    $form['#action'] = $base_url . '/whatever_you_like';

This might be a little extraordinary, but it might help in some special use cases depinding on your code and I must admit I did not test it with:

function THEME_node_form_submit($form, FormStateInterface $form_state)

Good luck :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.