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

/**
 * @file
 * Contains \Drupal\content_translation\Form\ContentTranslationDeleteForm.
 */

namespace Drupal\content_translation\Form;

use Drupal\Core\Form\ConfirmFormBase;
11
use Drupal\Core\Url;
12 13

/**
14
 * Delete translation form for content_translation module.
15 16 17 18 19 20 21 22 23 24 25 26 27
 */
class ContentTranslationDeleteForm extends ConfirmFormBase {

  /**
   * The entity whose translation is being deleted.
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  protected $entity;

  /**
   * The language of the translation being deleted.
   *
28
   * @var \Drupal\Core\Language\LanguageInterface
29 30 31 32 33 34 35 36 37 38 39 40 41
   */
  protected $language;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'content_translation_delete_confirm';
  }

  /**
   * {@inheritdoc}
   */
42 43
  public function buildForm(array $form, array &$form_state, $_entity_type_id = NULL, $language = NULL) {
    $this->entity = $this->getRequest()->attributes->get($_entity_type_id);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    $this->language = language_load($language);
    return parent::buildForm($form, $form_state);
  }

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

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

  /**
   * {@inheritdoc}
   */
65
  public function getCancelUrl() {
66
    return $this->entity->urlInfo('drupal:content-translation-overview');
67 68 69 70 71 72 73 74 75 76 77 78 79
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, array &$form_state) {
    // Remove the translated values.
    $this->entity->removeTranslation($this->language->id);
    $this->entity->save();

    // Remove any existing path alias for the removed translation.
    // @todo This should be taken care of by the Path module.
    if (\Drupal::moduleHandler()->moduleExists('path')) {
80 81
      $path = $this->entity->getSystemPath();
      $conditions = array('source' => $path, 'langcode' => $this->language->id);
82
      \Drupal::service('path.alias_storage')->delete($conditions);
83 84
    }

85
    $form_state['redirect_route'] = $this->getCancelUrl();
86 87 88
  }

}