0

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

  1. 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 ->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->get('field_document')->first()->getValue()['target_id'];

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

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

  // returns zero
  ksm($results);
}

We also tried using $and->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->get('field_document')->first()->getValue()['target_id'];

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

  // returns zero
  ksm($results);
}
  • Although I don't think it would affect your result, is there a reason why you are creating an andConditionGroup() instead of just appending your condition onto the other conditions? Conditions by default are 'AND', and since your group only has as single condition, it doesn't appear to be grouping anything. – Jaypan May 2 at 23:27
  • @Jaypan We only used the andConditionGroup() to see if we got separate results, but that did not happen. – usernameabc May 2 at 23:37
  • Have you checked that $linked_document_id contains the value you think it should? – Jaypan May 2 at 23:38
  • @Jaypan field_document is required and will always have a value. I tried using a bunch of kint statements as well. for some reason the ->condition('field_document', $linked_document_id, '='); just returns zero results – usernameabc May 3 at 15:01
  • 1
    the second code works for me, but i think the is ; in it shouldn't be after ->condition('status', 1); try with ` $node_storage = \Drupal::entityTypeManager()->getStorage('node'); $query = $node_storage->getQuery() ->condition('type', 'article') ->condition('status', 1) ->condition('field_document', $linked_document_id, '='); $results = $query->execute();` – berramou May 5 at 14:16
0

We found the issue, it was both duplicate data issue and syntax errors mentioned by @berramou. The fix to the query was changing ->condition('status', 1); line to ->condition('status', 1).

We found that the test data included duplicate media entities so when the ->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->get('field_document')->first()->getValue()['target_id'];

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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