class DeleteMultiple

Provides a node deletion confirmation form.

Hierarchy

Expanded class hierarchy of DeleteMultiple

1 string reference to 'DeleteMultiple'
node.routing.yml in drupal/core/modules/node/node.routing.yml
drupal/core/modules/node/node.routing.yml

File

drupal/core/modules/node/lib/Drupal/node/Form/DeleteMultiple.php, line 20
Contains \Drupal\node\Form\DeleteMultiple.

Namespace

Drupal\node\Form
View source
class DeleteMultiple extends ConfirmFormBase implements ControllerInterface {

  /**
   * The array of nodes to delete.
   *
   * @var array
   */
  protected $nodes = array();

  /**
   * The tempstore factory.
   *
   * @var \Drupal\user\TempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * The node storage controller.
   *
   * @var \Drupal\Core\Entity\EntityStorageControllerInterface
   */
  protected $manager;

  /**
   * Constructs a DeleteMultiple form object.
   *
   * @param \Drupal\user\TempStoreFactory $temp_store_factory
   *   The tempstore factory.
   * @param \Drupal\Core\Entity\EntityManager $manager
   *   The entity manager.
   */
  public function __construct(TempStoreFactory $temp_store_factory, EntityManager $manager) {
    $this->tempStoreFactory = $temp_store_factory;
    $this->storageController = $manager
      ->getStorageController('node');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('user.tempstore'), $container
      ->get('plugin.manager.entity'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormID() {
    return 'node_multiple_delete_confirm';
  }

  /**
   * {@inheritdoc}
   */
  protected function getQuestion() {
    return format_plural(count($this->nodes), 'Are you sure you want to delete this item?', 'Are you sure you want to delete these items?');
  }

  /**
   * {@inheritdoc}
   */
  protected function getCancelPath() {
    return 'admin/content';
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state) {
    $this->nodes = $this->tempStoreFactory
      ->get('node_multiple_delete_confirm')
      ->get($GLOBALS['user']->uid);
    if (empty($this->nodes)) {
      drupal_goto($this
        ->getCancelPath());
    }
    $form['nodes'] = array(
      '#theme' => 'item_list',
      '#items' => array_map(function ($node) {
        return String::checkPlain($node
          ->label());
      }, $this->nodes),
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, array &$form_state) {
    if ($form_state['values']['confirm'] && !empty($this->nodes)) {
      $this->storageController
        ->delete($this->nodes);
      $this->tempStoreFactory
        ->get('node_multiple_delete_confirm')
        ->delete($GLOBALS['user']->uid);
      $count = count($this->nodes);
      watchdog('content', 'Deleted @count posts.', array(
        '@count' => $count,
      ));
      drupal_set_message(format_plural($count, 'Deleted 1 post.', 'Deleted @count posts.'));
    }
    $form_state['redirect'] = 'admin/content';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfirmFormBase::getCancelText protected function Returns a caption for the link which cancels the action. 2
ConfirmFormBase::getDescription protected function Returns additional text to display as a description. 10
ConfirmFormBase::getFormName protected function Returns the internal name used to refer to the confirmation item.
ConfirmFormBase::validateForm public function Implements \Drupal\Core\Form\FormInterface::validateForm(). Overrides FormInterface::validateForm 1
DeleteMultiple::$manager protected property The node storage controller.
DeleteMultiple::$nodes protected property The array of nodes to delete.
DeleteMultiple::$tempStoreFactory protected property The tempstore factory.
DeleteMultiple::buildForm public function Implements \Drupal\Core\Form\FormInterface::buildForm(). Overrides ConfirmFormBase::buildForm
DeleteMultiple::create public static function Instantiates a new instance of this controller. Overrides ControllerInterface::create
DeleteMultiple::getCancelPath protected function Returns the page to go to if the user cancels the action. Overrides ConfirmFormBase::getCancelPath
DeleteMultiple::getConfirmText protected function Returns a caption for the button that confirms the action. Overrides ConfirmFormBase::getConfirmText
DeleteMultiple::getFormID public function Returns a unique string identifying the form. Overrides FormInterface::getFormID
DeleteMultiple::getQuestion protected function Returns the question to ask the user. Overrides ConfirmFormBase::getQuestion
DeleteMultiple::submitForm public function Form submission handler. Overrides FormInterface::submitForm
DeleteMultiple::__construct public function Constructs a DeleteMultiple form object.