You are here

function node_title_list in Drupal 8

Same name and namespace in other branches
  1. 4 modules/node.module \node_title_list()
  2. 5 modules/node/node.module \node_title_list()
  3. 6 modules/node/node.module \node_title_list()
  4. 7 modules/node/node.module \node_title_list()
  5. 9 core/modules/node/node.module \node_title_list()

Gathers a listing of links to nodes.

Parameters

\Drupal\Core\Database\StatementInterface $result: A database result object from a query to fetch node entities. If your query joins the {comment_entity_statistics} table so that the comment_count field is available, a title attribute will be added to show the number of comments.

$title: (optional) A heading for the resulting list.

Return value

array|false A renderable array containing a list of linked node titles fetched from $result, or FALSE if there are no rows in $result.

1 call to node_title_list()
ForumBlockBase::build in core/modules/forum/src/Plugin/Block/ForumBlockBase.php
Builds and returns the renderable array for this block plugin.

File

core/modules/node/node.module, line 221
The core module that allows content to be submitted to the site.

Code

function node_title_list(StatementInterface $result, $title = NULL) {
  $items = [];
  $num_rows = FALSE;
  $nids = [];
  foreach ($result as $row) {

    // Do not use $node->label() or $node->toUrl() here, because we only have
    // database rows, not actual nodes.
    $nids[] = $row->nid;
    $options = !empty($row->comment_count) ? [
      'attributes' => [
        'title' => \Drupal::translation()
          ->formatPlural($row->comment_count, '1 comment', '@count comments'),
      ],
    ] : [];
    $items[] = Link::fromTextAndUrl($row->title, Url::fromRoute('entity.node.canonical', [
      'node' => $row->nid,
    ], $options))
      ->toString();
    $num_rows = TRUE;
  }
  return $num_rows ? [
    '#theme' => 'item_list__node',
    '#items' => $items,
    '#title' => $title,
    '#cache' => [
      'tags' => Cache::mergeTags([
        'node_list',
      ], Cache::buildTags('node', $nids)),
    ],
  ] : FALSE;
}