PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/modules/language/src/Form/NegotiationConfigureForm.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 338 lines | 196 code | 44 blank | 98 comment | 13 complexity | c67dc4566072a30090d0fe86871bcd09 MD5 | raw file
  1. <?php
  2. namespace Drupal\language\Form;
  3. use Drupal\Core\Block\BlockManagerInterface;
  4. use Drupal\Core\Config\ConfigFactoryInterface;
  5. use Drupal\Core\Entity\EntityStorageInterface;
  6. use Drupal\Core\Extension\ThemeHandlerInterface;
  7. use Drupal\Core\Form\ConfigFormBase;
  8. use Drupal\Core\Form\FormStateInterface;
  9. use Drupal\Core\Url;
  10. use Drupal\language\ConfigurableLanguageManagerInterface;
  11. use Drupal\language\LanguageNegotiatorInterface;
  12. use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. /**
  15. * Configure the selected language negotiation method for this site.
  16. *
  17. * @internal
  18. */
  19. class NegotiationConfigureForm extends ConfigFormBase {
  20. /**
  21. * Stores the configuration object for language.types.
  22. *
  23. * @var \Drupal\Core\Config\Config
  24. */
  25. protected $languageTypes;
  26. /**
  27. * The language manager.
  28. *
  29. * @var \Drupal\language\ConfigurableLanguageManagerInterface
  30. */
  31. protected $languageManager;
  32. /**
  33. * The language negotiator.
  34. *
  35. * @var \Drupal\language\LanguageNegotiatorInterface
  36. */
  37. protected $negotiator;
  38. /**
  39. * The block manager.
  40. *
  41. * @var \Drupal\Core\Block\BlockManagerInterface
  42. */
  43. protected $blockManager;
  44. /**
  45. * The block storage.
  46. *
  47. * @var \Drupal\Core\Entity\EntityStorageInterface|null
  48. */
  49. protected $blockStorage;
  50. /**
  51. * The theme handler.
  52. *
  53. * @var \Drupal\Core\Extension\ThemeHandlerInterface
  54. */
  55. protected $themeHandler;
  56. /**
  57. * Constructs a NegotiationConfigureForm object.
  58. *
  59. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  60. * The factory for configuration objects.
  61. * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
  62. * The language manager.
  63. * @param \Drupal\language\LanguageNegotiatorInterface $negotiator
  64. * The language negotiation methods manager.
  65. * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
  66. * The block manager.
  67. * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
  68. * The theme handler.
  69. * @param \Drupal\Core\Entity\EntityStorageInterface $block_storage
  70. * The block storage, or NULL if not available.
  71. */
  72. public function __construct(ConfigFactoryInterface $config_factory, ConfigurableLanguageManagerInterface $language_manager, LanguageNegotiatorInterface $negotiator, BlockManagerInterface $block_manager, ThemeHandlerInterface $theme_handler, EntityStorageInterface $block_storage = NULL) {
  73. parent::__construct($config_factory);
  74. $this->languageTypes = $this->config('language.types');
  75. $this->languageManager = $language_manager;
  76. $this->negotiator = $negotiator;
  77. $this->blockManager = $block_manager;
  78. $this->themeHandler = $theme_handler;
  79. $this->blockStorage = $block_storage;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public static function create(ContainerInterface $container) {
  85. $entity_type_manager = $container->get('entity_type.manager');
  86. $block_storage = $entity_type_manager->hasHandler('block', 'storage') ? $entity_type_manager->getStorage('block') : NULL;
  87. return new static(
  88. $container->get('config.factory'),
  89. $container->get('language_manager'),
  90. $container->get('language_negotiator'),
  91. $container->get('plugin.manager.block'),
  92. $container->get('theme_handler'),
  93. $block_storage
  94. );
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function getFormId() {
  100. return 'language_negotiation_configure_form';
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. protected function getEditableConfigNames() {
  106. return ['language.types'];
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function buildForm(array $form, FormStateInterface $form_state) {
  112. $configurable = $this->languageTypes->get('configurable');
  113. $form = [
  114. '#theme' => 'language_negotiation_configure_form',
  115. '#language_types_info' => $this->languageManager->getDefinedLanguageTypesInfo(),
  116. '#language_negotiation_info' => $this->negotiator->getNegotiationMethods(),
  117. ];
  118. $form['#language_types'] = [];
  119. foreach ($form['#language_types_info'] as $type => $info) {
  120. // Show locked language types only if they are configurable.
  121. if (empty($info['locked']) || in_array($type, $configurable)) {
  122. $form['#language_types'][] = $type;
  123. }
  124. }
  125. foreach ($form['#language_types'] as $type) {
  126. $this->configureFormTable($form, $type);
  127. }
  128. $form['actions'] = ['#type' => 'actions'];
  129. $form['actions']['submit'] = [
  130. '#type' => 'submit',
  131. '#button_type' => 'primary',
  132. '#value' => $this->t('Save settings'),
  133. ];
  134. return $form;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function submitForm(array &$form, FormStateInterface $form_state) {
  140. $configurable_types = $form['#language_types'];
  141. $stored_values = $this->languageTypes->get('configurable');
  142. $customized = [];
  143. $method_weights_type = [];
  144. foreach ($configurable_types as $type) {
  145. $customized[$type] = in_array($type, $stored_values);
  146. $method_weights = [];
  147. $enabled_methods = $form_state->getValue([$type, 'enabled']);
  148. $enabled_methods[LanguageNegotiationSelected::METHOD_ID] = TRUE;
  149. $method_weights_input = $form_state->getValue([$type, 'weight']);
  150. if ($form_state->hasValue([$type, 'configurable'])) {
  151. $customized[$type] = !$form_state->isValueEmpty([$type, 'configurable']);
  152. }
  153. foreach ($method_weights_input as $method_id => $weight) {
  154. if ($enabled_methods[$method_id]) {
  155. $method_weights[$method_id] = $weight;
  156. }
  157. }
  158. $method_weights_type[$type] = $method_weights;
  159. $this->languageTypes->set('negotiation.' . $type . '.method_weights', $method_weights_input)->save();
  160. }
  161. // Update non-configurable language types and the related language
  162. // negotiation configuration.
  163. $this->negotiator->updateConfiguration(array_keys(array_filter($customized)));
  164. // Update the language negotiations after setting the configurability.
  165. foreach ($method_weights_type as $type => $method_weights) {
  166. $this->negotiator->saveConfiguration($type, $method_weights);
  167. }
  168. // Clear block definitions cache since the available blocks and their names
  169. // may have been changed based on the configurable types.
  170. if ($this->blockStorage) {
  171. // If there is an active language switcher for a language type that has
  172. // been made not configurable, deactivate it first.
  173. $non_configurable = array_keys(array_diff($customized, array_filter($customized)));
  174. $this->disableLanguageSwitcher($non_configurable);
  175. }
  176. $this->blockManager->clearCachedDefinitions();
  177. $form_state->setRedirect('language.negotiation');
  178. $this->messenger()->addStatus($this->t('Language detection configuration saved.'));
  179. }
  180. /**
  181. * Builds a language negotiation method configuration table.
  182. *
  183. * @param array $form
  184. * The language negotiation configuration form.
  185. * @param string $type
  186. * The language type to generate the table for.
  187. */
  188. protected function configureFormTable(array &$form, $type) {
  189. $info = $form['#language_types_info'][$type];
  190. $table_form = [
  191. '#title' => $this->t('@type language detection', ['@type' => $info['name']]),
  192. '#tree' => TRUE,
  193. '#description' => $info['description'],
  194. '#language_negotiation_info' => [],
  195. '#show_operations' => FALSE,
  196. 'weight' => ['#tree' => TRUE],
  197. ];
  198. // Only show configurability checkbox for the unlocked language types.
  199. if (empty($info['locked'])) {
  200. $configurable = $this->languageTypes->get('configurable');
  201. $table_form['configurable'] = [
  202. '#type' => 'checkbox',
  203. '#title' => $this->t('Customize %language_name language detection to differ from Interface text language detection settings', ['%language_name' => $info['name']]),
  204. '#default_value' => in_array($type, $configurable),
  205. '#attributes' => ['class' => ['language-customization-checkbox']],
  206. '#attached' => [
  207. 'library' => [
  208. 'language/drupal.language.admin',
  209. ],
  210. ],
  211. ];
  212. }
  213. $negotiation_info = $form['#language_negotiation_info'];
  214. $enabled_methods = $this->languageTypes->get('negotiation.' . $type . '.enabled') ?: [];
  215. $methods_weight = $this->languageTypes->get('negotiation.' . $type . '.method_weights') ?: [];
  216. // Add missing data to the methods lists.
  217. foreach ($negotiation_info as $method_id => $method) {
  218. if (!isset($methods_weight[$method_id])) {
  219. $methods_weight[$method_id] = $method['weight'] ?? 0;
  220. }
  221. }
  222. // Order methods list by weight.
  223. asort($methods_weight);
  224. foreach ($methods_weight as $method_id => $weight) {
  225. // A language method might be no more available if the defining module has
  226. // been disabled after the last configuration saving.
  227. if (!isset($negotiation_info[$method_id])) {
  228. continue;
  229. }
  230. $enabled = isset($enabled_methods[$method_id]);
  231. $method = $negotiation_info[$method_id];
  232. // List the method only if the current type is defined in its 'types' key.
  233. // If it is not defined default to all the configurable language types.
  234. $types = array_flip($method['types'] ?? $form['#language_types']);
  235. if (isset($types[$type])) {
  236. $table_form['#language_negotiation_info'][$method_id] = $method;
  237. $method_name = $method['name'];
  238. $table_form['weight'][$method_id] = [
  239. '#type' => 'weight',
  240. '#title' => $this->t('Weight for @title language detection method', ['@title' => mb_strtolower($method_name)]),
  241. '#title_display' => 'invisible',
  242. '#default_value' => $weight,
  243. '#attributes' => ['class' => ["language-method-weight-$type"]],
  244. '#delta' => 20,
  245. ];
  246. $table_form['title'][$method_id] = ['#plain_text' => $method_name];
  247. $table_form['enabled'][$method_id] = [
  248. '#type' => 'checkbox',
  249. '#title' => $this->t('Enable @title language detection method', ['@title' => mb_strtolower($method_name)]),
  250. '#title_display' => 'invisible',
  251. '#default_value' => $enabled,
  252. ];
  253. if ($method_id === LanguageNegotiationSelected::METHOD_ID) {
  254. $table_form['enabled'][$method_id]['#default_value'] = TRUE;
  255. $table_form['enabled'][$method_id]['#attributes'] = ['disabled' => 'disabled'];
  256. }
  257. $table_form['description'][$method_id] = ['#markup' => $method['description']];
  258. $config_op = [];
  259. if (isset($method['config_route_name'])) {
  260. $config_op['configure'] = [
  261. 'title' => $this->t('Configure'),
  262. 'url' => Url::fromRoute($method['config_route_name']),
  263. ];
  264. // If there is at least one operation enabled show the operation
  265. // column.
  266. $table_form['#show_operations'] = TRUE;
  267. }
  268. $table_form['operation'][$method_id] = [
  269. '#type' => 'operations',
  270. '#links' => $config_op,
  271. ];
  272. }
  273. }
  274. $form[$type] = $table_form;
  275. }
  276. /**
  277. * Disables the language switcher blocks.
  278. *
  279. * @param array $language_types
  280. * An array containing all language types whose language switchers need to
  281. * be disabled.
  282. */
  283. protected function disableLanguageSwitcher(array $language_types) {
  284. $theme = $this->themeHandler->getDefault();
  285. $blocks = $this->blockStorage->loadByProperties(['theme' => $theme]);
  286. foreach ($language_types as $language_type) {
  287. foreach ($blocks as $block) {
  288. if ($block->getPluginId() == 'language_block:' . $language_type) {
  289. $block->delete();
  290. }
  291. }
  292. }
  293. }
  294. }