summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2016-06-24 13:35:09 +0200
committerAlex Pott2016-06-24 13:35:09 +0200
commit705b75ca9322577aa236b92ae488a7b73792b6a6 (patch)
tree79522e5e576de525d646b839b3292ea5ad616fe7
parent3e07700c8be2b3a62a9b9137d3fa4281a103b77b (diff)
Issue #2676552 by justAChris, mbovan, chr.fritsch, gaborpeter, mariancalinro, alexpott, Berdir: File usage view breaks if an entity uses a file that has no canonical link template
-rw-r--r--core/modules/file/src/Tests/FileListingTest.php52
-rw-r--r--core/modules/views/src/Plugin/views/field/EntityLabel.php14
2 files changed, 63 insertions, 3 deletions
diff --git a/core/modules/file/src/Tests/FileListingTest.php b/core/modules/file/src/Tests/FileListingTest.php
index 4fd233d..708f138 100644
--- a/core/modules/file/src/Tests/FileListingTest.php
+++ b/core/modules/file/src/Tests/FileListingTest.php
@@ -4,6 +4,7 @@ namespace Drupal\file\Tests;
use Drupal\node\Entity\Node;
use Drupal\file\Entity\File;
+use Drupal\entity_test\Entity\EntityTestConstraints;
/**
* Tests file listing page functionality.
@@ -17,7 +18,7 @@ class FileListingTest extends FileFieldTestBase {
*
* @var array
*/
- public static $modules = array('views', 'file', 'image');
+ public static $modules = array('views', 'file', 'image', 'entity_test');
/**
* An authenticated user.
@@ -145,6 +146,55 @@ class FileListingTest extends FileFieldTestBase {
}
/**
+ * Tests file listing usage page for entities with no canonical link template.
+ */
+ function testFileListingUsageNoLink() {
+ // Login with user with right permissions and test listing.
+ $this->drupalLogin($this->adminUser);
+
+ // Create a bundle and attach a File field to the bundle.
+ $bundle = $this->randomMachineName();
+ entity_test_create_bundle($bundle, NULL, 'entity_test_constraints');
+ $this->createFileField('field_test_file', 'entity_test_constraints', $bundle, array(), array('file_extensions' => 'txt png'));
+
+ // Create file to attach to entity.
+ $file = File::create([
+ 'filename' => 'druplicon.txt',
+ 'uri' => 'public://druplicon.txt',
+ 'filemime' => 'text/plain',
+ ]);
+ $file->setPermanent();
+ file_put_contents($file->getFileUri(), 'hello world');
+ $file->save();
+
+ // Create entity and attach the created file.
+ $entity_name = $this->randomMachineName();
+ $entity = EntityTestConstraints::create(array(
+ 'uid' => 1,
+ 'name' => $entity_name,
+ 'type' => $bundle,
+ 'field_test_file' => array(
+ 'target_id' => $file->id(),
+ ),
+ ));
+ $entity->save();
+
+ // Create node entity and attach the created file.
+ $node = $this->drupalCreateNode(array('type' => 'article', 'file' => $file));
+ $node->save();
+
+ // Load the file usage page for the created and attached file.
+ $this->drupalGet('admin/content/files/usage/' . $file->id());
+
+ $this->assertResponse(200);
+ // Entity name should be displayed, but not linked if Entity::toUrl
+ // throws an exception
+ $this->assertText($entity_name, 'Entity name is added to file usage listing.');
+ $this->assertNoLink($entity_name, 'Linked entity name not added to file usage listing.');
+ $this->assertLink($node->getTitle());
+ }
+
+ /**
* Creates and saves a test file.
*
* @return \Drupal\Core\Entity\EntityInterface
diff --git a/core/modules/views/src/Plugin/views/field/EntityLabel.php b/core/modules/views/src/Plugin/views/field/EntityLabel.php
index 3691d06..967fd52 100644
--- a/core/modules/views/src/Plugin/views/field/EntityLabel.php
+++ b/core/modules/views/src/Plugin/views/field/EntityLabel.php
@@ -2,7 +2,9 @@
namespace Drupal\views\Plugin\views\field;
+use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityManagerInterface;
+use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
@@ -105,8 +107,16 @@ class EntityLabel extends FieldPluginBase {
$entity = $this->loadedReferencers[$type][$value];
if (!empty($this->options['link_to_entity'])) {
- $this->options['alter']['make_link'] = TRUE;
- $this->options['alter']['url'] = $entity->urlInfo();
+ try {
+ $this->options['alter']['url'] = $entity->toUrl();
+ $this->options['alter']['make_link'] = TRUE;
+ }
+ catch (UndefinedLinkTemplateException $e) {
+ $this->options['alter']['make_link'] = FALSE;
+ }
+ catch (EntityMalformedException $e) {
+ $this->options['alter']['make_link'] = FALSE;
+ }
}
return $this->sanitizeValue($entity->label());