LanguageDeleteForm.php 3.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\language\Form\LanguageDeleteForm.
 */

namespace Drupal\language\Form;

use Drupal\Core\Entity\EntityConfirmFormBase;
11
use Drupal\Core\Routing\UrlGeneratorInterface;
12 13 14 15 16 17 18 19 20 21 22 23 24
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Defines a confirmation form for deleting a language entity.
 */
class LanguageDeleteForm extends EntityConfirmFormBase {

  /**
   * The urlGenerator service.
   *
25
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
26 27 28 29 30 31
   */
  protected $urlGenerator;

  /**
   * Constructs a new LanguageDeleteForm object.
   *
32
   * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
33 34
   *   The url generator service.
   */
35
  public function __construct(UrlGeneratorInterface $url_generator) {
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    $this->urlGenerator = $url_generator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('url_generator')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this->t('Are you sure you want to delete the language %language?', array('%language' => $this->entity->label()));
  }

  /**
   * {@inheritdoc}
   */
58 59
  public function getCancelRoute() {
    return array(
60
      'route_name' => 'language.admin_overview',
61
    );
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->t('Deleting a language will remove all interface translations associated with it, and content in this language will be set to be language neutral. This action cannot be undone.');
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t('Delete');
  }

  /**
   * {@inheritdoc}
   */
81
  public function getFormId() {
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    return 'language_delete_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state) {
    $langcode = $this->entity->id();

    // Warn and redirect user when attempting to delete the default language.
    if (language_default()->id == $langcode) {
      drupal_set_message($this->t('The default language cannot be deleted.'));
      $url = $this->urlGenerator->generateFromPath('admin/config/regional/language', array('absolute' => TRUE));
      return new RedirectResponse($url);
    }

    // Throw a 404 when attempting to delete a non-existing language.
    $languages = language_list();
    if (!isset($languages[$langcode])) {
      throw new NotFoundHttpException();
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submit(array $form, array &$form_state) {
    // @todo This should be replaced with $this->entity->delete() when the
    //   additional logic in language_delete() is ported.
    $success = language_delete($this->entity->id());

    if ($success) {
      drupal_set_message($this->t('The %language (%langcode) language has been removed.', array('%language' => $this->entity->label(), '%langcode' => $this->entity->id())));
    }

    $form_state['redirect'] = 'admin/config/regional/language';
  }

}