5 use Drupal\views\EntityViewsData;
8 * Provides views data for the file entity type.
10 class FileViewsData extends EntityViewsData {
15 public function getViewsData() {
16 $data = parent::getViewsData();
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';
23 $data['file_managed']['fid']['argument'] = [
25 // The field to display in the summary.
26 'name field' => 'filename',
29 $data['file_managed']['fid']['relationship'] = [
30 'title' => $this->t('File usage'),
31 'help' => $this->t('Relate file entities to their usage.'),
33 'base' => 'file_usage',
34 'base field' => 'fid',
36 'label' => $this->t('File usage'),
39 $data['file_managed']['uri']['field']['default_formatter'] = 'file_uri';
41 $data['file_managed']['filemime']['field']['default_formatter'] = 'file_filemime';
43 $data['file_managed']['extension'] = [
44 'title' => $this->t('Extension'),
45 'help' => $this->t('The extension of the file.'),
46 'real field' => 'filename',
48 'entity_type' => 'file',
49 'field_name' => 'filename',
50 'default_formatter' => 'file_extension',
52 'click sortable' => FALSE,
56 $data['file_managed']['filesize']['field']['default_formatter'] = 'file_size';
58 $data['file_managed']['status']['field']['default_formatter_settings'] = [
60 'format_custom_false' => $this->t('Temporary'),
61 'format_custom_true' => $this->t('Permanent'),
63 $data['file_managed']['status']['filter']['id'] = 'file_status';
65 $data['file_managed']['uid']['relationship']['title'] = $this->t('User who uploaded');
66 $data['file_managed']['uid']['relationship']['label'] = $this->t('User who uploaded');
68 $data['file_usage']['table']['group'] = $this->t('File Usage');
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'] = [
77 'left_field' => 'fid',
79 // Link ourselves to the {node_field_data} table
80 // so we can provide node->file relationships.
81 'node_field_data' => [
83 'left_field' => 'nid',
84 'extra' => [['field' => 'type', 'value' => 'node']],
86 // Link ourselves to the {users_field_data} table
87 // so we can provide user->file relationships.
88 'users_field_data' => [
90 'left_field' => 'uid',
91 'extra' => [['field' => 'type', 'value' => 'user']],
93 // Link ourselves to the {comment_field_data} table
94 // so we can provide comment->file relationships.
97 'left_field' => 'cid',
98 'extra' => [['field' => 'type', 'value' => 'comment']],
100 // Link ourselves to the {taxonomy_term_field_data} table
101 // so we can provide taxonomy_term->file relationships.
102 'taxonomy_term_data' => [
104 'left_field' => 'tid',
105 'extra' => [['field' => 'type', 'value' => 'taxonomy_term']],
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.
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',
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']],
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',
140 'title' => $this->t('File'),
141 'label' => $this->t('File'),
142 'base' => 'file_managed',
143 'base field' => 'fid',
144 'relationship field' => 'fid',
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',
157 'title' => $this->t('User'),
158 'label' => $this->t('User'),
160 'base field' => 'uid',
161 'relationship field' => 'id',
162 'extra' => [['table' => 'file_usage', 'field' => 'type', 'operator' => '=', 'value' => 'user']],
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',
173 'title' => $this->t('File'),
174 'label' => $this->t('File'),
175 'base' => 'file_managed',
176 'base field' => 'fid',
177 'relationship field' => 'fid',
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',
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']],
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',
206 'title' => $this->t('File'),
207 'label' => $this->t('File'),
208 'base' => 'file_managed',
209 'base field' => 'fid',
210 'relationship field' => 'fid',
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',
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']],
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',
239 'title' => $this->t('File'),
240 'label' => $this->t('File'),
241 'base' => 'file_managed',
242 'base field' => 'fid',
243 'relationship field' => 'fid',
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
250 $data['file_usage']['module'] = [
251 'title' => $this->t('Module'),
252 'help' => $this->t('The module managing this file relationship.'),
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.'),
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.'),
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.'),
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',
316 'id' => 'entity_label',
317 'entity type field' => 'type',