ContentTranslationDeleteForm.php 2.28 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\Form\FormStateInterface;
12
use Drupal\Core\Url;
13 14

/**
15
 * Delete translation form for content_translation module.
16 17 18 19 20 21 22 23 24 25 26 27 28
 */
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.
   *
29
   * @var \Drupal\Core\Language\LanguageInterface
30 31 32 33 34 35 36 37 38 39 40 41 42
   */
  protected $language;

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

  /**
   * {@inheritdoc}
   */
43 44
  public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $language = NULL) {
    $this->entity = $this->getRequest()->attributes->get($entity_type_id);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    $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}
   */
66
  public function getCancelUrl() {
67
    return $this->entity->urlInfo('drupal:content-translation-overview');
68 69 70 71 72
  }

  /**
   * {@inheritdoc}
   */
73
  public function submitForm(array &$form, FormStateInterface $form_state) {
74
    // Remove the translated values.
75
    $this->entity = $this->entity->getUntranslated();
76
    $this->entity->removeTranslation($this->language->getId());
77 78 79 80 81
    $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')) {
82
      $path = $this->entity->getSystemPath();
83
      $conditions = array('source' => $path, 'langcode' => $this->language->getId());
84
      \Drupal::service('path.alias_storage')->delete($conditions);
85 86
    }

87
    $form_state->setRedirectUrl($this->getCancelUrl());
88 89 90
  }

}