5

How do I unpublish translations when the english content is unpublished? I tried to unpublish the language content programmatically when I am unpublishing the english content in the hook_node_update() but it returns below error.

$languages = getLanguageList();
$currLanguage = \Drupal::languageManager()->getCurrentLanguage()->getId();

if (!$node->status->value) {
    foreach ($languages as $languageId => $language) {
        if ($languageId != 'en' && $currLanguage == 'en') {
            if ($node->hasTranslation($languageId)) {
                $transNode = $node->getTranslation($languageId);
                $transNode->setPublished(false);
                $transNode->save();
            }
        }
    }        
}

Error :

Uncaught PHP Exception Drupal\\Core\\Entity\\EntityStorageException: "Update existing 'node' entity revision while changing the revision ID is not supported."

So I guess if I can do this after saving the node it would work. Is there a hook for node after save. Or is there a way to unpublish language contents of a node while unpublishing the source language(English)?

3
  • 2
    Yes, there is another option, see drupal.stackexchange.com/questions/263414/…
    – 4uk4
    Nov 7, 2018 at 10:38
  • 2
    @4k4 I need the unpublished/publish status for languages too. What I need to do is, I should be able to unpublish language content itself. And If I unpublish the source language content the translations should be unpublished too. If I unpublish a translation It shouldn't be affected to the source language Nov 7, 2018 at 10:46
  • 2
    Yes, the linked option only works if you want to synchronize both ways. In your code checking the current language doesn't make much sense. Probably you want to check the node language to prevent recursion while saving the translations: $node->language()->getId() == 'en'?
    – 4uk4
    Nov 7, 2018 at 12:17

3 Answers 3

4

In Drupal 8 you should use hook_entity_update().
This hook runs once the entity storage has been updated.

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_entity_update/8.3.x

2

Could it be this you are looking for:

Entity::postSave(EntityStorageInterface $storage, $update = TRUE)

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Entity.php/function/Entity%3A%3ApostSave/8.6.x

2

You could try in hook_pre_save so that should be before the main node has been effected

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_entity_presave/8.6.x

3
  • 2
    Can't update a translation while on the source language of the same node Nov 7, 2018 at 10:59
  • 2
    hook_ENTITY_TYPE_update happens after the DB submission
    – Clive
    Nov 7, 2018 at 11:30
  • 2
    @Clive I tried my code with hook_ENTITY_TYPE_update but it gave the same error as I mentioned Nov 8, 2018 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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