@@ -61,58 +61,21 @@ public static function create(ContainerInterface $container) {
61
61
*/
62
62
public function getContent () {
63
63
// 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().
67
66
$ build = [
68
67
'description ' => [
69
68
'#theme ' => 'pager_example_description ' ,
70
- '#description ' => 'foo ' ,
69
+ '#description ' => 'description ' ,
71
70
'#attributes ' => [],
72
71
],
73
72
];
74
73
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 ' ;
116
79
117
80
// Now we want to get our tabular data. We select nodes from node storage
118
81
// limited by 2 per page and sort by nid DESC because we want to show newest
@@ -155,21 +118,20 @@ public function getContent() {
155
118
156
119
// Build a render array which will be themed as a table with a pager.
157
120
$ build ['pager_example ' ] = [
158
- '#rows ' => $ rows ,
159
- '#header ' => [$ this ->t ('NID ' ), $ this ->t ('Title ' )],
160
121
'#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
+ ]),
162
127
];
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.
163
130
$ build ['pager ' ] = [
164
131
'#type ' => 'pager ' ,
165
132
'#weight ' => 10 ,
166
133
];
167
134
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
-
173
135
return $ build ;
174
136
}
175
137
0 commit comments