0

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)?

  • Yes, there is another option, see drupal.stackexchange.com/questions/263414/… – 4k4 Nov 7 '18 at 10:38
  • @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 – i am batman Nov 7 '18 at 10:46
  • 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'? – 4k4 Nov 7 '18 at 12:17
0

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

0

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

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

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.