Созонов Алексей

Веб-разработчик

Блок с Webform не остается на странице после ошибки

09.08.2015

Webform нормально показывается и сабмититься, но если у вас есть ошибки при проверке он перенаправляет на страницу Webform вместо того, чтобы остаться на странице, где рендерится блок.

function CUSTOM_MODULE_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    // Custom redirection for any webforms you choose
    case 'WEBFORM_FORM_ID':
      // Set a hidden field for the parent node and add submit handler to handle redirect
      if (empty($form_state['values']['redirect_override'])) {
        // The redirect needs a normal path
        $form['redirect_override'] = array(
          '#type' => 'hidden',
          '#value' => serialize($_GET['q']),
        );
        // But the action needs a url() path if errors happen
        $form['#action'] = _CUSTOM_MODULE_get_current_path();
      }
      // Add our custom submit handler LAST so that Webforms doesn't overwrite it with its redirect behaviors
      array_push($form['#submit'], '_CUSTOM_MODULE_webform_redirect_override');

      break;
  }
}

// Custom redirect submission for Webforms
function _CUSTOM_MODULE_webform_redirect_override($form, &$form_state) {
  if (!empty($form_state['values']['redirect_override'])) {
    $form_state['redirect'] = array(unserialize($form_state['values']['redirect_override']));
  }
}

// Get the proper path for the current page
function _CUSTOM_MODULE_get_current_path() {
  $url = drupal_lookup_path('alias', $_GET['q']);
  if (empty($url)) {
    $url = $_GET['q'];
  }
  // Front-page url should be empty
  if ($url == "front") {
    $url = "";
  }
  $url = url($url);
  return $url;
}
#Drupal