3 namespace Drupal\ctools\Plugin\Block;
5 use Drupal\Core\Block\BlockBase;
6 use Drupal\Core\Cache\CacheableMetadata;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\Core\Plugin\ContextAwarePluginInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides a block to view a specific entity.
18 * deriver = "Drupal\ctools\Plugin\Deriver\EntityViewDeriver",
21 class EntityView extends BlockBase implements ContextAwarePluginInterface, ContainerFactoryPluginInterface {
26 * @var \Drupal\Core\Entity\EntityManagerInterface
28 protected $entityManager;
31 * Constructs a new EntityView.
33 * @param array $configuration
34 * A configuration array containing information about the plugin instance.
35 * @param string $plugin_id
36 * The plugin ID for the plugin instance.
37 * @param mixed $plugin_definition
38 * The plugin implementation definition.
39 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
42 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
43 parent::__construct($configuration, $plugin_id, $plugin_definition);
45 $this->entityManager = $entity_manager;
51 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
56 $container->get('entity.manager')
63 public function defaultConfiguration() {
65 'view_mode' => 'default',
72 public function blockForm($form, FormStateInterface $form_state) {
73 $form['view_mode'] = [
75 '#options' => $this->entityManager->getViewModeOptions($this->getDerivativeId()),
76 '#title' => $this->t('View mode'),
77 '#default_value' => $this->configuration['view_mode'],
85 public function blockSubmit($form, FormStateInterface $form_state) {
86 $this->configuration['view_mode'] = $form_state->getValue('view_mode');
92 public function build() {
93 /** @var $entity \Drupal\Core\Entity\EntityInterface */
94 $entity = $this->getContextValue('entity');
96 $view_builder = $this->entityManager->getViewBuilder($entity->getEntityTypeId());
97 $build = $view_builder->view($entity, $this->configuration['view_mode']);
99 CacheableMetadata::createFromObject($this->getContext('entity'))