1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
<?php
namespace Drupal\node;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\views\EntityViewsData;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the views data for the node entity type.
*/
class NodeViewsData extends EntityViewsData {
/**
* Constructs an NodeViewsData object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type to provide views integration for.
* @param \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $storage_controller
* The storage handler used for this entity type.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
* The translation manager.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Extension\ModuleExtensionList|null $moduleExtensionList
* The module extension list.
*/
public function __construct(
EntityTypeInterface $entity_type,
SqlEntityStorageInterface $storage_controller,
EntityTypeManagerInterface $entity_type_manager,
ModuleHandlerInterface $module_handler,
TranslationInterface $translation_manager,
EntityFieldManagerInterface $entity_field_manager,
protected ?ModuleExtensionList $moduleExtensionList = NULL,
) {
parent::__construct($entity_type, $storage_controller, $entity_type_manager, $module_handler, $translation_manager, $entity_field_manager);
if ($this->moduleExtensionList === NULL) {
@trigger_error('Calling ' . __METHOD__ . '() without the $moduleExtensionList argument is deprecated in drupal:11.2.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3493129', E_USER_DEPRECATED);
$this->moduleExtensionList = \Drupal::service('extension.list.module');
}
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('entity_type.manager')->getStorage($entity_type->id()),
$container->get('entity_type.manager'),
$container->get('module_handler'),
$container->get('string_translation'),
$container->get('entity_field.manager'),
$container->get('extension.list.module')
);
}
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
$data['node_field_data']['table']['base']['weight'] = -10;
$data['node_field_data']['table']['base']['access query tag'] = 'node_access';
$data['node_field_data']['table']['wizard_id'] = 'node';
$data['node_field_data']['nid']['argument'] = [
'id' => 'node_nid',
'name field' => 'title',
'numeric' => TRUE,
'validate type' => 'nid',
];
$data['node_field_data']['title']['field']['default_formatter_settings'] = ['link_to_entity' => TRUE];
$data['node_field_data']['title']['field']['link_to_node default'] = TRUE;
$data['node_field_data']['type']['argument']['id'] = 'node_type';
$data['node_field_data']['status']['filter']['label'] = $this->t('Published status');
$data['node_field_data']['status']['filter']['type'] = 'yes-no';
// Use status = 1 instead of status <> 0 in WHERE statement.
$data['node_field_data']['status']['filter']['use_equal'] = TRUE;
// Check for any extensions that use node grants and block the use of this
// filter. If this filter is blocked then provide a helpful message.
$node_access_implementations = $this->getNodeAccessImplementations();
$node_access_implementation_count = count($node_access_implementations);
if ($node_access_implementation_count === 0) {
$status_extra_help_text = $this->t('Filters out unpublished content if the current user cannot view it.');
}
else {
uasort($node_access_implementations, 'strnatcasecmp');
$status_extra_help_text = new PluralTranslatableMarkup(
$node_access_implementation_count,
'This filter has no effect because the %module module controls access.',
'This filter has no effect because these modules control access: %modules.',
['%module' => reset($node_access_implementations), '%modules' => implode(', ', $node_access_implementations)]
);
}
$data['node_field_data']['status_extra'] = [
'title' => $this->t('Published status or admin user'),
'help' => $status_extra_help_text,
'filter' => [
'field' => 'status',
'id' => 'node_status',
'label' => $this->t('Published status or admin user'),
],
];
$data['node_field_data']['promote']['help'] = $this->t('A boolean indicating whether the node is visible on the front page.');
$data['node_field_data']['promote']['filter']['label'] = $this->t('Promoted to front page status');
$data['node_field_data']['promote']['filter']['type'] = 'yes-no';
$data['node_field_data']['sticky']['help'] = $this->t('A boolean indicating whether the node should sort to the top of content lists.');
$data['node_field_data']['sticky']['filter']['label'] = $this->t('Sticky status');
$data['node_field_data']['sticky']['filter']['type'] = 'yes-no';
$data['node_field_data']['sticky']['sort']['help'] = $this->t('Whether or not the content is sticky. To list sticky content first, set this to descending.');
$data['node']['node_bulk_form'] = [
'title' => $this->t('Node operations bulk form'),
'help' => $this->t('Add a form element that lets you run operations on multiple nodes.'),
'field' => [
'id' => 'node_bulk_form',
],
];
// Bogus fields for aliasing purposes.
// @todo Add similar support to any date field
// @see https://www.drupal.org/node/2337507
$data['node_field_data']['created_fulldate'] = [
'title' => $this->t('Created date'),
'help' => $this->t('Date in the form of CCYYMMDD.'),
'argument' => [
'field' => 'created',
'id' => 'date_fulldate',
],
];
$data['node_field_data']['created_year_month'] = [
'title' => $this->t('Created year + month'),
'help' => $this->t('Date in the form of YYYYMM.'),
'argument' => [
'field' => 'created',
'id' => 'date_year_month',
],
];
$data['node_field_data']['created_year'] = [
'title' => $this->t('Created year'),
'help' => $this->t('Date in the form of YYYY.'),
'argument' => [
'field' => 'created',
'id' => 'date_year',
],
];
$data['node_field_data']['created_month'] = [
'title' => $this->t('Created month'),
'help' => $this->t('Date in the form of MM (01 - 12).'),
'argument' => [
'field' => 'created',
'id' => 'date_month',
],
];
$data['node_field_data']['created_day'] = [
'title' => $this->t('Created day'),
'help' => $this->t('Date in the form of DD (01 - 31).'),
'argument' => [
'field' => 'created',
'id' => 'date_day',
],
];
$data['node_field_data']['created_week'] = [
'title' => $this->t('Created week'),
'help' => $this->t('Date in the form of WW (01 - 53).'),
'argument' => [
'field' => 'created',
'id' => 'date_week',
],
];
$data['node_field_data']['changed_fulldate'] = [
'title' => $this->t('Updated date'),
'help' => $this->t('Date in the form of CCYYMMDD.'),
'argument' => [
'field' => 'changed',
'id' => 'date_fulldate',
],
];
$data['node_field_data']['changed_year_month'] = [
'title' => $this->t('Updated year + month'),
'help' => $this->t('Date in the form of YYYYMM.'),
'argument' => [
'field' => 'changed',
'id' => 'date_year_month',
],
];
$data['node_field_data']['changed_year'] = [
'title' => $this->t('Updated year'),
'help' => $this->t('Date in the form of YYYY.'),
'argument' => [
'field' => 'changed',
'id' => 'date_year',
],
];
$data['node_field_data']['changed_month'] = [
'title' => $this->t('Updated month'),
'help' => $this->t('Date in the form of MM (01 - 12).'),
'argument' => [
'field' => 'changed',
'id' => 'date_month',
],
];
$data['node_field_data']['changed_day'] = [
'title' => $this->t('Updated day'),
'help' => $this->t('Date in the form of DD (01 - 31).'),
'argument' => [
'field' => 'changed',
'id' => 'date_day',
],
];
$data['node_field_data']['changed_week'] = [
'title' => $this->t('Updated week'),
'help' => $this->t('Date in the form of WW (01 - 53).'),
'argument' => [
'field' => 'changed',
'id' => 'date_week',
],
];
$data['node']['node_listing_empty'] = [
'title' => $this->t('Empty Node Frontpage behavior'),
'help' => $this->t('Provides a link to the node add overview page.'),
'area' => [
'id' => 'node_listing_empty',
],
];
$data['node_field_data']['uid_revision']['title'] = $this->t('User has a revision');
$data['node_field_data']['uid_revision']['help'] = $this->t('All nodes where a certain user has a revision');
$data['node_field_data']['uid_revision']['real field'] = 'nid';
$data['node_field_data']['uid_revision']['filter']['id'] = 'node_uid_revision';
$data['node_field_data']['uid_revision']['argument']['id'] = 'node_uid_revision';
$data['node_field_revision']['table']['wizard_id'] = 'node_revision';
// Advertise this table as a possible base table.
$data['node_field_revision']['table']['base']['help'] = $this->t('Content revision is a history of changes to content.');
$data['node_field_revision']['table']['base']['defaults']['title'] = 'title';
$data['node_field_revision']['nid']['argument'] = [
'id' => 'node_nid',
'numeric' => TRUE,
];
// @todo the NID field needs different behavior on revision/non-revision
// tables. It would be neat if this could be encoded in the base field
// definition.
$data['node_field_revision']['vid'] = [
'argument' => [
'id' => 'node_vid',
'numeric' => TRUE,
],
] + $data['node_field_revision']['vid'];
$data['node_field_revision']['langcode']['help'] = $this->t('The language the original content is in.');
$data['node_field_revision']['table']['wizard_id'] = 'node_field_revision';
$data['node_field_revision']['status']['filter']['label'] = $this->t('Published');
$data['node_field_revision']['status']['filter']['type'] = 'yes-no';
$data['node_field_revision']['status']['filter']['use_equal'] = TRUE;
$data['node_field_revision']['promote']['help'] = $this->t('A boolean indicating whether the node is visible on the front page.');
$data['node_field_revision']['sticky']['help'] = $this->t('A boolean indicating whether the node should sort to the top of content lists.');
$data['node_field_revision']['langcode']['help'] = $this->t('The language of the content or translation.');
$data['node_field_revision']['link_to_revision'] = [
'field' => [
'title' => $this->t('Link to revision'),
'help' => $this->t('Provide a simple link to the revision.'),
'id' => 'node_revision_link',
'click sortable' => FALSE,
],
];
$data['node_field_revision']['revert_revision'] = [
'field' => [
'title' => $this->t('Link to revert revision'),
'help' => $this->t('Provide a simple link to revert to the revision.'),
'id' => 'node_revision_link_revert',
'click sortable' => FALSE,
],
];
$data['node_field_revision']['delete_revision'] = [
'field' => [
'title' => $this->t('Link to delete revision'),
'help' => $this->t('Provide a simple link to delete the content revision.'),
'id' => 'node_revision_link_delete',
'click sortable' => FALSE,
],
];
// Define the base group of this table. Fields that don't have a group
// defined will go into this field by default.
$data['node_access']['table']['group'] = $this->t('Content access');
// For other base tables, explain how we join.
$data['node_access']['table']['join'] = [
'node_field_data' => [
'left_field' => 'nid',
'field' => 'nid',
],
];
$data['node_access']['nid'] = [
'title' => $this->t('Access'),
'help' => $this->t('Filter by access.'),
'filter' => [
'id' => 'node_access',
'help' => $this->t('Filter for content by view access. <strong>Not necessary if you are using node as your base table.</strong>'),
],
];
// Add search table, fields, filters, etc., but only if a page using the
// node_search plugin is enabled.
if (\Drupal::moduleHandler()->moduleExists('search')) {
$enabled = FALSE;
$search_page_repository = \Drupal::service('search.search_page_repository');
foreach ($search_page_repository->getActiveSearchPages() as $page) {
if ($page->getPlugin()->getPluginId() == 'node_search') {
$enabled = TRUE;
break;
}
}
if ($enabled) {
$data['node_search_index']['table']['group'] = $this->t('Search');
// Automatically join to the node table (or actually, node_field_data).
// Use a Views table alias to allow other modules to use this table too,
// if they use the search index.
$data['node_search_index']['table']['join'] = [
'node_field_data' => [
'left_field' => 'nid',
'field' => 'sid',
'table' => 'search_index',
'extra' => "node_search_index.type = 'node_search' AND node_search_index.langcode = node_field_data.langcode",
],
];
$data['node_search_total']['table']['join'] = [
'node_search_index' => [
'left_field' => 'word',
'field' => 'word',
],
];
$data['node_search_dataset']['table']['join'] = [
'node_field_data' => [
'left_field' => 'sid',
'left_table' => 'node_search_index',
'field' => 'sid',
'table' => 'search_dataset',
'extra' => 'node_search_index.type = node_search_dataset.type AND node_search_index.langcode = node_search_dataset.langcode',
'type' => 'INNER',
],
];
$data['node_search_index']['score'] = [
'title' => $this->t('Score'),
'help' => $this->t('The score of the search item. This will not be used if the search filter is not also present.'),
'field' => [
'id' => 'search_score',
'float' => TRUE,
'no group by' => TRUE,
],
'sort' => [
'id' => 'search_score',
'no group by' => TRUE,
],
];
$data['node_search_index']['keys'] = [
'title' => $this->t('Search Keywords'),
'help' => $this->t('The keywords to search for.'),
'filter' => [
'id' => 'search_keywords',
'no group by' => TRUE,
'search_type' => 'node_search',
],
'argument' => [
'id' => 'search',
'no group by' => TRUE,
'search_type' => 'node_search',
],
];
}
}
return $data;
}
/**
* Returns a list of modules that implements a node access hook.
*
* @return array<string,string>
* An associative array where keys are module machine names and values are
* the human-readable names.
*/
private function getNodeAccessImplementations(): array {
$implementations = [];
if ($this->moduleHandler->hasImplementations('node_grants')) {
$module_data = $this->moduleExtensionList->getAllInstalledInfo();
foreach (['node_grants', 'node_grants_alter'] as $hook) {
$this->moduleHandler->invokeAllWith(
$hook,
static function (callable $hook, string $module) use (&$implementations, $module_data) {
$implementations[$module] = $module_data[$module]['name'];
}
);
}
}
return $implementations;
}
}
|