isDisplayed example

use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;

/** * Base class for file formatters. */
abstract class FileFormatterBase extends EntityReferenceFormatterBase {

  /** * {@inheritdoc} */
  protected function needsEntityLoad(EntityReferenceItem $item) {
    return parent::needsEntityLoad($item) && $item->isDisplayed();
  }

  /** * {@inheritdoc} */
  protected function checkAccess(EntityInterface $entity) {
    // Only check access if the current file access control handler explicitly     // opts in by implementing FileAccessFormatterControlHandlerInterface.     $access_handler_class = $entity->getEntityType()->getHandlerClass('access');
    if (is_subclass_of($access_handler_class, '\Drupal\file\FileAccessFormatterControlHandlerInterface')) {
      return $entity->access('view', NULL, TRUE);
    }
public function testUpload() {
    $this->container->get('entity_type.manager')
      ->getStorage('node')
      ->resetCache([1, 2, 12]);

    $nodes = Node::loadMultiple([1, 2, 12]);
    $node = $nodes[1];
    $this->assertEquals('en', $node->langcode->value);
    $this->assertCount(1, $node->upload);
    $this->assertSame('1', $node->upload[0]->target_id);
    $this->assertSame('file 1-1-1', $node->upload[0]->description);
    $this->assertFalse($node->upload[0]->isDisplayed());

    $node = $nodes[2];
    $this->assertEquals('en', $node->langcode->value);
    $this->assertCount(2, $node->upload);
    $this->assertSame('3', $node->upload[0]->target_id);
    $this->assertSame('file 2-3-3', $node->upload[0]->description);
    $this->assertFalse($node->upload[0]->isDisplayed());
    $this->assertSame('2', $node->upload[1]->target_id);
    $this->assertTrue($node->upload[1]->isDisplayed());
    $this->assertSame('file 2-3-2', $node->upload[1]->description);

    
Home | Imprint | This part of the site doesn't use cookies.