5

I want something like SELECT nid FROM node WHERE type = "blablabla".

How do I do this in Drupal 8 PHP code?

3
  • 1
    There is a full topic on Drupal.org API. It is not possible to answer the question without repeating what already said in the documentation. If you had problems with code you are writing, you could ask a question about that, providing you show the code you are using.
    – apaderno
    Feb 27, 2017 at 19:45
  • Just to make it clear, the answer given here is not even complete, since it doesn't cover all the available methods.
    – apaderno
    Feb 27, 2017 at 19:45
  • 1
    To use pure SQL save the querry string in the $sql variable and do this: $connection = \Drupal::database(); $query = $connection->query($sql); $result = $query->fetchAll();
    – frazras
    Sep 16, 2019 at 15:12

1 Answer 1

17

You would use the database abstraction layer.

$query = \Drupal::database()->select('node', 'n');
$query->addField('n', 'nid');
$query->condition('n.type', 'blablabla');
$results = $query->execute();

As for executing the query, you can also fetch results in various ways.

Examples:

$query = \Drupal::database()->select('node', 'n');
$query->addField('n', 'nid');
$query->condition('n.type', 'blablabla');
$results = $query->execute()->fetchAll();


$query = \Drupal::database()->select('node', 'n');
$query->addField('n', 'nid');
$query->condition('n.type', 'blablabla');
$results = $query->execute()->fetchAllAssoc('nid');

If you are within a class, you should inject the appropriate classes in your constructor instead of use the static Drupal container. But since you are in a preprocess function, \Drupal::database() is fine.

From core/lib/Drupal.php:

 /**
   * Returns the current primary database.
   *
   * @return \Drupal\Core\Database\Connection
   *   The current active database's master connection.
   */
  public static function database() {
    return static::getContainer()->get('database');
  }

Depending on your use case(s), you can also leverage EntityQuery:

$query = \Drupal::entityQuery('node');
$query->condition('type', 'blablabla');
$results = $query->execute();

Please note that you may want to use node_field_data table instead of node, but your query is pretty generic so it is hard to tell without a better query.

Example, get all nodes of type that are published:

$query = \Drupal::database()->select('node_field_data', 'nfd');
$query->addField('nfd', 'nid');
$query->condition('nfd.type', 'blablabla');
$query->condition('nfd.status', 1);
$results = $query->execute();

Or:

$query = \Drupal::entityQuery('node');
$query->condition('type', 'blablabla');
$query->condition('status', 1);
$results = $query->execute();
3
  • Thank you so much! That's the kind of simple answer I was looking for :)
    – elterr1ble
    Feb 27, 2017 at 17:02
  • Updated with a couple of examples. Little hard to give better direction, but this should get you into the DB APIs. Also, if you add more details to your question, like what your objective may be in a preprocess function (show node counts?) it can be reopened.
    – Kevin
    Feb 27, 2017 at 17:13
  • $pager_data = $this->database->select('table_a', 'a'); $pager_data->fields('a', array('field1', 'field2')); $pager_data->join('ges_casse','b','b.id=a.bid'); $pager_data->fields('b', array('field3')); $pager_data->orderBy('nid', 'DESC'); $pager_data->extend(PagerSelectExtender::class)->limit(4); $pager_final_data = $pager_data->execute()->fetchAll(); dpm($pager_final_data);
    – Wasim Khan
    Feb 3, 2019 at 13:25

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