DeleteForm.php 1.99 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\path\Form\DeleteForm.
 */

namespace Drupal\path\Form;

10
use Drupal\Core\Form\ConfirmFormBase;
11
use Drupal\Core\Form\FormStateInterface;
12
use Drupal\Core\Path\AliasStorageInterface;
13
use Drupal\Core\Url;
14 15 16 17 18
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Builds the form to delete a path alias.
 */
19
class DeleteForm extends ConfirmFormBase {
20 21

  /**
22
   * The alias storage service.
23
   *
24
   * @var AliasStorageInterface $path
25
   */
26
  protected $aliasStorage;
27 28 29 30 31 32 33 34 35

  /**
   * The path alias being deleted.
   *
   * @var array $pathAlias
   */
  protected $pathAlias;

  /**
36
   * Constructs a \Drupal\path\Form\DeleteForm object.
37
   *
38 39
   * @param \Drupal\Core\Path\AliasStorageInterface $alias_storage
   *   The alias storage service.
40
   */
41 42
  public function __construct(AliasStorageInterface $alias_storage) {
    $this->aliasStorage = $alias_storage;
43 44 45
  }

  /**
46
   * {@inheritdoc}
47 48 49
   */
  public static function create(ContainerInterface $container) {
    return new static(
50
      $container->get('path.alias_storage')
51 52 53 54
    );
  }

  /**
55
   * {@inheritdoc}
56
   */
57
  public function getFormId() {
58 59 60 61 62 63
    return 'path_alias_delete';
  }

  /**
   * Implements \Drupal\Core\Form\ConfirmFormBase::getQuestion().
   */
64
  public function getQuestion() {
65 66 67 68
    return t('Are you sure you want to delete path alias %title?', array('%title' => $this->pathAlias['alias']));
  }

  /**
69
   * {@inheritdoc}
70
   */
71 72
  public function getCancelUrl() {
    return new Url('path.admin_overview');
73 74 75
  }

  /**
76
   * {@inheritdoc}
77
   */
78
  public function buildForm(array $form, FormStateInterface $form_state, $pid = NULL) {
79
    $this->pathAlias = $this->aliasStorage->load(array('pid' => $pid));
80

81 82 83
    $form = parent::buildForm($form, $form_state);

    return $form;
84 85 86
  }

  /**
87
   * {@inheritdoc}
88
   */
89
  public function submitForm(array &$form, FormStateInterface $form_state) {
90
    $this->aliasStorage->delete(array('pid' => $this->pathAlias['pid']));
91

92
    $form_state->setRedirect('path.admin_overview');
93
  }
94

95
}