privateTempStoreFactory = $temp_store_factory; $this->redirectStorage = $entity_type_manager->getStorage('redirect'); $this->currentUser = $account; $this->setStringTranslation($string_translation); } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('user.private_tempstore'), $container->get('entity_type.manager'), $container->get('current_user'), $container->get('string_translation') ); } /** * {@inheritdoc} */ public function getFormId() { return 'redirect_multiple_delete_confirm'; } /** * {@inheritdoc} */ public function getQuestion() { return $this->formatPlural(count($this->redirects), 'Are you sure you want to delete this redirect?', 'Are you sure you want to delete these redirects?'); } /** * {@inheritdoc} */ public function getCancelUrl() { return new Url('redirect.list'); } /** * {@inheritdoc} */ public function getConfirmText() { return $this->t('Delete'); } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $this->redirects = $this->privateTempStoreFactory->get('redirect_multiple_delete_confirm')->get($this->currentUser->id()); if (empty($this->redirects)) { return new RedirectResponse($this->getCancelUrl()->setAbsolute()->toString()); } $form['redirects'] = [ '#theme' => 'item_list', '#items' => array_map(function ($redirect) { return $redirect->label(); }, $this->redirects), ]; return parent::buildForm($form, $form_state); } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { if ($form_state->getValue('confirm') && !empty($this->redirects)) { $this->redirectStorage->delete($this->redirects); $this->privateTempStoreFactory->get('redirect_multiple_delete_confirm')->delete($this->currentUser->id()); $count = count($this->redirects); $this->logger('redirect')->notice('Deleted @count redirects.', ['@count' => $count]); drupal_set_message($this->stringTranslation->formatPlural($count, 'Deleted 1 redirect.', 'Deleted @count redirects.')); } $form_state->setRedirect('redirect.list'); } }