There is not a single usage in core of the argument capability in either Yaml backed definitions, Annotation backed definitions or direct TranslationWrapper instantiation. In fact if the argument was actually dynamic wouldn't we have take of bubbling up some cacheability metadata. Reading the docs in TranslationWrapper:

 * This class can be used to delay translating strings until the translation
 * system is ready. This is useful for using translation in very low level
 * subsystems like entity definition and stream wrappers.

I think we should completely remove support for arguments from that because support arguments at extremely low level is extremely risky. Obviously removing that support is followup material but not adding that support to YamlDiscovery is the concern of this issue.

I've classified this as a bug due to the lack of bubbling up some cacheability metadata.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alexpott’s picture

Status: Active » Needs review
FileSize
6.22 KB

Let's see what and if anything breaks.

pwolanin’s picture

Status: Needs review » Needs work

This looks wrong - we have to have a way to provide arguments when the translation happens

   public function render() {
-    return $this->t($this->string, $this->arguments, $this->options);
+    return $this->t($this->string, [], $this->options);
   }
dawehner’s picture

I always think its a bad argument to argument just with "It is not used in core". From an abstract point of view TranslationWrapper delays t(), this is all, so it should support what t() gives you.

In case we remove arguments support we should explicit explain why we don't want to support arguments.

pwolanin’s picture

Priority: Normal » Critical
Status: Needs work » Needs review
FileSize
10.96 KB
6.59 KB

Here's a fix via pulling in some methods and tests from #2338081: Local Tasks, Actions, and Contextual links mark strings from derivatives (or alter hooks) as safe and translated

Marking critical since it's blocking the other critical issue - it doesn't make sense to proceed there without this done first (imho)

