Drupal - How to load a node by a field value?

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()
  ->getStorage('node')
  ->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
}

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)
  ->condition($default_langcode_group)
  ->condition($langcode_group)
  ->sort('name', 'ASC', $default_langcode)
  ->execute();

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();

Tags:

Entities

Nodes

8