YamlDiscovery.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Drupal\Core\Plugin\Discovery;
  3. use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
  4. use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
  5. use Drupal\Core\Discovery\YamlDiscovery as CoreYamlDiscovery;
  6. use Drupal\Core\StringTranslation\TranslatableMarkup;
  7. /**
  8. * Allows YAML files to define plugin definitions.
  9. *
  10. * If the value of a key (like title) in the definition is translatable then
  11. * the addTranslatableProperty() method can be used to mark it as such and also
  12. * to add translation context. Then
  13. * \Drupal\Core\StringTranslation\TranslatableMarkup will be used to translate
  14. * the string and also to mark it safe. Only strings written in the YAML files
  15. * should be marked as safe, strings coming from dynamic plugin definitions
  16. * potentially containing user input should not.
  17. */
  18. class YamlDiscovery implements DiscoveryInterface {
  19. use DiscoveryTrait;
  20. /**
  21. * YAML file discovery and parsing handler.
  22. *
  23. * @var \Drupal\Core\Discovery\YamlDiscovery
  24. */
  25. protected $discovery;
  26. /**
  27. * Contains an array of translatable properties passed along to t().
  28. *
  29. * @see \Drupal\Core\Plugin\Discovery\YamlDiscovery::addTranslatableProperty()
  30. *
  31. * @var array
  32. */
  33. protected $translatableProperties = [];
  34. /**
  35. * Construct a YamlDiscovery object.
  36. *
  37. * @param string $name
  38. * The file name suffix to use for discovery; for example, 'test' will
  39. * become 'MODULE.test.yml'.
  40. * @param array $directories
  41. * An array of directories to scan.
  42. */
  43. public function __construct($name, array $directories) {
  44. $this->discovery = new CoreYamlDiscovery($name, $directories);
  45. }
  46. /**
  47. * Set one of the YAML values as being translatable.
  48. *
  49. * @param string $value_key
  50. * The key corresponding to the value in the YAML that contains a
  51. * translatable string.
  52. * @param string $context_key
  53. * (Optional) the translation context for the value specified by the
  54. * $value_key.
  55. *
  56. * @return $this
  57. */
  58. public function addTranslatableProperty($value_key, $context_key = '') {
  59. $this->translatableProperties[$value_key] = $context_key;
  60. return $this;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getDefinitions() {
  66. $plugins = $this->discovery->findAll();
  67. // Flatten definitions into what's expected from plugins.
  68. $definitions = [];
  69. foreach ($plugins as $provider => $list) {
  70. foreach ($list as $id => $definition) {
  71. // Add TranslatableMarkup.
  72. foreach ($this->translatableProperties as $property => $context_key) {
  73. if (isset($definition[$property])) {
  74. $options = [];
  75. // Move the t() context from the definition to the translation
  76. // wrapper.
  77. if ($context_key && isset($definition[$context_key])) {
  78. $options['context'] = $definition[$context_key];
  79. unset($definition[$context_key]);
  80. }
  81. $definition[$property] = new TranslatableMarkup($definition[$property], [], $options);
  82. }
  83. }
  84. // Add ID and provider.
  85. $definitions[$id] = $definition + [
  86. 'provider' => $provider,
  87. 'id' => $id,
  88. ];
  89. }
  90. }
  91. return $definitions;
  92. }
  93. }