Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

There is now an abstract base class, ConfirmFormBase, that implements FormInterface with specific methods that represent the parameters used by confirm_form().

The confirm_form() function has thus been removed.

Read more about FormInterface here: https://www.drupal.org/node/1932058

You can see a list of forms implementing ConfirmFormBase in core here: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21Co...

Drupal 7

function mymodule_delete_form($form, &$form_state, $id) {
  $form_state['mymodule_id'] = $id;
  return confirm_form($form,
    t('Do you want to delete %id?', array('%id' => $id)),
    'admin/config/mymodule',
    t('Only do this if you are sure!'),
    t('Delete it!'),
    t('Nevermind')
  );
}

function mymodule_delete_form_submit($form, &$form_state) {
  mymodule_delete($form_state['delete']);
}

Drupal 8


namespace Drupal\my_module\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Defines a confirmation form for deleting mymodule data.
 */
class MymoduleDeleteForm extends ConfirmFormBase {

  /**
   * The ID of the item to delete.
   *
   * @var string
   */
  protected $id;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'my_module_delete';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return t('Do you want to delete %id?', array('%id' => $this->id));
  }

  /**
   * {@inheritdoc}
   */
    public function getCancelUrl() {
      return new Url('my_module.myroute');
  }

  /**
   * {@inheritdoc}
   */
    public function getDescription() {
    return t('Only do this if you are sure!');
  }

  /**
   * {@inheritdoc}
   */
    public function getConfirmText() {
    return t('Delete it!');
  }

  /**
   * {@inheritdoc}
   */
    public function getCancelText() {
    return t('Nevermind');
  }

  /**
   * {@inheritdoc}
   *
   * @param int $id
   *   (optional) The ID of the item to be deleted.
   */
  public function buildForm(array $form, FormStateInterface $form_state, $pid = NULL) {
    $this->id = $id;
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    mymodule_delete($this->id);
  }

}

If you are implementing a confirm form for an entity using _entity_form this is even easier.
Drupal\Core\Entity\EntityConfirmFormBase will store the entity in a property called entity. Which means you only have to do something like this:


namespace Drupal\mymodule;

use Drupal\Core\Entity\EntityConfirmFormBase;

class MyEntityDeleteForm extends EntityConfirmFormBase {

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return t('Are you sure you want to delete myentity %myentity?', array('%myentity' => $this->entity->label()));
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return t('Delete');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelRoute() {
    return array(
      'route_name' => 'myentity.admin',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function submit(array $form, array &$form_state) {
    $this->entity->delete();
  }

}
Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done

Comments

mikejw’s picture

buildForm and submitForm signatures are out of date and need to match FormInterface. Also missing the getFormId() function.