Nicolas Wadel
Nicolas Wadel

Reputation: 75

Drupal - hook translation - deletion

I'm currently working with Drupal 8 API, and I need to store on a DataBase my menu_link_content type. To do so I have a hook on entity_presave & entity_predelete. Since I don't want to loose sync between the database & Drupal, when my code got an error I stop Drupal from saving the entity. It is why I hooked presave and not save. anyway...

function modulename_entity_presave(\Drupal\Core\Entity\EntityInterface $entity) : void
{
    $class = get_class($entity);

    if ($entity->isNew()) {
        switch ($class) {
            case 'Drupal\menu_link_content\Entity\MenuLinkContent':
                //dispatch event menu link content insert
                //listener on that event that insert the entity in DB
                break;
        }
    }

    if (isset($entity->original)) {
        switch ($class) {
            case 'Drupal\menu_link_content\Entity\MenuLinkContent':
                //dispatch event menu link content update <- this one triggered
                //listener on that event that update the entity in DB
                break;
        }
    }
}

My problem is that when I delete a translation of my menu_link_content (or any type) the event "presave" is triggered and the entity langcode is changed from translated langcode (e.g. 'fr') to the default langcode (e.g. 'en') and then the entity_translation_delete hook is triggered.

function modulename_entity_translation_delete(\Drupal\Core\Entity\EntityInterface $translation)
{
    $class = get_class($translation);
    
    switch ($class) {
        case 'Drupal\menu_link_content\Entity\MenuLinkContent':
            // get the langcode && the uuid of the $translation entity
            // deleting the row from database using langcode && uuid as key
            break;
    }
}

So when I query my database with the translated langcode (e.g. 'fr') I get nothing... since the row has already been updated. So I can't remove it from my DB

I've think of a way to catch if the langcode of the entity is changed during the update to trigger the remove function (since I think that the lancode of an entity cannot be changed, the only time I saw that is when I deleted the translation). But I don't think it's the best way to do so.. Is there some kind of hook_entity_translation_predelete ?

Drupal hook list not showing anything

The code from modulename_entity_translation_delete works well if I do not update the row in DB (by commenting the update line)

Thanks for your help !

Upvotes: 0

Views: 567

Answers (1)

Nicolas Wadel
Nicolas Wadel

Reputation: 75

The only way I found to catch if I delete a translation of the entity (even if I catch an update before), is to check that :

  • the langcode of my updated data is the default language
  • I had a previous registered data (so the entity isn't new)
  • the previous registered langcode isn't in the available translations anymore

For me, the code looks like this :

if (\Drupal::languageManager()->getDefaultLanguage()->getId() === $data['langcode']->getValue()
            && $data['langcode']->getOriginalData() !== null
            && $data['langcode']->getAvailableTranslations() !== $data['langcode']->getOriginalData()) {
            // do what you want of the matching data in DB
        }

Here $data is an array that store (custom) field objects.

Upvotes: 0

Related Questions