I'm looking at building a simple key => value array with a certain type of node I have in order to generate a select list in a form.
The thing is that I have thousands of nodes of this type and the page takes forever to load with the current code I have.
Is there a way to load only certain fields using the QueryInterface in order to cut on loading times? Is there a more efficient way to do this?
$query = \Drupal::entityQuery('node') ->condition('type', 'product') ->condition('status', 1) ; $nids = $query->execute(); $nodeStorage = \Drupal::entityTypeManager()->getStorage('node'); // Load multiple nodes $nodes = $nodeStorage->loadMultiple($nids); $list = array(); foreach ($nodes as $node) { $list[$node->field_code->value] = $node->title->value . ' (' . $node->field_code->value . ')'; }