Basically we need it for this issue in tmgmt_memory #2729873: Create a hook to translate exact matches on submission so this module can translate it before sobmiting it to the translator or even skip job_items or data_item, but could be used for any other module, to add fields or remove them if needed or modify the data items for any other reason.

To do that we will add a hook before the request translation (If need to modify data) and another after (If need to revert changes or add more changes).

Also we also will add a flag in Job that will be set to TRUE while we request a translation, also in getItems if we are requesting a translation and a jobItem does not have pending data_items we will not return this item.

Comments

edurenye created an issue. See original summary.

edurenye’s picture

StatusFileSize
new5.8 KB

Done.

edurenye’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: add_hooks_to_alter_the-2731939-2.patch, failed testing.

edurenye’s picture

Status: Needs work » Needs review
StatusFileSize
new5.66 KB
new937 bytes

Fixed, we need to open a issue to make request translation return the state as says the documentation.

edurenye’s picture

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/src/Entity/Job.php
    @@ -364,9 +374,18 @@ class Job extends ContentEntityBase implements EntityOwnerInterface, JobInterfac
    +      $all_items = \Drupal::entityTypeManager()->getStorage('tmgmt_job_item')->loadMultiple($results);
    +      $items = $all_items;
    +      if ($this->requestingTranslation) {
    +        /** @var \Drupal\tmgmt\JobItemInterface $item */
    +        foreach ($all_items as $key => $item) {
    +          if ($item->getCountPending() == 0) {
    +            unset($items[$key]);
    

    No need for $all_items? You could also use array_filter to simplify this:

    $items = array_filter($items, function (JobInterface $item) { $item->getCountPending() > 0; }));

  2. +++ b/src/Entity/Job.php
    @@ -689,10 +708,19 @@ class Job extends ContentEntityBase implements EntityOwnerInterface, JobInterfac
    +
    +    \Drupal::moduleHandler()->invokeAll('tmgmt_job_before_request_translation', [$this->getItems()]);
    +
    +    $this->requestingTranslation = TRUE;
    ...
    +
    +    $this->requestingTranslation = FALSE;
    +
    +    \Drupal::moduleHandler()->invokeAll('tmgmt_job_after_request_translation', [$this->getItems()]);
    

    Needs comments to explain those lines.

  3. +++ b/tmgmt.module
    @@ -102,9 +102,19 @@ function tmgmt_cron() {
    -    foreach ($job_items_by_job as $items) {
    +    foreach ($job_items_by_job as $all_items) {
    +      $items = $all_items;
           $translator = reset($items)->getTranslatorPlugin();
    -      $translator->requestJobItemsTranslation($items);
    +      \Drupal::moduleHandler()->invokeAll('tmgmt_job_before_request_translation', [$all_items]);
    +      foreach ($all_items as $key => $item) {
    +        if ($item->getCountPending() == 0) {
    +          unset($items[$key]);
    +        }
    +      }
    

    Same here

Not a big fan of the requestingTranslation flag but I don't really have any better ideas. I think we need to find a better name however, instead of naming it after the context, lets name it after what we use it for. $this->filterTranslatedItems maybe? Should require less comments to explain what it is for.

We should also add @see documentation to the two hooks we're calling on requestTranslation and possibly also mention this concept on the translator requestTranslation() method docs, something along the lines of "During the translation process, Job::getItems() will only return job items that are not already fully translated.".

edurenye’s picture

Status: Needs work » Needs review
StatusFileSize
new6.8 KB
new4.54 KB

Done all those changes.

Status: Needs review » Needs work

The last submitted patch, 8: add_hooks_to_alter_the-2731939-8.patch, failed testing.

edurenye’s picture

Status: Needs work » Needs review

  • Berdir committed 35acbe1 on 8.x-1.x authored by edurenye
    Issue #2731939 by edurenye: Add hooks to alter the behavior during the...
berdir’s picture

Status: Needs review » Fixed
diff --git a/src/Entity/Job.php b/src/Entity/Job.php
index 57e444c..b46a8c5 100644
--- a/src/Entity/Job.php
+++ b/src/Entity/Job.php
@@ -700,7 +700,6 @@ class Job extends ContentEntityBase implements EntityOwnerInterface, JobInterfac
     }
 
     // Call the hook before requesting the translation.
-    // @see hook_tmgmt_job_before_request_translation()
     \Drupal::moduleHandler()->invokeAll('tmgmt_job_before_request_translation', [$this->getItems()]);
 
     // We do not want to translate the items that are already translated.
@@ -712,7 +711,6 @@ class Job extends ContentEntityBase implements EntityOwnerInterface, JobInterfac
     $this->getTranslatorPlugin()->requestTranslation($this);
 
     // Reset it again so getData returns again all the values.
-    // @see hook_tmgmt_job_after_request_translation()
     $this->filterTranslatedItems = FALSE;
 
     // Call the hook after requesting the translation.
diff --git a/src/TranslatorPluginInterface.php b/src/TranslatorPluginInterface.php
index 300e569..053d80a 100644
--- a/src/TranslatorPluginInterface.php
+++ b/src/TranslatorPluginInterface.php
@@ -118,6 +118,9 @@ interface TranslatorPluginInterface extends PluginInspectionInterface {
    * @param \Drupal\tmgmt\JobInterface $job
    *   The job that should be submitted.
    *
+   * @see hook_tmgmt_job_before_request_translation()
+   * @see hook_tmgmt_job_after_request_translation()
+   *
    * @ingroup tmgmt_remote_languages_mapping
    */
   public function requestTranslation(JobInterface $job);

That's what I meant.

Edit: Forgot to mention, .. committed :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.