annotate core/modules/config_translation/config_translation.module @ 19:fa3358dc1485 tip

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