Frage

Wenn ich auf die Schaltfläche Löschen auf einige Inhalte drücken, bin ich auf eine Bestätigungsseite. Die Löschoption ist eine Taste, während die Löschoption ein Link ist. Das sieht ziemlich komisch. Ich fand, dass es eine form_confirm () Funktion in Drupal, aber ich kann nicht verstehen, wie es zu benutzen. Weiß jemand, wie man die Verbindung in eine Taste abbrechen?

War es hilfreich?

Lösung

oder dies kein mit Hilfe von Javascript (und Ersetzen eregi () mit 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,
      );

    }
  }

und

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

Andere Tipps

Der Grund der Aufhebungs Link sieht aus wie ein Link, ist, weil es ein Link <a>, während der Bestätigungstaste ist, ist ein Formularelement <input type="submut> einreichen.

Wenn Sie die Abbrechen-Link machen wollen, um wie ein Submit-Button, können Sie das tun mit reinem CSS.

Mit hook_form_alter (), versuchen Sie dies:

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;';
    }
  }

Für Drupal 7 I Verwendung:

/**
 * 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;
  }
}

Ausgabe auch ist für Drupal 8, aber Drupal Core-Team hat berichtet, nicht die Absicht, das Thema im Kern zu lösen. Siehe Drupal Support-Anfrage ändern Bestätigungsformular Link Abbrechen einer Schaltfläche .

Mit freundlichen Grüßen, Baso.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top