Question

Lorsque j'appuie sur le bouton de suppression sur un certain contenu, je suis à une page de confirmation. L'option de suppression est un bouton, tandis que l'option d'annulation est un lien. Cela semble assez bizarre. Je trouve qu'il ya une fonction form_confirm () à l'aide de Drupal, mais je ne comprends pas comment l'utiliser. Est-ce que quelqu'un sait comment faire le lien dans cancel un bouton?

Était-ce utile?

La solution

Ou cela en utilisant pas javascript (et le remplacement eregi () avec 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,
      );

    }
  }

et

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

Autres conseils

La raison pour laquelle l'annulation ressemble lien comme un lien, parce qu'il est un lien <a>, tandis que le bouton de confirmation, est une forme soumettre <input type="submut> élément.

Si vous voulez faire le lien annuler, pour ressembler à un bouton d'envoi, vous pouvez le faire avec CSS pur.

Utilisation hook_form_alter (), essayez ceci:

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

Pour utiliser Drupal 7 I:

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

question a également été rapporté pour Drupal 8, mais l'équipe de base Drupal n'a aucune intention de résoudre le problème dans le noyau. Voir demande de support Drupal changement formulaire de confirmation Annuler lien vers un bouton .

Cordialement, Baso.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top