1

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

  • 1
    Yes, there is another option, see drupal.stackexchange.com/questions/263414/… – 4k4 Nov 7 '18 at 10:38
  • 1
    @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
  • 1
    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
1

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

|improve this answer|||||
1

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

|improve this answer|||||
  • 1
    Can't update a translation while on the source language of the same node – i am batman Nov 7 '18 at 10:59
  • 1
    hook_ENTITY_TYPE_update happens after the DB submission – Clive Nov 7 '18 at 11:30
  • 1
    @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
1

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

|improve this answer|||||
0
/**
 * Respond to creation of a new entity.
 *
 * This hook runs once the entity has been stored. Note that hook
 * implementations may not alter the stored entity data.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity object.
 *
 * @ingroup entity_crud
 * @see hook_ENTITY_TYPE_insert()
 */
function hook_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {
  // Insert the new entity into a fictional table of all entities.
  \Drupal::database()->insert('example_entity')
    ->fields([
      'type' => $entity->getEntityTypeId(),
      'id' => $entity->id(),
      'created' => REQUEST_TIME,
      'updated' => REQUEST_TIME,
    ])
    ->execute();
}
|improve this answer|||||

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.