diff --git a/core/modules/file/config/views.view.files.yml b/core/modules/file/config/views.view.files.yml index 2d633c2813ac35f956299da682f95e71c5407d4c..c11c4728459cb7c4e46523d3904d314258a38969 100644 --- a/core/modules/file/config/views.view.files.yml +++ b/core/modules/file/config/views.view.files.yml @@ -396,7 +396,7 @@ display: id: count table: file_usage field: count - relationship: none + relationship: fid group_type: sum admin_label: '' label: 'Used in' @@ -578,7 +578,15 @@ display: empty: true content: 'No files available.' plugin_id: text_custom - relationships: { } + relationships: + fid: + id: fid + table: file_managed + field: fid + relationship: none + group_type: group + admin_label: 'File usage' + required: true arguments: { } group_by: '1' show_admin_links: '1' @@ -600,6 +608,16 @@ display: defaults: pager: true pager_options: true + relationships: false + relationships: + fid: + id: fid + table: file_managed + field: fid + relationship: none + group_type: group + admin_label: 'File usage' + required: false page_2: display_plugin: page id: page_2 @@ -621,6 +639,7 @@ display: arguments: false style: false row: false + relationships: false pager: type: mini options: @@ -947,6 +966,15 @@ display: row: type: fields options: { } + relationships: + fid: + id: fid + table: file_managed + field: fid + relationship: none + group_type: group + admin_label: 'File usage' + required: true label: Files module: file id: files diff --git a/core/modules/file/file.views.inc b/core/modules/file/file.views.inc index 7adc1943802befcc77ecfda2731e160c4898a609..70131f64999053c9a026e1485120f98e5015a3eb 100644 --- a/core/modules/file/file.views.inc +++ b/core/modules/file/file.views.inc @@ -48,6 +48,15 @@ function file_views_data() { 'sort' => array( 'id' => 'standard', ), + 'relationship' => array( + 'title' => t('File usage'), + 'help' => t('Relate file entities to their usage.'), + 'id' => 'standard', + 'base' => 'file_usage', + 'base field' => 'fid', + 'field' => 'fid', + 'label' => t('File usage'), + ), ); // filename @@ -180,11 +189,6 @@ function file_views_data() { // that we can create relationships from files to entities, and then on each core entity type base // table so that we can provide general relationships between entities and files. $data['file_usage']['table']['join'] = array( - // Link ourself to the {file_managed} table so we can provide file->entity relationships. - 'file_managed' => array( - 'field' => 'fid', - 'left_field' => 'fid', - ), // Link ourself to the {node} table so we can provide node->file relationships. 'node' => array( 'field' => 'id', diff --git a/core/modules/file/lib/Drupal/file/Tests/FileListingTest.php b/core/modules/file/lib/Drupal/file/Tests/FileListingTest.php index 3126560cfcd77a26c2d16433b5159c82d25fad0c..598a52fa7e757635264c841786e8e4b654920100 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileListingTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileListingTest.php @@ -72,6 +72,19 @@ function testFileListingPages() { $nodes[] = $this->drupalCreateNode(array('type' => 'article')); } + $this->drupalGet('admin/content/files'); + $this->assertResponse(200); + $this->assertText('No files available.'); + $this->drupalGet('admin/content/files'); + $this->assertResponse(200); + + // Create a file with no usage. + $file = $this->createFile(); + + $this->drupalGet('admin/content/files/usage/' . $file->id()); + $this->assertResponse(200); + $this->assertTitle(t('File usage information for @file | Drupal', array('@file' => $file->getFilename()))); + foreach ($nodes as &$node) { $this->drupalGet('node/' . $node->id() . '/edit'); $file = $this->getTestFile('image'); @@ -84,7 +97,6 @@ function testFileListingPages() { } $this->drupalGet('admin/content/files'); - $this->assertResponse(200); foreach ($nodes as $node) { $file = entity_load('file', $node->file->target_id); @@ -131,4 +143,29 @@ function testFileListingPages() { $this->assertLinkByHref('node/' . $node->id(), 0, 'Link to registering entity found on usage page.'); } } + + /** + * Creates and saves a test file. + * + * @return \Drupal\Core\Entity\EntityInterface + * A file entity. + */ + protected function createFile() { + // Create a new file entity. + $file = entity_create('file', array( + 'uid' => 1, + 'filename' => 'druplicon.txt', + 'uri' => 'public://druplicon.txt', + 'filemime' => 'text/plain', + 'timestamp' => 1, + 'status' => FILE_STATUS_PERMANENT, + )); + file_put_contents($file->getFileUri(), 'hello world'); + + // Save it, inserting a new record. + $file->save(); + + return $file; + } + } diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/field/EntityLabel.php b/core/modules/views/lib/Drupal/views/Plugin/views/field/EntityLabel.php index 97f25f8f0f4a69b9eee56e083b55c008f6c45233..bc37f335e54d858956f6d2592a01e7d0dbb01480 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/field/EntityLabel.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/field/EntityLabel.php @@ -100,12 +100,15 @@ public function buildOptionsForm(&$form, &$form_state) { * {@inheritdoc} */ public function render(ResultRow $values) { - $entity = $this->loadedReferencers[$this->getValue($values, $this->definition['entity type field'])][$this->getValue($values)]; + $type = $this->getValue($values, $this->definition['entity type field']); + $value = $this->getValue($values); - if (empty($entity)) { - return NULL; + if (empty($this->loadedReferencers[$type][$value])) { + return; } + $entity = $this->loadedReferencers[$type][$value]; + if (!empty($this->options['link_to_entity'])) { $uri = $entity->uri(); $this->options['alter']['make_link'] = TRUE; @@ -123,7 +126,9 @@ public function preRender(&$values) { $entity_ids_per_type = array(); foreach ($values as $value) { - $entity_ids_per_type[$this->getValue($value, 'type')][] = $this->getValue($value); + if ($type = $this->getValue($value, 'type')) { + $entity_ids_per_type[$type][] = $this->getValue($value); + } } foreach ($entity_ids_per_type as $type => $ids) {