Newer
Older
<?php
/**
* @file
* Contains \Drupal\node\Form\DeleteMultiple.
*/
namespace Drupal\node\Form;
Angie Byron
committed
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Url;
use Drupal\Component\Utility\String;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\TempStoreFactory;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a node deletion confirmation form.
*/
Angie Byron
committed
class DeleteMultiple extends ConfirmFormBase {
/**
* The array of nodes to delete.
*
* @var array
*/
protected $nodes = array();
/**
* The tempstore factory.
*
* @var \Drupal\user\TempStoreFactory
*/
protected $tempStoreFactory;
/**
catch
committed
* The node storage.
catch
committed
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $manager;
/**
* Constructs a DeleteMultiple form object.
*
* @param \Drupal\user\TempStoreFactory $temp_store_factory
* The tempstore factory.
Angie Byron
committed
* @param \Drupal\Core\Entity\EntityManagerInterface $manager
* The entity manager.
*/
Angie Byron
committed
public function __construct(TempStoreFactory $temp_store_factory, EntityManagerInterface $manager) {
$this->tempStoreFactory = $temp_store_factory;
catch
committed
$this->storage = $manager->getStorage('node');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('user.tempstore'),
$container->get('entity.manager')
);
}
/**
* {@inheritdoc}
*/
Alex Pott
committed
public function getFormId() {
return 'node_multiple_delete_confirm';
}
/**
* {@inheritdoc}
*/
public 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}
*/
public function getCancelUrl() {
return new Url('system.admin_content');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Delete');
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function buildForm(array $form, FormStateInterface $form_state) {
$this->nodes = $this->tempStoreFactory->get('node_multiple_delete_confirm')->get(\Drupal::currentUser()->id());
if (empty($this->nodes)) {
return new RedirectResponse(url('admin/content', array('absolute' => TRUE)));
}
$form['nodes'] = array(
'#theme' => 'item_list',
'#items' => array_map(function ($node) {
return String::checkPlain($node->label());
}, $this->nodes),
);
$form = parent::buildForm($form, $form_state);
return $form;
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function submitForm(array &$form, FormStateInterface $form_state) {
Alex Pott
committed
if ($form_state->getValue('confirm') && !empty($this->nodes)) {
catch
committed
$this->storage->delete($this->nodes);
$this->tempStoreFactory->get('node_multiple_delete_confirm')->delete(\Drupal::currentUser()->id());
$count = count($this->nodes);
$this->logger('content')->notice('Deleted @count posts.', array('@count' => $count));
drupal_set_message(format_plural($count, 'Deleted 1 post.', 'Deleted @count posts.'));
}
Alex Pott
committed
$form_state->setRedirect('system.admin_content');