Get node based target id from entity reference We have an Article content type which has an Entity Reference field called field_document that is linked to the Media bundle type of Document. In mymodule we need to write a hook that will take the current field_document.id (which maps to Document.mid) and return all the Article pages where field_document.target_id = mid.
Goal

Take the current node id and retrieve the Document entity that is linked to by accessing field_document
Using the field_document value, return other Articles that have the same value for field_document
Perform the query in a hook_preprocess_node() hook.

Question

How can we retrieve all the nodes based on the $mid?

When we tried
We were able to return all Articles. However when we add -&gt;condition('field_document.target_id, $mid), we get zero results even though we know there are Articles where the field_document.target_id = $mid.
function mymodule_preprocess_node($variables) {
  $node = $variables['node'];
  $linked_document_id = $node-&gt;get('field_document')-&gt;first()-&gt;getValue()['target_id'];

  $node_storage = \Drupal::entityTypeManager()-&gt;getStorage('node');
  $query = $node_storage-&gt;getQuery()
    -&gt;condition('type', 'article')
    -&gt;condition('status', 1);

  $and = $query-&gt;andConditionGroup();
  $and-&gt;condition('field_document', $linked_document_id, '=');
  $query-&gt;condition($and);
  $results = $query-&gt;execute();

  // returns zero
  ksm($results);
}

We also tried using $and-&gt;condition('field_document.target_id', $linked_document_id, '=');, but that did not return any results either.
Additional steps we tried, but with similar issue.
 function mymodule_preprocess_node($variables) {
  $node = $variables['node'];
  $linked_document_id = $node-&gt;get('field_document')-&gt;first()-&gt;getValue()['target_id'];

  $node_storage = \Drupal::entityTypeManager()-&gt;getStorage('node');
  $query = $node_storage-&gt;getQuery()
    -&gt;condition('type', 'article')
    -&gt;condition('status', 1);
    -&gt;condition('field_document', $linked_document_id, '=');
  $results = $query-&gt;execute();

  // returns zero
  ksm($results);
}
We found the issue, it was both duplicate data issue and syntax errors mentioned by @berramou. The fix to the query was  changing -&gt;condition('status', 1); line to -&gt;condition('status', 1). 

We found that the test data included duplicate media entities so when the -&gt;condition('field_document', $linked_document_id, '='); was added, there were no results because the entities we were expecting were not in the table.

Sample test data:


media entity (id=1) name --> foo
media entity (id=2) name --> foo
media entity (id=3) name --> bar
Article->field_document->target_id = 1


I was expecting foo (id=1) to be returned but I was receiving foo (id=2). We have a specific requirement that media entities should be unique so after we merged the duplicate ones, the issue resolved. 

function mymodule_preprocess_node($variables) {
  $node = $variables['node'];
  $linked_document_id = $node-&gt;get('field_document')-&gt;first()-&gt;getValue()['target_id'];

  $node_storage = \Drupal::entityTypeManager()-&gt;getStorage('node');
  $query = $node_storage-&gt;getQuery()
    -&gt;condition('type', 'article')
    -&gt;condition('status', 1)
    -&gt;condition('field_document', $linked_document_id, '=');
  $results = $query-&gt;execute();

  // returns results now
  ksm($results);
}