Commit a57094c8 authored by webchick's avatar webchick

Issue #2120839 by damiankloip, slashrm, Dave Reid: File usage view causes...

Issue #2120839 by damiankloip, slashrm, Dave Reid: File usage view causes fatal error for a file with 0 usage.
parent 063b5cd3
......@@ -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
......
......@@ -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',
......
......@@ -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;
}
}
......@@ -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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment