use Drupal\node\Entity\Node;
$node = Node::load($nid);
$node->setTitle('MY NEW TITLE'); // This is a special meta field
$node->set('FIELD_NAME', 'THIS IS DATA'); // This is a Field added in to the content type
$node->save();

This code allows you to update the item (s) of a content type.
I need to update a boolean field of all nodes of a content type.
Is it possible to do this with getQuery or entityQuery to make it faster?
Or if there is another sql command to be able to do it

Comments

mitpatoliya’s picture

You ca fetch all the nodes of given type by using the following logic.

$nodeids = \Drupal::entityQuery('node')->condition('type','your_custom_type')->execute();
$nodes =  \Drupal\node\Entity\Node::loadMultiple($nodeids);

then you just have to iterate through all those nodes to do whatever changes you want.

Gae58’s picture

Thank you for the answer but in this way it has to go through the whole array and it takes time to update all the nodes. I was thinking of a single command to update the dataBase

dabley’s picture

You can use the Database API to perform an "Update Query" - see https://www.drupal.org/docs/drupal-apis/database-api/update-queries. This will be fast, but there are two things to beware of:

1. It requires a knowledge of, and is sensitive to the underlying database structure, which might change in future versions of Drupal.

2. The cached data for the relevant table will become out of date, so you'll need to refresh it.