@@ -61,58 +61,21 @@ public static function create(ContainerInterface $container) {
6161 */
6262 public function getContent () {
6363 // First we'll tell the user what's going on. This content can be found
64- // in the twig template file: templates/description.html.twig.
65- // @todo: Set up links to create nodes and point to devel module.
66- // https://www.drupal.org/project/examples/issues/2986434
64+ // in the twig template file: templates/description.html.twig. It will be
65+ // inserted by the theming function pager_example_description().
6766 $ build = [
6867 'description ' => [
6968 '#theme ' => 'pager_example_description ' ,
70- '#description ' => 'foo ' ,
69+ '#description ' => 'description ' ,
7170 '#attributes ' => [],
7271 ],
7372 ];
7473
75- // We need to count the number of nodes so that we can tell the user to add
76- // some if there aren't any.
77- $ query = $ this ->nodeStorage ->getQuery ()
78- ->addTag ('node_access ' )
79- ->count ();
80-
81- // The node_access tag does not trigger a check on whether a user has the
82- // ability to view unpublished content. The 'bypass node access' permission
83- // is really more than we need. But, there is no separate permission for
84- // viewing unpublished content. There is a permission to 'view own
85- // unpublished content', but we don't have a good way of using that in this
86- // query. So, unfortunately this query will incorrectly eliminate even those
87- // unpublished nodes that the user may, in fact, be allowed to view.
88- if (!$ this ->currentUser ->hasPermission ('bypass node access ' )) {
89- $ query ->condition ('status ' , 1 );
90- }
91- $ count_nodes = $ query ->execute ();
92-
93- if ($ count_nodes == 0 ) {
94- if ($ this ->currentUser ->hasPermission ('create page content ' )) {
95- $ build ['no-nodes ' ] = [
96- '#markup ' => $ this ->t ('There are no nodes to display.
97- Please <a href=":url">create a node</a>. ' ,
98- [
99- ':url ' => Url::fromRoute ('node.add ' , ['node_type ' => 'page ' ])->toString (),
100- ]
101- ),
102- ];
103- }
104- else {
105- $ build ['no-nodes ' ] = [
106- '#markup ' => $ this ->t ('There are no nodes to display. ' ),
107- ];
108- }
109-
110- // Ensure that Drupal clears the cache when nodes have been published,
111- // unpublished, added or deleted; and when user permissions change.
112- $ build ['#cache ' ]['tags ' ][] = 'node_list ' ;
113- $ build ['#cache ' ]['contexts ' ][] = 'user.permissions ' ;
114- return $ build ;
115- }
74+ // Ensure that this page's cache is invalidated when nodes have been
75+ // published, unpublished, added or deleted; and when user permissions
76+ // change.
77+ $ build ['#cache ' ]['tags ' ][] = 'node_list ' ;
78+ $ build ['#cache ' ]['contexts ' ][] = 'user.permissions ' ;
11679
11780 // Now we want to get our tabular data. We select nodes from node storage
11881 // limited by 2 per page and sort by nid DESC because we want to show newest
@@ -155,21 +118,20 @@ public function getContent() {
155118
156119 // Build a render array which will be themed as a table with a pager.
157120 $ build ['pager_example ' ] = [
158- '#rows ' => $ rows ,
159- '#header ' => [$ this ->t ('NID ' ), $ this ->t ('Title ' )],
160121 '#type ' => 'table ' ,
161- '#empty ' => $ this ->t ('No content available. ' ),
122+ '#header ' => [$ this ->t ('NID ' ), $ this ->t ('Title ' )],
123+ '#rows ' => $ rows ,
124+ '#empty ' => $ this ->t ('There are no nodes to display. Please <a href=":url">create a node</a>. ' , [
125+ ':url ' => Url::fromRoute ('node.add ' , ['node_type ' => 'page ' ])->toString (),
126+ ]),
162127 ];
128+ // Add our pager element so the user can choose which pagination to see.
129+ // This will add a '?page=1' fragment to the links to subsequent pages.
163130 $ build ['pager ' ] = [
164131 '#type ' => 'pager ' ,
165132 '#weight ' => 10 ,
166133 ];
167134
168- // Ensure that Drupal clears the cache when nodes have been published,
169- // unpublished, added or deleted; and when user permissions change.
170- $ build ['#cache ' ]['tags ' ][] = 'node_list ' ;
171- $ build ['#cache ' ]['contexts ' ][] = 'user.permissions ' ;
172-
173135 return $ build ;
174136 }
175137
0 commit comments