dawehner’s picture

  1. +++ b/core/lib/Drupal/Core/Entity/Annotation/ContentEntityType.php
    index 3a36db5..8f6236b 100644
    --- a/core/lib/Drupal/Core/StringTranslation/TranslationWrapper.php
    
    --- a/core/lib/Drupal/Core/StringTranslation/TranslationWrapper.php
    +++ b/core/lib/Drupal/Core/StringTranslation/TranslationWrapper.php
    

    As written before we should document why we not support arguments

  2. +++ b/core/lib/Drupal/Core/StringTranslation/TranslationWrapper.php
    @@ -85,6 +92,16 @@ public function getOption($name) {
    +   * @return array
    

    mixed[] would be fair I guess?

  3. +++ b/core/lib/Drupal/Core/StringTranslation/TranslationWrapper.php
    @@ -92,6 +109,23 @@ public function __toString() {
     
       /**
    +   * Creates a new object with updated arguments.
    +   *
    +   * @param string[] $arguments
    +   *   An array with placeholder replacements, keyed by placeholder. The values
    +   *   will be merged with and replace any existing arguments with the same
    +   *   keys.
    +   *
    +   * @return \Drupal\Core\StringTranslation\TranslationWrapper
    +   *   The new object.
    +   */
    +  public function withArguments(array $arguments) {
    +    $with = new static($this->string, $this->options);
    +    $with->arguments = $arguments + $this->arguments;
    +    return $with;
    +  }
    

    this should be not longer needed

  4. +++ b/core/tests/Drupal/Tests/Core/StringTranslation/TranslationWrapperTest.php
    @@ -0,0 +1,91 @@
    +  /**
    +   * @covers ::withArguments
    +   */
    +  public function testWithArguments() {
    

    can be dropped as well

pwolanin’s picture

ok, let's kill it.

alexpott’s picture

Priority: Critical » Major

I discussed this with @xjm, @catch, @effulgentsia and @webchick and we felt that this was not critical as it is okay that yaml plugin discovery loses argument support in #2338081: Local Tasks, Actions, and Contextual links mark strings from derivatives (or alter hooks) as safe and translated but annotation based discovery still has it. And this issue does not block the critical. We agreed that the cacheability concerns probably mean that this is a major.

alexpott’s picture

No harm in adding a test :)

pwolanin’s picture

Status: Needs review » Reviewed & tested by the community

As long as the discussion already inckuded @effulgentsia, I think this is ready.

dawehner’s picture

Status: Reviewed & tested by the community » Needs work
diff --git a/core/lib/Drupal/Core/Annotation/Translation.php b/core/lib/Drupal/Core/Annotation/Translation.php
index e1cc845..372000b 100644

index e1cc845..372000b 100644
--- a/core/lib/Drupal/Core/Annotation/Translation.php

--- a/core/lib/Drupal/Core/Annotation/Translation.php
+++ b/core/lib/Drupal/Core/Annotation/Translation.php

+++ b/core/lib/Drupal/Core/Annotation/Translation.php
@@ -73,20 +73,17 @@ class Translation extends AnnotationBase {

@@ -73,20 +73,17 @@ class Translation extends AnnotationBase {
    * @param array $values
 *
 * To provide replacement values for placeholders, use the "arguments" array:
 * @code
 *   title = @ Translation("Bundle !title", arguments = {"!title" = "Foo"}),
 * @endcode

Does someone mind removing those lines as well?

pwolanin’s picture

Status: Needs work » Needs review
FileSize
9.82 KB
989 bytes

like so?

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Thank you

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

TranslationWrapper's are how we avoid caching all plugin definitions by language - @dawehner has pointed out that we break this in ViewsEntityRow:

  public function getDerivativeDefinitions($base_plugin_definition) {
    foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
      // Just add support for entity types which have a views integration.
      if (($base_table = $entity_type->getBaseTable()) && $this->viewsData->get($base_table) && $this->entityManager->hasHandler($entity_type_id, 'view_builder')) {
        $this->derivatives[$entity_type_id] = array(
          'id' => 'entity:' . $entity_type_id,
          'provider' => 'views',
          'title' => $entity_type->getLabel(),
          'help' => t('Display the @label', array('@label' => $entity_type->getLabel())),
          'base' => array($entity_type->getDataTable() ?: $entity_type->getBaseTable()),
          'entity_type' => $entity_type_id,
          'display_types' => array('normal'),
          'class' => $base_plugin_definition['class'],
        );
      }
    }

    return $this->derivatives;
  }

I think we should dial this issue back to just removing argument support from annotation defined plugins and address TranslationWrapper and ViewsEntityRow in yet another issue.

dawehner’s picture

I try to do just that change.

dawehner’s picture

Status: Needs work » Needs review
FileSize
3.06 KB

There we go.

alexpott’s picture

FileSize
7.04 KB

My version of #15 was identical. Attaching an interdiff to #11.

dawehner’s picture

Title: Remove argument support from TranslationWrapper » Remove argument support from Translation Annotation
Related issues: +#2539106: Ensure that the labels in ViewsEntityRow appear in the right language

change title and add the views followop.

Gábor Hojtsy’s picture

Re the original goal of this issue, if the arguments may be dynamic and need their cache tags, so is the target language of the translations (via $options), so you would need to remove support of that too if you want predictable output based on global cache tags without the need for cache tag bubbling. At that point, the argument would be an empty or a single item array, so no need for it to be an array, so both the list of arguments and the number of them and type of them would be different between TranslationWrapper and t(), which I am not sure qualifies TranslationWrapper to be named as it is? At least someone in contrib would need to come up with an ActualTranslationWrapper that allows those options (and somehow bubbles cache info) then. Also the plan to change the signature would have required potx changes.

The newly proposed patch (totally different direction I see) may also need a sister issue in potx to adjust the parsing of the annotation.

Gábor Hojtsy’s picture

Adding a whole bunch of tags. Given this is not required for release, and we are post API-freeze (by over two years :D), it would be good to summarize the impact / disruption like any other major API change. Also will need a change record once its settled.

pwolanin’s picture

Since this is just about annotation processing, is it really an API change?

Gábor Hojtsy’s picture

Issue tags: -sprint

Looks like there is a lack of interest to prioritize this, so removing from sprint accordingly.

dawehner’s picture

Status: Needs review » Closed (works as designed)