FileViewsData.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace Drupal\file;
  3. use Drupal\views\EntityViewsData;
  4. /**
  5. * Provides views data for the file entity type.
  6. */
  7. class FileViewsData extends EntityViewsData {
  8. /**
  9. * {@inheritdoc}
  10. */
  11. public function getViewsData() {
  12. $data = parent::getViewsData();
  13. // @TODO There is no corresponding information in entity metadata.
  14. $data['file_managed']['table']['base']['help'] = $this->t('Files maintained by Drupal and various modules.');
  15. $data['file_managed']['table']['base']['defaults']['field'] = 'filename';
  16. $data['file_managed']['table']['wizard_id'] = 'file_managed';
  17. $data['file_managed']['fid']['argument'] = [
  18. 'id' => 'file_fid',
  19. // The field to display in the summary.
  20. 'name field' => 'filename',
  21. 'numeric' => TRUE,
  22. ];
  23. $data['file_managed']['fid']['relationship'] = [
  24. 'title' => $this->t('File usage'),
  25. 'help' => $this->t('Relate file entities to their usage.'),
  26. 'id' => 'standard',
  27. 'base' => 'file_usage',
  28. 'base field' => 'fid',
  29. 'field' => 'fid',
  30. 'label' => $this->t('File usage'),
  31. ];
  32. $data['file_managed']['uri']['field']['default_formatter'] = 'file_uri';
  33. $data['file_managed']['filemime']['field']['default_formatter'] = 'file_filemime';
  34. $data['file_managed']['extension'] = [
  35. 'title' => $this->t('Extension'),
  36. 'help' => $this->t('The extension of the file.'),
  37. 'real field' => 'filename',
  38. 'field' => [
  39. 'entity_type' => 'file',
  40. 'field_name' => 'filename',
  41. 'default_formatter' => 'file_extension',
  42. 'id' => 'field',
  43. 'click sortable' => FALSE,
  44. ],
  45. ];
  46. $data['file_managed']['filesize']['field']['default_formatter'] = 'file_size';
  47. $data['file_managed']['status']['field']['default_formatter_settings'] = [
  48. 'format' => 'custom',
  49. 'format_custom_false' => $this->t('Temporary'),
  50. 'format_custom_true' => $this->t('Permanent'),
  51. ];
  52. $data['file_managed']['status']['filter']['id'] = 'file_status';
  53. $data['file_managed']['uid']['relationship']['title'] = $this->t('User who uploaded');
  54. $data['file_managed']['uid']['relationship']['label'] = $this->t('User who uploaded');
  55. $data['file_usage']['table']['group'] = $this->t('File Usage');
  56. // Provide field-type-things to several base tables; on the core files table
  57. // ("file_managed") so that we can create relationships from files to
  58. // entities, and then on each core entity type base table so that we can
  59. // provide general relationships between entities and files.
  60. $data['file_usage']['table']['join'] = [
  61. 'file_managed' => [
  62. 'field' => 'fid',
  63. 'left_field' => 'fid',
  64. ],
  65. // Link ourselves to the {node_field_data} table
  66. // so we can provide node->file relationships.
  67. 'node_field_data' => [
  68. 'field' => 'id',
  69. 'left_field' => 'nid',
  70. 'extra' => [['field' => 'type', 'value' => 'node']],
  71. ],
  72. // Link ourselves to the {users_field_data} table
  73. // so we can provide user->file relationships.
  74. 'users_field_data' => [
  75. 'field' => 'id',
  76. 'left_field' => 'uid',
  77. 'extra' => [['field' => 'type', 'value' => 'user']],
  78. ],
  79. // Link ourselves to the {comment_field_data} table
  80. // so we can provide comment->file relationships.
  81. 'comment' => [
  82. 'field' => 'id',
  83. 'left_field' => 'cid',
  84. 'extra' => [['field' => 'type', 'value' => 'comment']],
  85. ],
  86. // Link ourselves to the {taxonomy_term_field_data} table
  87. // so we can provide taxonomy_term->file relationships.
  88. 'taxonomy_term_data' => [
  89. 'field' => 'id',
  90. 'left_field' => 'tid',
  91. 'extra' => [['field' => 'type', 'value' => 'taxonomy_term']],
  92. ],
  93. ];
  94. // Provide a relationship between the files table and each entity type,
  95. // and between each entity type and the files table. Entity->file
  96. // relationships are type-restricted in the joins declared above, and
  97. // file->entity relationships are type-restricted in the relationship
  98. // declarations below.
  99. // Describes relationships between files and nodes.
  100. $data['file_usage']['file_to_node'] = [
  101. 'title' => $this->t('Content'),
  102. 'help' => $this->t('Content that is associated with this file, usually because this file is in a field on the content.'),
  103. // Only provide this field/relationship/etc.,
  104. // when the 'file_managed' base table is present.
  105. 'skip base' => ['node_field_data', 'node_field_revision', 'users_field_data', 'comment_field_data', 'taxonomy_term_field_data'],
  106. 'real field' => 'id',
  107. 'relationship' => [
  108. 'title' => $this->t('Content'),
  109. 'label' => $this->t('Content'),
  110. 'base' => 'node_field_data',
  111. 'base field' => 'nid',
  112. 'relationship field' => 'id',
  113. 'extra' => [['table' => 'file_usage', 'field' => 'type', 'operator' => '=', 'value' => 'node']],
  114. ],
  115. ];
  116. $data['file_usage']['node_to_file'] = [
  117. 'title' => $this->t('File'),
  118. 'help' => $this->t('A file that is associated with this node, usually because it is in a field on the node.'),
  119. // Only provide this field/relationship/etc.,
  120. // when the 'node' base table is present.
  121. 'skip base' => ['file_managed', 'users_field_data', 'comment_field_data', 'taxonomy_term_field_data'],
  122. 'real field' => 'fid',
  123. 'relationship' => [
  124. 'title' => $this->t('File'),
  125. 'label' => $this->t('File'),
  126. 'base' => 'file_managed',
  127. 'base field' => 'fid',
  128. 'relationship field' => 'fid',
  129. ],
  130. ];
  131. // Describes relationships between files and users.
  132. $data['file_usage']['file_to_user'] = [
  133. 'title' => $this->t('User'),
  134. 'help' => $this->t('A user that is associated with this file, usually because this file is in a field on the user.'),
  135. // Only provide this field/relationship/etc.,
  136. // when the 'file_managed' base table is present.
  137. 'skip base' => ['node_field_data', 'node_field_revision', 'users_field_data', 'comment_field_data', 'taxonomy_term_field_data'],
  138. 'real field' => 'id',
  139. 'relationship' => [
  140. 'title' => $this->t('User'),
  141. 'label' => $this->t('User'),
  142. 'base' => 'users',
  143. 'base field' => 'uid',
  144. 'relationship field' => 'id',
  145. 'extra' => [['table' => 'file_usage', 'field' => 'type', 'operator' => '=', 'value' => 'user']],
  146. ],
  147. ];
  148. $data['file_usage']['user_to_file'] = [
  149. 'title' => $this->t('File'),
  150. 'help' => $this->t('A file that is associated with this user, usually because it is in a field on the user.'),
  151. // Only provide this field/relationship/etc.,
  152. // when the 'users' base table is present.
  153. 'skip base' => ['file_managed', 'node_field_data', 'node_field_revision', 'comment_field_data', 'taxonomy_term_field_data'],
  154. 'real field' => 'fid',
  155. 'relationship' => [
  156. 'title' => $this->t('File'),
  157. 'label' => $this->t('File'),
  158. 'base' => 'file_managed',
  159. 'base field' => 'fid',
  160. 'relationship field' => 'fid',
  161. ],
  162. ];
  163. // Describes relationships between files and comments.
  164. $data['file_usage']['file_to_comment'] = [
  165. 'title' => $this->t('Comment'),
  166. 'help' => $this->t('A comment that is associated with this file, usually because this file is in a field on the comment.'),
  167. // Only provide this field/relationship/etc.,
  168. // when the 'file_managed' base table is present.
  169. 'skip base' => ['node_field_data', 'node_field_revision', 'users_field_data', 'comment_field_data', 'taxonomy_term_field_data'],
  170. 'real field' => 'id',
  171. 'relationship' => [
  172. 'title' => $this->t('Comment'),
  173. 'label' => $this->t('Comment'),
  174. 'base' => 'comment_field_data',
  175. 'base field' => 'cid',
  176. 'relationship field' => 'id',
  177. 'extra' => [['table' => 'file_usage', 'field' => 'type', 'operator' => '=', 'value' => 'comment']],
  178. ],
  179. ];
  180. $data['file_usage']['comment_to_file'] = [
  181. 'title' => $this->t('File'),
  182. 'help' => $this->t('A file that is associated with this comment, usually because it is in a field on the comment.'),
  183. // Only provide this field/relationship/etc.,
  184. // when the 'comment' base table is present.
  185. 'skip base' => ['file_managed', 'node_field_data', 'node_field_revision', 'users_field_data', 'taxonomy_term_field_data'],
  186. 'real field' => 'fid',
  187. 'relationship' => [
  188. 'title' => $this->t('File'),
  189. 'label' => $this->t('File'),
  190. 'base' => 'file_managed',
  191. 'base field' => 'fid',
  192. 'relationship field' => 'fid',
  193. ],
  194. ];
  195. // Describes relationships between files and taxonomy_terms.
  196. $data['file_usage']['file_to_taxonomy_term'] = [
  197. 'title' => $this->t('Taxonomy Term'),
  198. 'help' => $this->t('A taxonomy term that is associated with this file, usually because this file is in a field on the taxonomy term.'),
  199. // Only provide this field/relationship/etc.,
  200. // when the 'file_managed' base table is present.
  201. 'skip base' => ['node_field_data', 'node_field_revision', 'users_field_data', 'comment_field_data', 'taxonomy_term_field_data'],
  202. 'real field' => 'id',
  203. 'relationship' => [
  204. 'title' => $this->t('Taxonomy Term'),
  205. 'label' => $this->t('Taxonomy Term'),
  206. 'base' => 'taxonomy_term_data',
  207. 'base field' => 'tid',
  208. 'relationship field' => 'id',
  209. 'extra' => [['table' => 'file_usage', 'field' => 'type', 'operator' => '=', 'value' => 'taxonomy_term']],
  210. ],
  211. ];
  212. $data['file_usage']['taxonomy_term_to_file'] = [
  213. 'title' => $this->t('File'),
  214. 'help' => $this->t('A file that is associated with this taxonomy term, usually because it is in a field on the taxonomy term.'),
  215. // Only provide this field/relationship/etc.,
  216. // when the 'taxonomy_term_data' base table is present.
  217. 'skip base' => ['file_managed', 'node_field_data', 'node_field_revision', 'users_field_data', 'comment_field_data'],
  218. 'real field' => 'fid',
  219. 'relationship' => [
  220. 'title' => $this->t('File'),
  221. 'label' => $this->t('File'),
  222. 'base' => 'file_managed',
  223. 'base field' => 'fid',
  224. 'relationship field' => 'fid',
  225. ],
  226. ];
  227. // Provide basic fields from the {file_usage} table to all of the base tables
  228. // we've declared joins to, because there is no 'skip base' property on these
  229. // fields.
  230. $data['file_usage']['module'] = [
  231. 'title' => $this->t('Module'),
  232. 'help' => $this->t('The module managing this file relationship.'),
  233. 'field' => [
  234. 'id' => 'standard',
  235. ],
  236. 'filter' => [
  237. 'id' => 'string',
  238. ],
  239. 'argument' => [
  240. 'id' => 'string',
  241. ],
  242. 'sort' => [
  243. 'id' => 'standard',
  244. ],
  245. ];
  246. $data['file_usage']['type'] = [
  247. 'title' => $this->t('Entity type'),
  248. 'help' => $this->t('The type of entity that is related to the file.'),
  249. 'field' => [
  250. 'id' => 'standard',
  251. ],
  252. 'filter' => [
  253. 'id' => 'string',
  254. ],
  255. 'argument' => [
  256. 'id' => 'string',
  257. ],
  258. 'sort' => [
  259. 'id' => 'standard',
  260. ],
  261. ];
  262. $data['file_usage']['id'] = [
  263. 'title' => $this->t('Entity ID'),
  264. 'help' => $this->t('The ID of the entity that is related to the file.'),
  265. 'field' => [
  266. 'id' => 'numeric',
  267. ],
  268. 'argument' => [
  269. 'id' => 'numeric',
  270. ],
  271. 'filter' => [
  272. 'id' => 'numeric',
  273. ],
  274. 'sort' => [
  275. 'id' => 'standard',
  276. ],
  277. ];
  278. $data['file_usage']['count'] = [
  279. 'title' => $this->t('Use count'),
  280. 'help' => $this->t('The number of times the file is used by this entity.'),
  281. 'field' => [
  282. 'id' => 'numeric',
  283. ],
  284. 'filter' => [
  285. 'id' => 'numeric',
  286. ],
  287. 'sort' => [
  288. 'id' => 'standard',
  289. ],
  290. ];
  291. $data['file_usage']['entity_label'] = [
  292. 'title' => $this->t('Entity label'),
  293. 'help' => $this->t('The label of the entity that is related to the file.'),
  294. 'real field' => 'id',
  295. 'field' => [
  296. 'id' => 'entity_label',
  297. 'entity type field' => 'type',
  298. ],
  299. ];
  300. return $data;
  301. }
  302. }