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