1

I want to do array_unshift for submit function in D8. Array_unshift working perfectly for validate function but not submit function. Below is my code:

function example_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id == "commerce_order_default_edit_form") {
    array_unshift($form['#validate'], 'example_manual_validate');
    array_unshift($form['#submit'], 'example_manual_submit');
  }
}
function example_manual_validate(&$form, \Drupal\Core\Form\FormStateInterface &$form_state) {
  $values = $form_state->getValues();
  dsm($values);
}

function example_manual_submit(&$form, \Drupal\Core\Form\FormStateInterface &$form_state) {
  $values = $form_state->getValues();
  dsm($values);
}
  • Drupal doesn't change the way array_unshift() works. What could eventually happen is that you add submission handlers to $form['#submit'] when the form is using something else, like $form['actions']['submit']['#submit'], for example. – kiamlaluno Apr 28 '16 at 17:41
2

Here am altering the submit handler of the article content type while creating new article type contents

  /**
     * Implements hook_form_alter().
     */
    function example_module_form_alter(&$form, FormStateInterface $form_state,$form_id) {
   // dsm($form_id);

      switch ($form_id) {

        case 'node_article_form':      // New article nodes.
        // Attach our custom submit handler.
     array_unshift( $form['actions']['publish']['#submit'], 'example_module_node_article_form_submit');
          break;

      }

    }

    function example_module_node_article_form_submit($form, FormStateInterface $form_state) {
      drupal_set_message('custom submit ...');
    }

Source

0

Found Solution to alter submit function in D8 , this will be helpful https://www.drupal.org/node/2637958

function mymodule_form_node_form_alter(&$form, FormStateInterface $form_state) {
    foreach (array_keys($form['actions']) as $action) {
      if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'mymodule_node_form_submit';
      }
    }
}
function mymodule_node_form_submit($form, FormStateInterface $form_state) {
   $node = $form_state->getFormObject()->getEntity();
  // mymodule_do_something($node);
 \Drupal::logger(' mymodule')->notice('mymodule submit ') ;

}
  • 3
    May you quote the relevant parts? Answers should not rely on off-site resources, to be understandable. – kiamlaluno Apr 28 '16 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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