2

Here is my use case:

I have created a media bundle (FILM IMG) to store a media entity (image) field. I can manage it at admin/structure/media/manage/film_img/fields

In a content type named "FILM" , I have added a entity reference field which reference my "FILM IMG" media entity bundle.

I am also using file_field_path module to rename the uploaded images. When I am adding a new image for a new Film, I would like to use hook_filefield_paths_process_file to rename my image file name.

This hook is providing me the media entity ($entity) as a parameter, but of course it is not providing the Film entity that I need (I need it to get some info to rename my file name)

Here is my question: How to get the "parent" entity from the referenced entity;

In other words, with my use case: How to get my film node from the image media entity provided by the hook.

1 Answer 1

3

You can get that with an entity query:

// Get IDs of all film nodes that reference this entity
$nids = Drupal::entityQuery('node')
  ->condition('type', 'film')
  ->condition('field_foo.target_id', $entity->id())
  ->execute();

// Load the nodes
$nodes = Drupal::entityManager()
  ->getStorage('node')
  ->loadMultiple($nids);
1
  • Many thanks for the help, unfortunately, this is not working. Even if I add the missing 'node' parameter : Drupal::entityQuery('node')..., the result will be empty because I think that the entity is not yet saved when the hook is called. If I run this query after the save operation, I got the expected result.
    – Baud
    Jul 15, 2016 at 21:17

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.