1. 8.2.x core/lib/Drupal/Core/Discovery/YamlDiscovery.php YamlDiscovery
  2. 8.2.x core/lib/Drupal/Component/Discovery/YamlDiscovery.php YamlDiscovery
  3. 8.2.x core/lib/Drupal/Core/Plugin/Discovery/YamlDiscovery.php YamlDiscovery
  4. 8.0.x core/lib/Drupal/Component/Discovery/YamlDiscovery.php YamlDiscovery
  5. 8.0.x core/lib/Drupal/Core/Plugin/Discovery/YamlDiscovery.php YamlDiscovery
  6. 8.1.x core/lib/Drupal/Core/Plugin/Discovery/YamlDiscovery.php YamlDiscovery
  7. 8.1.x core/lib/Drupal/Component/Discovery/YamlDiscovery.php YamlDiscovery
  8. 8.3.x core/lib/Drupal/Core/Plugin/Discovery/YamlDiscovery.php YamlDiscovery
  9. 8.3.x core/lib/Drupal/Component/Discovery/YamlDiscovery.php YamlDiscovery
  10. 8.3.x core/lib/Drupal/Core/Discovery/YamlDiscovery.php YamlDiscovery

Allows YAML files to define plugin definitions.

If the value of a key (like title) in the definition is translatable then the addTranslatableProperty() method can be used to mark it as such and also to add translation context. Then \Drupal\Core\StringTranslation\TranslatableMarkup will be used to translate the string and also to mark it safe. Only strings written in the YAML files should be marked as safe, strings coming from dynamic plugin definitions potentially containing user input should not.

Hierarchy

Expanded class hierarchy of YamlDiscovery

8 files declare their use of YamlDiscovery
BreakpointManager.php in core/modules/breakpoint/src/BreakpointManager.php
ConfigMapperManager.php in core/modules/config_translation/src/ConfigMapperManager.php
ContextualLinkManager.php in core/lib/Drupal/Core/Menu/ContextualLinkManager.php
LocalActionManager.php in core/lib/Drupal/Core/Menu/LocalActionManager.php
LocalTaskIntegrationTestBase.php in core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTestBase.php

... See full list

File

core/lib/Drupal/Core/Plugin/Discovery/YamlDiscovery.php, line 21

Namespace

Drupal\Core\Plugin\Discovery
View source
class YamlDiscovery implements DiscoveryInterface {

  use DiscoveryTrait;

  /**
   * YAML file discovery and parsing handler.
   *
   * @var \Drupal\Core\Discovery\YamlDiscovery
   */
  protected $discovery;

  /**
   * Contains an array of translatable properties passed along to t().
   *
   * @see \Drupal\Core\Plugin\Discovery\YamlDiscovery::addTranslatableProperty()
   *
   * @var array
   */
  protected $translatableProperties = [];

  /**
   * Construct a YamlDiscovery object.
   *
   * @param string $name
   *   The file name suffix to use for discovery; for example, 'test' will
   *   become 'MODULE.test.yml'.
   * @param array $directories
   *   An array of directories to scan.
   */
  function __construct($name, array $directories) {
    $this->discovery = new CoreYamlDiscovery($name, $directories);
  }

  /**
   * Set one of the YAML values as being translatable.
   *
   * @param string $value_key
   *   The key corresponding to the value in the YAML that contains a
   *   translatable string.
   * @param string $context_key
   *   (Optional) the translation context for the value specified by the
   *   $value_key.
   *
   * @return $this
   */
  public function addTranslatableProperty($value_key, $context_key = '') {
    $this->translatableProperties[$value_key] = $context_key;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefinitions() {
    $plugins = $this->discovery->findAll();

    // Flatten definitions into what's expected from plugins.
    $definitions = array();
    foreach ($plugins as $provider => $list) {
      foreach ($list as $id => $definition) {
        // Add TranslatableMarkup.
        foreach ($this->translatableProperties as $property => $context_key) {
          if (isset($definition[$property])) {
            $options = [];
            // Move the t() context from the definition to the translation
            // wrapper.
            if ($context_key && isset($definition[$context_key])) {
              $options['context'] = $definition[$context_key];
              unset($definition[$context_key]);
            }
            $definition[$property] = new TranslatableMarkup($definition[$property], [], $options);
          }
        }
        // Add ID and provider.
        $definitions[$id] = $definition + array(
          'provider' => $provider,
          'id' => $id,
        );
      }
    }

    return $definitions;
  }

}

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
DiscoveryTrait::doGetDefinition protected function Gets a specific plugin definition. Overrides DiscoveryTrait::doGetDefinition
DiscoveryTrait::getDefinition public function Gets a specific plugin definition. Overrides DiscoveryInterface::getDefinition
DiscoveryTrait::hasDefinition public function Indicates if a specific plugin definition exists. Overrides DiscoveryTrait::hasDefinition
YamlDiscovery::$discovery protected property YAML file discovery and parsing handler.
YamlDiscovery::$translatableProperties protected property Contains an array of translatable properties passed along to t().
YamlDiscovery::addTranslatableProperty public function Set one of the YAML values as being translatable.
YamlDiscovery::getDefinitions public function Gets the definition of all plugins for this type. Overrides DiscoveryTrait::getDefinitions
YamlDiscovery::__construct function Construct a YamlDiscovery object.