function PagerExamplePage::getContent

Same name in other branches
  1. 3.x modules/pager_example/src/Controller/PagerExamplePage.php \Drupal\pager_example\Controller\PagerExamplePage::getContent()

Content callback for the pager_example.page route.

1 string reference to 'PagerExamplePage::getContent'
pager_example.routing.yml in modules/pager_example/pager_example.routing.yml
modules/pager_example/pager_example.routing.yml

File

modules/pager_example/src/Controller/PagerExamplePage.php, line 62

Class

PagerExamplePage
Controller for pager_example.page route.

Namespace

Drupal\pager_example\Controller

Code

public function getContent() {
    // First we'll tell the user what's going on. This content can be found
    // in the twig template file: templates/description.html.twig. It will be
    // inserted by the theming function pager_example_description().
    $build = [
        'description' => [
            '#theme' => 'pager_example_description',
            '#description' => $this->t('description'),
            '#attributes' => [],
        ],
    ];
    // Ensure that this page's cache is invalidated when nodes have been
    // published, unpublished, added or deleted; and when user permissions
    // change.
    $build['#cache']['tags'][] = 'node_list';
    $build['#cache']['contexts'][] = 'user.permissions';
    // Now we want to get our tabular data. We select nodes from node storage
    // limited by 2 per page and sort by nid DESC because we want to show newest
    // node first. Additionally, we check that the user has permission to
    // view the node.
    $query = $this->nodeStorage
        ->getQuery()
        ->sort('nid', 'DESC')
        ->addTag('node_access')
        ->pager(2);
    // The node_access tag does not trigger a check on whether a user has the
    // ability to view unpublished content. The 'bypass node access' permission
    // is really more than we need. But, there is no separate permission for
    // viewing unpublished content. There is a permission to 'view own
    // unpublished content', but we don't have a good way of using that in this
    // query. So, unfortunately this query will incorrectly eliminate even those
    // unpublished nodes that the user may, in fact, be allowed to view.
    if (!$this->currentUser
        ->hasPermission('bypass node access')) {
        $query->condition('status', 1);
    }
    $query->accessCheck();
    $entity_ids = $query->execute();
    $nodes = $this->nodeStorage
        ->loadMultiple($entity_ids);
    // We are going to output the results in a table so we set up the rows.
    $rows = [];
    foreach ($nodes as $node) {
        // There are certain things (besides unpublished nodes) that the
        // node_access tag won't prevent from being seen. The only way to get at
        // those is by explicitly checking for (view) access on a node-by-node
        // basis. In order to prevent the pager from looking strange, we will
        // "mask" these nodes that should not be accessible. If we don't do this
        // masking, it's possible that we'd have lots of pages that don't show any
        // content.
        $rows[] = [
            'nid' => $node->access('view') ? $node->id() : $this->t('XXXXXX'),
            'title' => $node->access('view') ? $node->getTitle() : $this->t('Redacted'),
        ];
    }
    // Build a render array which will be themed as a table with a pager.
    $build['pager_example'] = [
        '#type' => 'table',
        '#header' => [
            $this->t('NID'),
            $this->t('Title'),
        ],
        '#rows' => $rows,
        '#empty' => $this->t('There are no nodes to display. Please <a href=":url">create a node</a>.', [
            ':url' => Url::fromRoute('node.add', [
                'node_type' => 'page',
            ])->toString(),
        ]),
    ];
    // Add our pager element so the user can choose which pagination to see.
    // This will add a '?page=1' fragment to the links to subsequent pages.
    $build['pager'] = [
        '#type' => 'pager',
        '#weight' => 10,
    ];
    return $build;
}