DeleteMultiple.php in Feeds 8.3
File
src/Form/DeleteMultiple.php
View source
<?php
namespace Drupal\feeds\Form;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
use Drupal\feeds\FeedStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class DeleteMultiple extends ConfirmFormBase {
protected $feeds = [];
protected $tempStoreFactory;
protected $storage;
protected $user;
public function __construct(PrivateTempStoreFactory $temp_store_factory, FeedStorageInterface $storage, AccountInterface $user) {
$this->tempStoreFactory = $temp_store_factory;
$this->storage = $storage;
$this->user = $user;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.private'), $container
->get('entity_type.manager')
->getStorage('feeds_feed'), $container
->get('current_user'));
}
public function getFormId() {
return 'feeds_feed_multiple_delete_confirm';
}
public function getQuestion() {
return $this
->formatPlural(count($this->feeds), 'Are you sure you want to delete this item?', 'Are you sure you want to delete these items?');
}
public function getCancelUrl() {
return new Url('feeds.admin');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$this->feeds = $this->tempStoreFactory
->get('feeds_feed_multiple_delete_confirm')
->get($this->user
->id());
if (empty($this->feeds)) {
return new RedirectResponse($this
->getCancelUrl()
->setAbsolute()
->toString());
}
$form['feeds'] = [
'#theme' => 'item_list',
'#items' => array_map(function ($feed) {
return Html::escape($feed
->label());
}, $this->feeds),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('confirm') && !empty($this->feeds)) {
$this->storage
->delete($this->feeds);
$this->tempStoreFactory
->get('feeds_feed_multiple_delete_confirm')
->delete($this->user
->id());
$count = count($this->feeds);
$this
->logger('feeds')
->notice('Deleted @count feeds.', [
'@count' => $count,
]);
$this
->messenger()
->addMessage($this
->formatPlural($count, 'Deleted 1 feed.', 'Deleted @count feeds.'));
}
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}