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
							parent
							
								
									3e07700c8b
								
							
						
					
					
						commit
						705b75ca93
					
				| 
						 | 
				
			
			@ -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.
 | 
			
		||||
| 
						 | 
				
			
			@ -144,6 +145,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.
 | 
			
		||||
   *
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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());
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue