DeleteForm.php 2.02 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\DependencyInjection\ContainerInjectionInterface;
11
use Drupal\Core\Form\ConfirmFormBase;
12 13 14 15 16 17
use Drupal\Core\Path\Path;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Builds the form to delete a path alias.
 */
18
class DeleteForm extends ConfirmFormBase implements ContainerInjectionInterface {
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

  /**
   * The path crud service.
   *
   * @var Path $path
   */
  protected $path;

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

  /**
   * Constructs a \Drupal\Core\Path\Path object.
   *
   * @param \Drupal\Core\Path\Path $path
   *   The path crud service.
   */
  public function __construct(Path $path) {
    $this->path = $path;
  }

  /**
45
   * {@inheritdoc}
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('path.crud')
    );
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::getFormID().
   */
  public function getFormID() {
    return 'path_alias_delete';
  }

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

  /**
   * Implements \Drupal\Core\Form\ConfirmFormBase::getCancelPath().
   */
70
  public function getCancelPath() {
71 72 73 74 75 76
    return 'admin/config/search/path';
  }

  /**
   * Overrides \Drupal\Core\Form\ConfirmFormBase::buildForm().
   */
77
  public function buildForm(array $form, array &$form_state, $pid = NULL) {
78 79
    $this->pathAlias = $this->path->load(array('pid' => $pid));

80
    return parent::buildForm($form, $form_state);
81 82 83 84 85 86 87 88 89 90 91
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::submitForm().
   */
  public function submitForm(array &$form, array &$form_state) {
    $this->path->delete(array('pid' => $this->pathAlias['pid']));

    $form_state['redirect'] = 'admin/config/search/path';
  }
}