Commit 01df63cd authored by Kenny D's avatar Kenny D Committed by Jasper Lammens
Browse files

Issue #3262013 by Zerdiox: Add optional langcode to findOneByTag for Tokens

parent 69257f70
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -14,21 +14,25 @@ interface SystemTagFinderInterface {
   *
   * @param string $systemTagId
   *   The ID of the System Tag.
   * @param string|null $langcode
   *   Use a specific language code.
   *
   * @return \Drupal\Core\Entity\EntityInterface[]
   *   An array of entities, tagged with the given ID.
   */
  public function findByTag($systemTagId);
  public function findByTag($systemTagId, $langcode = NULL);

  /**
   * Find a single entity by System Tag ID.
   *
   * @param string $systemTagId
   *   The ID of the System Tag.
   * @param string|null $langcode
   *   Use a specific language code.
   *
   * @return \Drupal\Core\Entity\EntityInterface|null
   *   An entity, tagged with the given ID or NULL if nothing is found.
   */
  public function findOneByTag($systemTagId);
  public function findOneByTag($systemTagId, $langcode = NULL);

}
+4 −4
Original line number Diff line number Diff line
@@ -86,12 +86,12 @@ abstract class SystemTagFinderPluginBase extends PluginBase implements SystemTag
  /**
   * {@inheritdoc}
   */
  public function findByTag($systemTagId) {
  public function findByTag($systemTagId, $langcode = NULL) {
    $entities = [];
    $entityTypeId = $this->pluginDefinition['entity_type'];

    if ($fields = $this->systemTagHelper->getReferenceFieldNames($entityTypeId)) {
      $langcode = $this->languageManager->getCurrentLanguage()->getId();
      $langcode = $langcode ?: $this->languageManager->getCurrentLanguage()->getId();
      $storage = $this->entityTypeManager->getStorage($entityTypeId);

      $query = $storage->getQuery()
@@ -128,8 +128,8 @@ abstract class SystemTagFinderPluginBase extends PluginBase implements SystemTag
  /**
   * {@inheritdoc}
   */
  public function findOneByTag($systemTagId) {
    $entities = $this->findByTag($systemTagId);
  public function findOneByTag($systemTagId, $langcode = NULL) {
    $entities = $this->findByTag($systemTagId, $langcode);

    return reset($entities) ?: NULL;
  }
+1 −1
Original line number Diff line number Diff line
@@ -64,7 +64,7 @@ function system_tags_tokens($type, array $tokens, array $data, array $options, B
      $system_tag_finder = Drupal::service('plugin.manager.system_tags.system_tag_finder_manager')
        ->getInstance(compact('entity_type'));

      if ($entity = $system_tag_finder->findOneByTag($tag_id)) {
      if ($entity = $system_tag_finder->findOneByTag($tag_id, $options['langcode'] ?? NULL)) {
        // Replace the original token with the translated alias.
        $replacements[$original] = $path_alias_manager->getAliasByPath(
          sprintf('/%s', $entity->toUrl()->getInternalPath()),