ca3ad28aa30de8a47f726cb62fec2663f805fc5e
[yaffs-website] / web / core / modules / config_translation / src / Form / ConfigTranslationDeleteForm.php
1 <?php
2
3 namespace Drupal\config_translation\Form;
4
5 use Drupal\config_translation\ConfigMapperManagerInterface;
6 use Drupal\Core\Cache\Cache;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Form\ConfirmFormBase;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Routing\RouteMatchInterface;
11 use Drupal\Core\Url;
12 use Drupal\language\ConfigurableLanguageManagerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15
16 /**
17  * Builds a form to delete configuration translation.
18  */
19 class ConfigTranslationDeleteForm extends ConfirmFormBase {
20
21   /**
22    * The language manager.
23    *
24    * @var \Drupal\language\ConfigurableLanguageManagerInterface
25    */
26   protected $languageManager;
27
28   /**
29    * The configuration mapper manager.
30    *
31    * @var \Drupal\config_translation\ConfigMapperManagerInterface
32    */
33   protected $configMapperManager;
34
35   /**
36    * The module handler.
37    *
38    * @var \Drupal\Core\Extension\ModuleHandlerInterface
39    */
40   protected $moduleHandler;
41
42   /**
43    * The configuration translation to be deleted.
44    *
45    * @var \Drupal\config_translation\ConfigMapperInterface
46    */
47   protected $mapper;
48
49   /**
50    * The language of configuration translation.
51    *
52    * @var \Drupal\Core\Language\LanguageInterface
53    */
54   protected $language;
55
56   /**
57    * Constructs a ConfigTranslationDeleteForm.
58    *
59    * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
60    *   The language override configuration storage.
61    * @param \Drupal\config_translation\ConfigMapperManagerInterface $config_mapper_manager
62    *   The configuration mapper manager.
63    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
64    *   The module handler.
65    */
66   public function __construct(ConfigurableLanguageManagerInterface $language_manager, ConfigMapperManagerInterface $config_mapper_manager, ModuleHandlerInterface $module_handler) {
67     $this->languageManager = $language_manager;
68     $this->configMapperManager = $config_mapper_manager;
69     $this->moduleHandler = $module_handler;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public static function create(ContainerInterface $container) {
76     return new static(
77       $container->get('language_manager'),
78       $container->get('plugin.manager.config_translation.mapper'),
79       $container->get('module_handler')
80     );
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function getQuestion() {
87     return $this->t('Are you sure you want to delete the @language translation of %label?', ['%label' => $this->mapper->getTitle(), '@language' => $this->language->getName()]);
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function getConfirmText() {
94     return $this->t('Delete');
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function getCancelUrl() {
101     return new Url($this->mapper->getOverviewRouteName(), $this->mapper->getOverviewRouteParameters());
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function getFormId() {
108     return 'config_translation_delete_form';
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL, $plugin_id = NULL, $langcode = NULL) {
115     /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */
116     $mapper = $this->configMapperManager->createInstance($plugin_id);
117     $mapper->populateFromRouteMatch($route_match);
118
119     $language = $this->languageManager->getLanguage($langcode);
120     if (!$language) {
121       throw new NotFoundHttpException();
122     }
123
124     $this->mapper = $mapper;
125     $this->language = $language;
126     return parent::buildForm($form, $form_state);
127   }
128
129   /**
130    * {@inheritdoc}
131    */
132   public function submitForm(array &$form, FormStateInterface $form_state) {
133     foreach ($this->mapper->getConfigNames() as $name) {
134       $this->languageManager->getLanguageConfigOverride($this->language->getId(), $name)->delete();
135     }
136
137     // Flush all persistent caches.
138     $this->moduleHandler->invokeAll('cache_flush');
139     foreach (Cache::getBins() as $cache_backend) {
140       $cache_backend->deleteAll();
141     }
142
143     drupal_set_message($this->t('@language translation of %label was deleted', ['%label' => $this->mapper->getTitle(), '@language' => $this->language->getName()]));
144
145     $form_state->setRedirectUrl($this->getCancelUrl());
146   }
147
148 }