1
votes

This is my first foray into Drupal 8 and I am being stumped.

I am trying to get the filename of a file in a file field (and its location on disk if possible) after a node has been updated. Right now I am implementing hook_entity_update and am sometimes getting unexpected entity id with the following code:

function mymodule_entity_update(Drupal\Core\Entity\EntityInterface $entity)
{
    die($entity->id());
}

If I update the node and change other fields besides the file field, it prints the node's entity id.

If I update the node and delete/replace the file in the file field, it prints the file's entity id.

I expect it to always print the node's entity id, so I am obviously not understanding something here.

The end game is to copy the file in the file field to a specific location after the node has been updated, but I am having trouble reliably getting the values I expect. If anybody can help me understand what is happening I'd appreciate it.

1

1 Answers

2
votes

Figured out my issue.

When I update the image field, it updates the file entity first, then the node entity. So the first run through the hook implementation was the File entity.

To only act on a Node entity, I check if it's a Node entity first.

function mymodule_entity_update(Drupal\Core\Entity\EntityInterface $entity)
{
    if($entity->getEntityTypeId() == 'node'){
        die($entity->id()); // prints node id only
    }
}

For what I need to do, now that I know what's going on, it's more useful for me to check if it's a file entity being updated.