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?

3 Answers 3


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();
  • 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
    Commented Feb 14, 2017 at 10:47
  • yes you are right.
    Commented Feb 14, 2017 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()
      'title' => $title,
      'field_hash' => 'YOUR_HASH',

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
  • 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
    Commented Feb 14, 2017 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.
    – 4uk4
    Commented Feb 14, 2017 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
    Commented Nov 30, 2019 at 18:57
  • 3
    @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.
    – 4uk4
    Commented Nov 30, 2019 at 19:36
  • 2
    See this example for querying two different fields drupal.stackexchange.com/questions/250306/…
    – 4uk4
    Commented Nov 30, 2019 at 19:45

To load entities (nodes, users etc.) based on an array of multiple possible values/types you can use the entity query or entity type manager like that:

// Get all node IDs of multiple node types.
$query = \Drupal::entityQuery('node')
  ->condition('type', ['page', 'news'], 'IN');
$nids = $query->execute();
// Get all nodes of multiple node types.
$nodes = \Drupal::entityTypeManager()
    'type' => ['page', 'news'],

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.