config_translation.api.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Configuration Translation module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Introduce dynamic translation tabs for translation of configuration.
  12. *
  13. * This hook augments MODULE.config_translation.yml as well as
  14. * THEME.config_translation.yml files to collect dynamic translation mapper
  15. * information. If your information is static, just provide such a YAML file
  16. * with your module containing the mapping.
  17. *
  18. * Note that while themes can provide THEME.config_translation.yml files this
  19. * hook is not invoked for themes.
  20. *
  21. * @param array $info
  22. * An associative array of configuration mapper information. Use an entity
  23. * name for the key (for entity mapping) or a unique string for configuration
  24. * name list mapping. The values of the associative array are arrays
  25. * themselves in the same structure as the *.config_translation.yml files.
  26. *
  27. * @see hook_config_translation_info_alter()
  28. * @see \Drupal\config_translation\ConfigMapperManagerInterface
  29. * @see \Drupal\config_translation\Routing\RouteSubscriber::routes()
  30. */
  31. function hook_config_translation_info(&$info) {
  32. $entity_manager = \Drupal::entityManager();
  33. $route_provider = \Drupal::service('router.route_provider');
  34. // If field UI is not enabled, the base routes of the type
  35. // "entity.field_config.{$entity_type}_field_edit_form" are not defined.
  36. if (\Drupal::moduleHandler()->moduleExists('field_ui')) {
  37. // Add fields entity mappers to all fieldable entity types defined.
  38. foreach ($entity_manager->getDefinitions() as $entity_type_id => $entity_type) {
  39. $base_route = NULL;
  40. try {
  41. $base_route = $route_provider->getRouteByName('entity.field_config.' . $entity_type_id . '_field_edit_form');
  42. }
  43. catch (RouteNotFoundException $e) {
  44. // Ignore non-existent routes.
  45. }
  46. // Make sure entity type has field UI enabled and has a base route.
  47. if ($entity_type->get('field_ui_base_route') && !empty($base_route)) {
  48. $info[$entity_type_id . '_fields'] = [
  49. 'base_route_name' => 'entity.field_config.' . $entity_type_id . '_field_edit_form',
  50. 'entity_type' => 'field_config',
  51. 'title' => t('Title'),
  52. 'class' => '\Drupal\config_translation\ConfigFieldMapper',
  53. 'base_entity_type' => $entity_type_id,
  54. 'weight' => 10,
  55. ];
  56. }
  57. }
  58. }
  59. }
  60. /**
  61. * Alter existing translation tabs for translation of configuration.
  62. *
  63. * This hook is useful to extend existing configuration mappers with new
  64. * configuration names, for example when altering existing forms with new
  65. * settings stored elsewhere. This allows the translation experience to also
  66. * reflect the compound form element in one screen.
  67. *
  68. * @param array $info
  69. * An associative array of discovered configuration mappers. Use an entity
  70. * name for the key (for entity mapping) or a unique string for configuration
  71. * name list mapping. The values of the associative array are arrays
  72. * themselves in the same structure as the *.config_translation.yml files.
  73. *
  74. * @see hook_translation_info()
  75. * @see \Drupal\config_translation\ConfigMapperManagerInterface
  76. */
  77. function hook_config_translation_info_alter(&$info) {
  78. // Add additional site settings to the site information screen, so it shows
  79. // up on the translation screen. (Form alter in the elements whose values are
  80. // stored in this config file using regular form altering on the original
  81. // configuration form.)
  82. $info['system.site_information_settings']['names'][] = 'example.site.setting';
  83. }
  84. /**
  85. * @} End of "addtogroup hooks".
  86. */