NodeViewsData.php 14 KB

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