1

I have a custom block and I want to output a list of nodes of a certain type.

This is what I have so far:

public function build() {

$nids = \Drupal::entityQuery('node')
  ->condition('type', 'my_content_type')
  ->execute();

$nodes = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->loadMultiple($nids);

    return array(
      '#markup' => $this->t('How to display list here'),
    );
  }
}

How do I list the nodes by title and ordered by newest at the bottom of list?

3

Unless you have a specific issue not mentioned in the question, I would recommend using a view to create this block. This would mean that the criteria remained easily editable.

If you had specific need to output the list of nodes within a custom block, or would like to pass arguments to the view from custom code you could use views_embed_view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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