XmlSitemapCustomDeleteForm.php in XML sitemap 8
File
xmlsitemap_custom/src/Form/XmlSitemapCustomDeleteForm.php
View source
<?php
namespace Drupal\xmlsitemap_custom\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\xmlsitemap\XmlSitemapLinkStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class XmlSitemapCustomDeleteForm extends ConfirmFormBase {
protected $linkStorage;
protected $custom_link;
public function __construct(XmlSitemapLinkStorageInterface $link_storage) {
$this->linkStorage = $link_storage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('xmlsitemap.link_storage'));
}
public function getFormId() {
return 'xmlsitemap_custom_delete';
}
public function buildForm(array $form, FormStateInterface $form_state, $link = '') {
if (!($custom_link = $this->linkStorage
->load('custom', $link))) {
throw new NotFoundHttpException();
}
else {
$this->custom_link = $custom_link;
}
return parent::buildForm($form, $form_state);
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function getCancelUrl() {
return new Url('xmlsitemap_custom.list');
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete %link?', [
'%link' => $this->custom_link['loc'],
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->linkStorage
->delete('custom', $this->custom_link['id']);
$this
->logger('xmlsitemap')
->debug('The custom link for %loc has been deleted.', [
'%loc' => $this->custom_link['loc'],
]);
$this
->messenger()
->addStatus($this
->t('The custom link for %loc has been deleted.', [
'%loc' => $this->custom_link['loc'],
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}