NodeViewsData.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. namespace Drupal\node;
  3. use Drupal\views\EntityViewsData;
  4. /**
  5. * Provides the views data for the node entity type.
  6. */
  7. class NodeViewsData extends EntityViewsData {
  8. /**
  9. * {@inheritdoc}
  10. */
  11. public function getViewsData() {
  12. $data = parent::getViewsData();
  13. $data['node_field_data']['table']['base']['weight'] = -10;
  14. $data['node_field_data']['table']['base']['access query tag'] = 'node_access';
  15. $data['node_field_data']['table']['wizard_id'] = 'node';
  16. $data['node_field_data']['nid']['argument'] = [
  17. 'id' => 'node_nid',
  18. 'name field' => 'title',
  19. 'numeric' => TRUE,
  20. 'validate type' => 'nid',
  21. ];
  22. $data['node_field_data']['title']['field']['default_formatter_settings'] = ['link_to_entity' => TRUE];
  23. $data['node_field_data']['title']['field']['link_to_node default'] = TRUE;
  24. $data['node_field_data']['type']['argument']['id'] = 'node_type';
  25. $data['node_field_data']['langcode']['help'] = $this->t('The language of the content or translation.');
  26. $data['node_field_data']['status']['filter']['label'] = $this->t('Published status');
  27. $data['node_field_data']['status']['filter']['type'] = 'yes-no';
  28. // Use status = 1 instead of status <> 0 in WHERE statement.
  29. $data['node_field_data']['status']['filter']['use_equal'] = TRUE;
  30. $data['node_field_data']['status_extra'] = [
  31. 'title' => $this->t('Published status or admin user'),
  32. 'help' => $this->t('Filters out unpublished content if the current user cannot view it.'),
  33. 'filter' => [
  34. 'field' => 'status',
  35. 'id' => 'node_status',
  36. 'label' => $this->t('Published status or admin user'),
  37. ],
  38. ];
  39. $data['node_field_data']['promote']['help'] = $this->t('A boolean indicating whether the node is visible on the front page.');
  40. $data['node_field_data']['promote']['filter']['label'] = $this->t('Promoted to front page status');
  41. $data['node_field_data']['promote']['filter']['type'] = 'yes-no';
  42. $data['node_field_data']['sticky']['help'] = $this->t('A boolean indicating whether the node should sort to the top of content lists.');
  43. $data['node_field_data']['sticky']['filter']['label'] = $this->t('Sticky status');
  44. $data['node_field_data']['sticky']['filter']['type'] = 'yes-no';
  45. $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.');
  46. $data['node']['path'] = [
  47. 'field' => [
  48. 'title' => $this->t('Path'),
  49. 'help' => $this->t('The aliased path to this content.'),
  50. 'id' => 'node_path',
  51. ],
  52. ];
  53. $data['node']['node_bulk_form'] = [
  54. 'title' => $this->t('Node operations bulk form'),
  55. 'help' => $this->t('Add a form element that lets you run operations on multiple nodes.'),
  56. 'field' => [
  57. 'id' => 'node_bulk_form',
  58. ],
  59. ];
  60. // Bogus fields for aliasing purposes.
  61. // @todo Add similar support to any date field
  62. // @see https://www.drupal.org/node/2337507
  63. $data['node_field_data']['created_fulldate'] = [
  64. 'title' => $this->t('Created date'),
  65. 'help' => $this->t('Date in the form of CCYYMMDD.'),
  66. 'argument' => [
  67. 'field' => 'created',
  68. 'id' => 'date_fulldate',
  69. ],
  70. ];
  71. $data['node_field_data']['created_year_month'] = [
  72. 'title' => $this->t('Created year + month'),
  73. 'help' => $this->t('Date in the form of YYYYMM.'),
  74. 'argument' => [
  75. 'field' => 'created',
  76. 'id' => 'date_year_month',
  77. ],
  78. ];
  79. $data['node_field_data']['created_year'] = [
  80. 'title' => $this->t('Created year'),
  81. 'help' => $this->t('Date in the form of YYYY.'),
  82. 'argument' => [
  83. 'field' => 'created',
  84. 'id' => 'date_year',
  85. ],
  86. ];
  87. $data['node_field_data']['created_month'] = [
  88. 'title' => $this->t('Created month'),
  89. 'help' => $this->t('Date in the form of MM (01 - 12).'),
  90. 'argument' => [
  91. 'field' => 'created',
  92. 'id' => 'date_month',
  93. ],
  94. ];
  95. $data['node_field_data']['created_day'] = [
  96. 'title' => $this->t('Created day'),
  97. 'help' => $this->t('Date in the form of DD (01 - 31).'),
  98. 'argument' => [
  99. 'field' => 'created',
  100. 'id' => 'date_day',
  101. ],
  102. ];
  103. $data['node_field_data']['created_week'] = [
  104. 'title' => $this->t('Created week'),
  105. 'help' => $this->t('Date in the form of WW (01 - 53).'),
  106. 'argument' => [
  107. 'field' => 'created',
  108. 'id' => 'date_week',
  109. ],
  110. ];
  111. $data['node_field_data']['changed_fulldate'] = [
  112. 'title' => $this->t('Updated date'),
  113. 'help' => $this->t('Date in the form of CCYYMMDD.'),
  114. 'argument' => [
  115. 'field' => 'changed',
  116. 'id' => 'date_fulldate',
  117. ],
  118. ];
  119. $data['node_field_data']['changed_year_month'] = [
  120. 'title' => $this->t('Updated year + month'),
  121. 'help' => $this->t('Date in the form of YYYYMM.'),
  122. 'argument' => [
  123. 'field' => 'changed',
  124. 'id' => 'date_year_month',
  125. ],
  126. ];
  127. $data['node_field_data']['changed_year'] = [
  128. 'title' => $this->t('Updated year'),
  129. 'help' => $this->t('Date in the form of YYYY.'),
  130. 'argument' => [
  131. 'field' => 'changed',
  132. 'id' => 'date_year',
  133. ],
  134. ];
  135. $data['node_field_data']['changed_month'] = [
  136. 'title' => $this->t('Updated month'),
  137. 'help' => $this->t('Date in the form of MM (01 - 12).'),
  138. 'argument' => [
  139. 'field' => 'changed',
  140. 'id' => 'date_month',
  141. ],
  142. ];
  143. $data['node_field_data']['changed_day'] = [
  144. 'title' => $this->t('Updated day'),
  145. 'help' => $this->t('Date in the form of DD (01 - 31).'),
  146. 'argument' => [
  147. 'field' => 'changed',
  148. 'id' => 'date_day',
  149. ],
  150. ];
  151. $data['node_field_data']['changed_week'] = [
  152. 'title' => $this->t('Updated week'),
  153. 'help' => $this->t('Date in the form of WW (01 - 53).'),
  154. 'argument' => [
  155. 'field' => 'changed',
  156. 'id' => 'date_week',
  157. ],
  158. ];
  159. $data['node_field_data']['uid']['help'] = $this->t('The user authoring the content. If you need more fields than the uid add the content: author relationship');
  160. $data['node_field_data']['uid']['filter']['id'] = 'user_name';
  161. $data['node_field_data']['uid']['relationship']['title'] = $this->t('Content author');
  162. $data['node_field_data']['uid']['relationship']['help'] = $this->t('Relate content to the user who created it.');
  163. $data['node_field_data']['uid']['relationship']['label'] = $this->t('author');
  164. $data['node']['node_listing_empty'] = [
  165. 'title' => $this->t('Empty Node Frontpage behavior'),
  166. 'help' => $this->t('Provides a link to the node add overview page.'),
  167. 'area' => [
  168. 'id' => 'node_listing_empty',
  169. ],
  170. ];
  171. $data['node_field_data']['uid_revision']['title'] = $this->t('User has a revision');
  172. $data['node_field_data']['uid_revision']['help'] = $this->t('All nodes where a certain user has a revision');
  173. $data['node_field_data']['uid_revision']['real field'] = 'nid';
  174. $data['node_field_data']['uid_revision']['filter']['id'] = 'node_uid_revision';
  175. $data['node_field_data']['uid_revision']['argument']['id'] = 'node_uid_revision';
  176. $data['node_field_revision']['table']['wizard_id'] = 'node_revision';
  177. // Advertise this table as a possible base table.
  178. $data['node_field_revision']['table']['base']['help'] = $this->t('Content revision is a history of changes to content.');
  179. $data['node_field_revision']['table']['base']['defaults']['title'] = 'title';
  180. $data['node_field_revision']['nid']['argument'] = [
  181. 'id' => 'node_nid',
  182. 'numeric' => TRUE,
  183. ];
  184. // @todo the NID field needs different behaviour on revision/non-revision
  185. // tables. It would be neat if this could be encoded in the base field
  186. // definition.
  187. $data['node_field_revision']['nid']['relationship']['id'] = 'standard';
  188. $data['node_field_revision']['nid']['relationship']['base'] = 'node_field_data';
  189. $data['node_field_revision']['nid']['relationship']['base field'] = 'nid';
  190. $data['node_field_revision']['nid']['relationship']['title'] = $this->t('Content');
  191. $data['node_field_revision']['nid']['relationship']['label'] = $this->t('Get the actual content from a content revision.');
  192. $data['node_field_revision']['vid'] = [
  193. 'argument' => [
  194. 'id' => 'node_vid',
  195. 'numeric' => TRUE,
  196. ],
  197. 'relationship' => [
  198. 'id' => 'standard',
  199. 'base' => 'node_field_data',
  200. 'base field' => 'vid',
  201. 'title' => $this->t('Content'),
  202. 'label' => $this->t('Get the actual content from a content revision.'),
  203. ],
  204. ] + $data['node_field_revision']['vid'];
  205. $data['node_field_revision']['langcode']['help'] = $this->t('The language the original content is in.');
  206. $data['node_revision']['revision_uid']['help'] = $this->t('Relate a content revision to the user who created the revision.');
  207. $data['node_revision']['revision_uid']['relationship']['label'] = $this->t('revision user');
  208. $data['node_field_revision']['table']['wizard_id'] = 'node_field_revision';
  209. $data['node_field_revision']['table']['join']['node_field_data']['left_field'] = 'vid';
  210. $data['node_field_revision']['table']['join']['node_field_data']['field'] = 'vid';
  211. $data['node_field_revision']['status']['filter']['label'] = $this->t('Published');
  212. $data['node_field_revision']['status']['filter']['type'] = 'yes-no';
  213. $data['node_field_revision']['status']['filter']['use_equal'] = TRUE;
  214. $data['node_field_revision']['promote']['help'] = $this->t('A boolean indicating whether the node is visible on the front page.');
  215. $data['node_field_revision']['sticky']['help'] = $this->t('A boolean indicating whether the node should sort to the top of content lists.');
  216. $data['node_field_revision']['langcode']['help'] = $this->t('The language of the content or translation.');
  217. $data['node_field_revision']['link_to_revision'] = [
  218. 'field' => [
  219. 'title' => $this->t('Link to revision'),
  220. 'help' => $this->t('Provide a simple link to the revision.'),
  221. 'id' => 'node_revision_link',
  222. 'click sortable' => FALSE,
  223. ],
  224. ];
  225. $data['node_field_revision']['revert_revision'] = [
  226. 'field' => [
  227. 'title' => $this->t('Link to revert revision'),
  228. 'help' => $this->t('Provide a simple link to revert to the revision.'),
  229. 'id' => 'node_revision_link_revert',
  230. 'click sortable' => FALSE,
  231. ],
  232. ];
  233. $data['node_field_revision']['delete_revision'] = [
  234. 'field' => [
  235. 'title' => $this->t('Link to delete revision'),
  236. 'help' => $this->t('Provide a simple link to delete the content revision.'),
  237. 'id' => 'node_revision_link_delete',
  238. 'click sortable' => FALSE,
  239. ],
  240. ];
  241. // Define the base group of this table. Fields that don't have a group defined
  242. // will go into this field by default.
  243. $data['node_access']['table']['group'] = $this->t('Content access');
  244. // For other base tables, explain how we join.
  245. $data['node_access']['table']['join'] = [
  246. 'node_field_data' => [
  247. 'left_field' => 'nid',
  248. 'field' => 'nid',
  249. ],
  250. ];
  251. $data['node_access']['nid'] = [
  252. 'title' => $this->t('Access'),
  253. 'help' => $this->t('Filter by access.'),
  254. 'filter' => [
  255. 'id' => 'node_access',
  256. 'help' => $this->t('Filter for content by view access. <strong>Not necessary if you are using node as your base table.</strong>'),
  257. ],
  258. ];
  259. // Add search table, fields, filters, etc., but only if a page using the
  260. // node_search plugin is enabled.
  261. if (\Drupal::moduleHandler()->moduleExists('search')) {
  262. $enabled = FALSE;
  263. $search_page_repository = \Drupal::service('search.search_page_repository');
  264. foreach ($search_page_repository->getActiveSearchpages() as $page) {
  265. if ($page->getPlugin()->getPluginId() == 'node_search') {
  266. $enabled = TRUE;
  267. break;
  268. }
  269. }
  270. if ($enabled) {
  271. $data['node_search_index']['table']['group'] = $this->t('Search');
  272. // Automatically join to the node table (or actually, node_field_data).
  273. // Use a Views table alias to allow other modules to use this table too,
  274. // if they use the search index.
  275. $data['node_search_index']['table']['join'] = [
  276. 'node_field_data' => [
  277. 'left_field' => 'nid',
  278. 'field' => 'sid',
  279. 'table' => 'search_index',
  280. 'extra' => "node_search_index.type = 'node_search' AND node_search_index.langcode = node_field_data.langcode",
  281. ]
  282. ];
  283. $data['node_search_total']['table']['join'] = [
  284. 'node_search_index' => [
  285. 'left_field' => 'word',
  286. 'field' => 'word',
  287. ],
  288. ];
  289. $data['node_search_dataset']['table']['join'] = [
  290. 'node_field_data' => [
  291. 'left_field' => 'sid',
  292. 'left_table' => 'node_search_index',
  293. 'field' => 'sid',
  294. 'table' => 'search_dataset',
  295. 'extra' => 'node_search_index.type = node_search_dataset.type AND node_search_index.langcode = node_search_dataset.langcode',
  296. 'type' => 'INNER',
  297. ],
  298. ];
  299. $data['node_search_index']['score'] = [
  300. 'title' => $this->t('Score'),
  301. 'help' => $this->t('The score of the search item. This will not be used if the search filter is not also present.'),
  302. 'field' => [
  303. 'id' => 'search_score',
  304. 'float' => TRUE,
  305. 'no group by' => TRUE,
  306. ],
  307. 'sort' => [
  308. 'id' => 'search_score',
  309. 'no group by' => TRUE,
  310. ],
  311. ];
  312. $data['node_search_index']['keys'] = [
  313. 'title' => $this->t('Search Keywords'),
  314. 'help' => $this->t('The keywords to search for.'),
  315. 'filter' => [
  316. 'id' => 'search_keywords',
  317. 'no group by' => TRUE,
  318. 'search_type' => 'node_search',
  319. ],
  320. 'argument' => [
  321. 'id' => 'search',
  322. 'no group by' => TRUE,
  323. 'search_type' => 'node_search',
  324. ],
  325. ];
  326. }
  327. }
  328. return $data;
  329. }
  330. }