class EntityTestBlock

Same name and namespace in other branches
  1. 11.x core/modules/system/tests/modules/entity_test/src/Plugin/Block/EntityTestBlock.php \Drupal\entity_test\Plugin\Block\EntityTestBlock

Provides a block that renders an entity with parallel placeholder rendering.

Attributes

#[Block(id: 'entity_test_block', admin_label: new TranslatableMarkup('Entity test block'))]

Hierarchy

Expanded class hierarchy of EntityTestBlock

File

core/modules/system/tests/modules/entity_test/src/Plugin/Block/EntityTestBlock.php, line 16

Namespace

Drupal\entity_test\Plugin\Block
View source
class EntityTestBlock extends BlockBase implements ContainerFactoryPluginInterface {
  public function __construct(array $configuration, $plugin_id, $plugin_definition, protected EntityTypeManagerInterface $entityTypeManager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }
  
  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() : array {
    return [
      'entity_type_id' => 'entity_test',
      'entity_id' => NULL,
    ] + parent::defaultConfiguration();
  }
  
  /**
   * {@inheritdoc}
   */
  public function createPlaceholder() : bool {
    // Render as a placeholder so this block is rendered in a Fiber, enabling
    // tests to verify concurrent entity rendering behavior.
    return TRUE;
  }
  
  /**
   * {@inheritdoc}
   */
  public function build() : array {
    $entity_type_id = $this->configuration['entity_type_id'];
    $entity = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->load($this->configuration['entity_id']);
    if ($entity) {
      return $this->entityTypeManager
        ->getViewBuilder($entity_type_id)
        ->view($entity);
    }
    return [];
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.