Drupal - Сделайте ссылку «Отмена» на странице «Подтверждение»

StackOverflow https://stackoverflow.com/questions/3487470

  •  28-09-2019
  •  | 
  •  

Вопрос

Когда я нажимаю кнопку Удалить в некотором контентах, я доставлен на страницу подтверждения. Опция удаления - это кнопка, а параметр «Отмена» - это ссылка. Это выглядит довольно странно. Я обнаружил, что в Drupal функция form_confirm () (), но я не могу понять, как его использовать. Кто-нибудь знает, как сделать ссылку «Отмена» в кнопку?

Это было полезно?

Решение

Или это не использует JavaScript (и замена EREGI () с preg_match () ...

  if ( $form['#theme'] == 'confirm_form' ) {
    $no = $form['actions']['cancel']['#value'];
    if (!is_null($no)) {
      // Get the text to put on the cancel button
      $value = preg_replace('/(<\/?)(\w+)([^>]*>)/e', '', $no);
      preg_match('/href\s*=\s*\"([^\"]+)\"/', $no, $href);
      $form['actions']['cancel']['#value'] = '';
      $form['href']=array(
        '#type'=>'value',
        '#value'=>$href[1],
      );

      // Add our own button
      $form['actions']['docancel'] = array(
        '#type' => 'submit',
        '#name' => 'cancel',
        '#submit' => array('mymodule_confirm_form_cancel'),
        '#value' => $value,
      );

    }
  }

и

function mymodule_confirm_form_cancel(&$form,&$form_state) {
  $href=$form['href']['#value'];
  if ( !is_null($href) ) {
    $form['#redirect']=$href;
  }
}

Другие советы

Причина, по которой ссылка «Отмена» выглядит как ссылка, это потому, что это ссылка <a>, в то время как кнопка подтверждения является элементом отправки формы <input type="submut>.

Если вы хотите сделать ссылку «Отмена», выглядеть как кнопка «Отправить», вы можете сделать это с чистыми CSS.

Использование Cook_form_alter (), попробуйте следующее:

if($form['#theme'] == 'confirm_form') {
    $no = $form['actions']['cancel']['#value'];
    if (!is_null($no)) {
      // Get the text to put on the cancel button
      $value = preg_replace('/(<\/?)(\w+)([^>]*>)/e', '', $no);
      eregi('m|href\s*=\s*\"([^\"]+)\"|ig', $no, $href);

      $form['actions']['cancel']['#value'] = '';

      // Add our own button
      $form['actions']['docancel'] = array(
        '#type' => 'button',
        '#button_type' => 'reset',
        '#name' => 'cancel',
        '#submit' => 'false',
        '#value' => $value,
        '#attributes' => array(
          'onclick' => '$(this).parents("form").attr("allowSubmission", "false");window.location = "'.$href[1].'";',
        ),
      );
      // Prevent the form submission via our button
      $form['#attributes']['onsubmit'] = 'if ($(this).attr("allowSubmission") == "false") return false;';
    }
  }

Для Drupal 7 я использую:

/**
 * Implements hook_form_alter().
 */
function yourmodule_form_alter(&$form, $form_state, $form_id) {
  // Change 'cancel' link to 'cancel' button. 
  if ( $form['#theme'] == 'confirm_form' ) {
    if ($form['actions']['cancel']['#type'] == 'link') {
      $title = $form['actions']['cancel']['#title'];
      $href = $form['actions']['cancel']['#href'];
      if (!is_null($title) and !is_null($href)) {
        // Disable Cancel link.
        $form['actions']['cancel']['#title'] = '';
        // Add our own Cancel button.
        $form['actions']['docancel'] = array(
          '#type' => 'submit',
          '#name' => 'cancel',
          '#submit' => array('yourmodule_confirm_form_cancel'),
          '#value' => $title,
        );
      }
    }
  }
}

/**
 * Redirect to previous page after confirm form cancel().
 */
function yourmodule_confirm_form_cancel(&$form, &$form_state) {
  $href = $form['actions']['cancel']['#href'];
  if (!is_null($href)) {
    $form_state['redirect'] = $href;
  }
}

Проблема также сообщается о Drupal 8, но команда Drupal Core не намерена решить проблему в ядре. Смотрите запрос поддержки Drupal Изменить форму подтверждения Отмена ссылки на кнопку.

С уважением, базо.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top