0

We are using the entity_translation module in a large site, and we have 57 defined languages. We have some custom date fields in each locale, and we are using some custom code to only publish the locale when the current date is within the specified date range. However, one problem we are running into is that when an entity (node, taxonomy term) is created, the Published status for the default language (en-us in our case, defined at admin/config/regional/entity_translation) is automatically set to Published, which we don't want, so I need to override that value and set it to 0.

After delving into the module code, I found the hook_entity_translation_insert hook, which is called from EntityTranslationDefaultHandler:saveTranslations():

  foreach ($translations->hook as $langcode => $info) {
    $translation = isset($translations->data[$langcode]) ? $translations->data[$langcode] : $langcode;
    $data = isset($info['data']) ? $info['data'] : NULL;
    module_invoke_all('entity_translation_' . $info['hook'], $this->entityType, $this->entity, $translation, $data);
    // Provide Rules events integration if available.
    if (module_exists('rules')) {
      // Pass the entity as a wrapped one since rules can't do it for us
      // when using the variable type 'entity'.
      rules_invoke_event('entity_translation_' . $info['hook'], $this->entityType, entity_metadata_wrapper($this->entityType, $this->entity), $translation, $data);
    }
  }

where $op = 'insert'. I tried implementing my hook like so:

function mymodule_entity_translation_insert($entity_type, $entity, $translation, $values = array()) {
  // Set the publishing status for en-us to 0.
  if ($translation['language'] == 'en-us') {
    $translation['status'] = 0;
  }
}

but that doesn't change the status that gets passed on. If I try passing $translation in by reference:

function fluke_staging_entity_translation_insert($entity_type, $entity, &$translation, $values = array())

krumo gets into a huge recursive loop, and my computer maxes out the CPU.I can return a value like so:

function mymodule_entity_translation_insert($entity_type, $entity, $translation, $values = array()) {
  // Set the publishing status for en-us to 0.
  if ($translation['language'] == 'en-us') {
    $translation['status'] = 0;
    return ['translation' => $translation];
  }
}

but that's worthless, too, because the hook invocation doesn't do anything with it:

module_invoke_all('entity_translation_' . $info['hook'], $this->entityType, $this->entity, $translation, $data);

as compared to

$new_data = module_invoke_all('entity_translation_' . $info['hook'], $this->entityType, $this->entity, $translation, $data);

or something like that.

So my question is, how can I override the translation status in this hook, since it doesn't seem that it's a) passable by reference, or 2) it accepts any data from the hook_entity_translation_insert implementation?

0

I ended up posting this to the module issue queue and got this suggested code:

function MODULE_entity_translation_insert($entity_type, $entity, $translation, $values = array()) {
  // Set the publishing status for en-us to 0.
  if ($translation['language'] == 'en-us') {
    $translation['status'] = 0;
    $handler = entity_translation_get_handler($entity_type, $entity);
    $handler->setTranslation($translation);
    // Important: Avoid infinite loops by firing the insert hook again and again.
    $handler->getTranslations()->hook = array();
    $handler->saveTranslations();
  }
}

which was pretty close to what I had come up with on my own:

function mymodule_entity_insert($entity, $type) {
  // We don't want to do this for field collections. 
   if (in_array($type, array('node', 'taxonomy_term'))) {
    // Attempt to unpublish the en-us translation that is set to published by default.
    $handler = entity_translation_get_handler($type, $entity);
    $translations = $handler->getTranslations();
    $enus_translation = $translations->data['en-us'];
    // Set the status = 0.
    $enus_translation['status'] = 0;
    $handler->setTranslation($enus_translation);
    // Save the translation.
    $handler->saveTranslations();
  }
}

And since it's not part of the saveTranslations() hook firing logic, no infinite loops should occur.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.