3

I have a data structure where content type research has an entity reference field scientists.

The content type scientists does not have any research reference entity field though.

I can get all scientists that a particular research has through research.

But is there a way to get all researches one particular scientist has? When content types were created I wasn't involved, so I'm trying to see my options whether I can somehow click a checkbox to make this applicable.

When I tried to set up a field with type of entity reference and pointed it to research for scientists, they were empty.

Any help is highly appreciated.

3
  • Have you had a look at Reverse Entity Reference, yet?
    – leymannx
    Commented Nov 11, 2020 at 14:34
  • 2
    Does this answer your question? View for Entity Reference reverse, backwards forwards
    – leymannx
    Commented Nov 11, 2020 at 14:35
  • @leymannx thanks again for your help. I'm using Drupal 9 actually, but the second link might be helpful. I don't need this in a view but in a preprocess function, so I'm wondering whether this will create the referenced entities when I go along.
    – supersize
    Commented Nov 11, 2020 at 14:47

1 Answer 1

6

Is there a way to get all researches one particular scientist has?

Yes, search for the scientist target id:

$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties([
  'type' => 'research',
  'field_scientists' => $scientist_id,
]);
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.