Backup of db before drupal security update
[yaffs-website] / web / core / modules / config_translation / config_translation.module
1 <?php
2
3 /**
4  * @file
5  * Configuration Translation module.
6  */
7
8 use Drupal\Core\Config\Entity\ConfigEntityInterface;
9 use Drupal\Core\Entity\EntityInterface;
10 use Drupal\Core\Routing\RouteMatchInterface;
11 use Drupal\field\FieldConfigInterface;
12
13 /**
14  * Implements hook_help().
15  */
16 function config_translation_help($route_name, RouteMatchInterface $route_match) {
17   switch ($route_name) {
18     case 'help.page.config_translation':
19       $output = '';
20       $output .= '<h3>' . t('About') . '</h3>';
21       $output .= '<p>' . t('The Configuration Translation module allows you to translate configuration text; for example, the site name, vocabularies, menus, or date formats. Together with the modules <a href=":language">Language</a>, <a href=":content-translation">Content Translation</a>, and <a href=":locale">Interface Translation</a>, it allows you to build multilingual websites. For more information, see the <a href=":doc_url">online documentation for the Configuration Translation module</a>.', [':doc_url' => 'https://www.drupal.org/documentation/modules/config_translation', ':config' => \Drupal::url('help.page', ['name' => 'config']), ':language' => \Drupal::url('help.page', ['name' => 'language']), ':locale' => \Drupal::url('help.page', ['name' => 'locale']), ':content-translation' => (\Drupal::moduleHandler()->moduleExists('content_translation')) ? \Drupal::url('help.page', ['name' => 'content_translation']) : '#']) . '</p>';
22       $output .= '<h3>' . t('Uses') . '</h3>';
23       $output .= '<dl>';
24       $output .= '<dt>' . t('Enabling translation') . '</dt>';
25       $output .= '<dd>' . t('In order to translate configuration, the website must have at least two <a href=":url">languages</a>.', [':url' => \Drupal::url('entity.configurable_language.collection')]) . '</dd>';
26       $output .= '<dt>' . t('Translating configuration text') . '</dt>';
27       $output .= '<dd>' . t('Users with the <em>Translate user edited configuration</em> permission can access the configuration translation overview, and manage translations for specific languages. The <a href=":translation-page">Configuration translation</a> page shows a list of all configuration text that can be translated, either as individual items or as lists. After you click on <em>Translate</em>, you are provided with a list of all languages. You can <em>add</em> or <em>edit</em> a translation for a specific language. Users with specific configuration permissions can also <em>edit</em> the text for the site\'s default language. For some configuration text items (for example for the site information), the specific translation pages can also be accessed directly from their configuration pages.', [':translation-page' => \Drupal::url('config_translation.mapper_list')]) . '</dd>';
28       $output .= '<dt>' . t('Translating date formats') . '</dt>';
29       $output .= '<dd>' . t('You can choose to translate date formats on the <a href=":translation-page">Configuration translation</a> page. This allows you not only to translate the label text, but also to set a language-specific <em>PHP date format</em>.', [':translation-page' => \Drupal::url('config_translation.mapper_list')]) . '</dd>';
30       $output .= '</dl>';
31       return $output;
32
33     case 'config_translation.mapper_list':
34       $output = '<p>' . t('This page lists all configuration items on your site that have translatable text, like your site name, role names, etc.') . '</p>';
35       return $output;
36   }
37 }
38
39 /**
40  * Implements hook_theme().
41  */
42 function config_translation_theme() {
43   return [
44     'config_translation_manage_form_element' => [
45       'render element' => 'element',
46       'template' => 'config_translation_manage_form_element',
47     ],
48   ];
49 }
50
51 /**
52  * Implements hook_themes_installed().
53  */
54 function config_translation_themes_installed() {
55   // Themes can provide *.config_translation.yml declarations.
56   // @todo Make ThemeHandler trigger an event instead and make
57   //   ConfigMapperManager plugin manager subscribe to it.
58   // @see https://www.drupal.org/node/2206347
59   \Drupal::service('plugin.manager.config_translation.mapper')->clearCachedDefinitions();
60 }
61
62 /**
63  * Implements hook_themes_uninstalled().
64  */
65 function config_translation_themes_uninstalled() {
66   // Themes can provide *.config_translation.yml declarations.
67   // @todo Make ThemeHandler trigger an event instead and make
68   //   ConfigMapperManager plugin manager subscribe to it.
69   // @see https://www.drupal.org/node/2206347
70   \Drupal::service('plugin.manager.config_translation.mapper')->clearCachedDefinitions();
71 }
72
73 /**
74  * Implements hook_entity_type_alter().
75  */
76 function config_translation_entity_type_alter(array &$entity_types) {
77   /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
78   foreach ($entity_types as $entity_type_id => $entity_type) {
79     if ($entity_type->entityClassImplements(ConfigEntityInterface::class)) {
80       if ($entity_type_id == 'block') {
81         $class = 'Drupal\config_translation\Controller\ConfigTranslationBlockListBuilder';
82       }
83       elseif ($entity_type_id == 'field_config') {
84         $class = 'Drupal\config_translation\Controller\ConfigTranslationFieldListBuilder';
85         // Will be filled in dynamically, see \Drupal\field\Entity\FieldConfig::linkTemplates().
86         $entity_type->setLinkTemplate('config-translation-overview', $entity_type->getLinkTemplate('edit-form') . '/translate');
87       }
88       else {
89         $class = 'Drupal\config_translation\Controller\ConfigTranslationEntityListBuilder';
90       }
91       $entity_type->setHandlerClass('config_translation_list', $class);
92
93       if ($entity_type->hasLinkTemplate('edit-form')) {
94         $entity_type->setLinkTemplate('config-translation-overview', $entity_type->getLinkTemplate('edit-form') . '/translate');
95       }
96     }
97   }
98 }
99
100 /**
101  * Implements hook_config_translation_info().
102  */
103 function config_translation_config_translation_info(&$info) {
104   $entity_manager = \Drupal::entityManager();
105
106   // If field UI is not enabled, the base routes of the type
107   // "entity.field_config.{$entity_type}_field_edit_form" are not defined.
108   if (\Drupal::moduleHandler()->moduleExists('field_ui')) {
109     // Add fields entity mappers to all fieldable entity types defined.
110     foreach ($entity_manager->getDefinitions() as $entity_type_id => $entity_type) {
111       // Make sure entity type has field UI enabled and has a base route.
112       if ($entity_type->get('field_ui_base_route')) {
113         $info[$entity_type_id . '_fields'] = [
114           'base_route_name' => "entity.field_config.{$entity_type_id}_field_edit_form",
115           'entity_type' => 'field_config',
116           'class' => '\Drupal\config_translation\ConfigFieldMapper',
117           'base_entity_type' => $entity_type_id,
118           'weight' => 10,
119         ];
120       }
121     }
122   }
123
124   // Discover configuration entities automatically.
125   foreach ($entity_manager->getDefinitions() as $entity_type_id => $entity_type) {
126     // Determine base path for entities automatically if provided via the
127     // configuration entity.
128     if (
129       !$entity_type->entityClassImplements(ConfigEntityInterface::class) ||
130       !$entity_type->hasLinkTemplate('edit-form')
131     ) {
132       // Do not record this entity mapper if the entity type does not
133       // provide a base route. We'll surely not be able to do anything with
134       // it anyway. Configuration entities with a dynamic base path, such as
135       // fields, need special treatment. See above.
136       continue;
137     }
138
139     // Use the entity type as the plugin ID.
140     $base_route_name = "entity.$entity_type_id.edit_form";
141     $info[$entity_type_id] = [
142       'class' => '\Drupal\config_translation\ConfigEntityMapper',
143       'base_route_name' => $base_route_name,
144       'title' => $entity_type->getLowercaseLabel(),
145       'names' => [],
146       'entity_type' => $entity_type_id,
147       'weight' => 10,
148     ];
149   }
150 }
151
152 /**
153  * Implements hook_entity_operation().
154  */
155 function config_translation_entity_operation(EntityInterface $entity) {
156   $operations = [];
157   $entity_type = $entity->getEntityType();
158   if ($entity_type->entityClassImplements(ConfigEntityInterface::class) &&
159     $entity->hasLinkTemplate('config-translation-overview') &&
160     \Drupal::currentUser()->hasPermission('translate configuration')) {
161
162     $link_template = 'config-translation-overview';
163     if ($entity instanceof FieldConfigInterface) {
164       $link_template = "config-translation-overview.{$entity->getTargetEntityTypeId()}";
165     }
166
167     $operations['translate'] = [
168       'title' => t('Translate'),
169       'weight' => 50,
170       'url' => $entity->urlInfo($link_template),
171     ];
172   }
173
174   return $operations;
175 }
176
177 /**
178  * Implements hook_config_schema_info_alter().
179  */
180 function config_translation_config_schema_info_alter(&$definitions) {
181   $map = [
182     'label' => '\Drupal\config_translation\FormElement\Textfield',
183     'text' => '\Drupal\config_translation\FormElement\Textarea',
184     'date_format' => '\Drupal\config_translation\FormElement\DateFormat',
185     'text_format' => '\Drupal\config_translation\FormElement\TextFormat',
186     'mapping' => '\Drupal\config_translation\FormElement\ListElement',
187     'sequence' => '\Drupal\config_translation\FormElement\ListElement',
188     'plural_label' => '\Drupal\config_translation\FormElement\PluralVariants',
189   ];
190
191   // Enhance the text and date type definitions with classes to generate proper
192   // form elements in ConfigTranslationFormBase. Other translatable types will
193   // appear as a one line textfield.
194   foreach ($definitions as $type => &$definition) {
195     if (isset($map[$type]) && !isset($definition['form_element_class'])) {
196       $definition['form_element_class'] = $map[$type];
197     }
198   }
199 }