summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2013-11-28 08:02:41 (GMT)
committerwebchick2013-11-28 08:02:41 (GMT)
commita57094c870cba6170ea70e92c806ad75247c75c4 (patch)
tree6954c7ab332af7cc50ff93248c16b1395bac6e5c
parent063b5cd333443545b4fbdff3422315a762c875cc (diff)
Issue #2120839 by damiankloip, slashrm, Dave Reid: File usage view causes fatal error for a file with 0 usage.
-rw-r--r--core/modules/file/config/views.view.files.yml32
-rw-r--r--core/modules/file/file.views.inc14
-rw-r--r--core/modules/file/lib/Drupal/file/Tests/FileListingTest.php39
-rw-r--r--core/modules/views/lib/Drupal/views/Plugin/views/field/EntityLabel.php13
4 files changed, 86 insertions, 12 deletions
diff --git a/core/modules/file/config/views.view.files.yml b/core/modules/file/config/views.view.files.yml
index 2d633c2..c11c472 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 7adc194..70131f6 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 3126560..598a52f 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 @@ class FileListingTest extends FileFieldTestBase {
$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 @@ class FileListingTest extends FileFieldTestBase {
}
$this->drupalGet('admin/content/files');
- $this->assertResponse(200);
foreach ($nodes as $node) {
$file = entity_load('file', $node->file->target_id);
@@ -131,4 +143,29 @@ class FileListingTest extends FileFieldTestBase {
$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 97f25f8..bc37f33 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 @@ class EntityLabel extends FieldPluginBase {
* {@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 @@ class EntityLabel extends FieldPluginBase {
$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) {