I want to load a certain node. I don't know the id of the node but I do know that it has a certain hash field that is unique and I know that hash.

I want to load the node via the hash field. How can I do that?


You can achieve by using EntityFieldQuery.

For D8 EntityFieldQuery has been rewritten.

Drupal 8:

$query = \Drupal::entityQuery('entity_test');
$default_langcode_group = $query->andConditionGroup()
  ->condition('user_id', $properties[$default_langcode]['user_id'], '=', $default_langcode)
  ->condition('name', $properties[$default_langcode]['name'], '=', $default_langcode);
$langcode_group = $query->andConditionGroup()
  ->condition('name', $properties[$langcode]['name'], '=', $langcode)
  ->condition("$this->field_name.value", $field_value, '=', $langcode);
$result = $query
  ->condition('langcode', $default_langcode)
  ->sort('name', 'ASC', $default_langcode)

How do I fetch the values of an entity's field?

$query = \Drupal::entityQuery('node')
    ->condition('status', 1)
    ->condition('changed', REQUEST_TIME, '<')
    ->condition('title', 'cat', 'CONTAINS')
    ->condition('field_tags.entity.name', 'cats');

$nids = $query->execute();
| improve this answer | |
  • So if I'm understanding this correctly, this will return a bunch of node ids(or one, or none, depending on the condition), but I still have to use Node:: to load said nodes, correct? – Andrew Feb 14 '17 at 10:47
  • yes you are right. – DRUPWAY Feb 14 '17 at 10:49

The quickest way to load a specific node by field value is to use the method loadByProperties().

You specify one or more field values and returned is an array containing the nodes matching the field values:

$nodes = \Drupal::entityTypeManager()
  ->loadByProperties(['title' => $title]);

Normally you loop over the nodes. In your case you are looking for one specific node. A single node is also returned in array, so apply reset() and this returns either the node or NULL if nothing was found:

if ($node = reset($nodes)) {
  // found $node that matches the title
| improve this answer | |
  • 1
    I assume in my case I wouldn't need the second part of the code ($node = reset...) since the hash itself is unique. – Andrew Feb 14 '17 at 12:24
  • 3
    The single node is still in an array. You could use $node = $nodes[0], but then you would have to check if there is a node to avoid warnings in the error log. reset() is better because it silently returns NULL. – 4k4 Feb 14 '17 at 12:32
  • Not sure why all the upvotes for this; the original question is to load a node by field value. A property (like Title) is not a field. – liquidcms Nov 30 '19 at 18:57
  • 2
    @liquidcms, title is a node field and you can use any other node field. BTW Drupal fields have one or more properties. If it is one property, Drupal uses most times the property name value. So for most fields both terms are interchangeable. – 4k4 Nov 30 '19 at 19:36
  • 1
    See this example for querying two different fields drupal.stackexchange.com/questions/250306/… – 4k4 Nov 30 '19 at 19:45

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.