View source
<?php
namespace Drupal\entity_block\Plugin\Block;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $entityTypeName;
public $entityTypeManager;
protected $entityStorage;
protected $entityViewBuilder;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, EntityDisplayRepositoryInterface $entityDisplayRepository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeName = $this
->getDerivativeId();
$this->entityTypeManager = $entityTypeManager;
$this->entityStorage = $entityTypeManager
->getStorage($this->entityTypeName);
if ($entityTypeManager
->hasHandler($this->entityTypeName, 'fallback_view_builder')) {
$this->entityViewBuilder = $entityTypeManager
->getHandler($this->entityTypeName, 'fallback_view_builder');
}
else {
$this->entityViewBuilder = $entityTypeManager
->getHandler($this->entityTypeName, 'view_builder');
}
$this->view_mode_options = $entityDisplayRepository
->getViewModeOptions($this->entityTypeName);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_display.repository'));
}
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this->configuration;
$form['entity'] = [
'#type' => 'entity_autocomplete',
'#title' => $this
->t('Entity'),
'#target_type' => $this->entityTypeName,
'#required' => TRUE,
];
if (isset($config['entity'])) {
if ($entity = $this->entityStorage
->load($config['entity'])) {
$form['entity']['#default_value'] = $entity;
}
}
$view_mode = isset($config['view_mode']) ? $config['view_mode'] : NULL;
$form['view_mode'] = [
'#type' => 'select',
'#title' => $this
->t('View mode'),
'#options' => $this->view_mode_options,
'#default_value' => $view_mode,
];
return $form;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['admin_label']['#access'] = FALSE;
$form['label']['#access'] = FALSE;
$form['label_display']['#access'] = FALSE;
$form['label_display']['#value'] = FALSE;
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
parent::blockSubmit($form, $form_state);
$this->configuration['entity'] = $form_state
->getValue('entity');
$this->configuration['view_mode'] = $form_state
->getValue('view_mode');
if ($entity = $this->entityStorage
->load($this->configuration['entity'])) {
$plugin_definition = $this
->getPluginDefinition();
$admin_label = $plugin_definition['admin_label'];
$this->configuration['label'] = new FormattableMarkup('@entity_label (@admin_label)', [
'@entity_label' => $entity
->label(),
'@admin_label' => $admin_label,
]);
}
}
public function build() {
if ($entity_id = $this->configuration['entity']) {
if (($entity = $this->entityStorage
->load($entity_id)) && $entity
->access('view')) {
$render_controller = \Drupal::entityTypeManager()
->getViewBuilder($entity
->getEntityTypeId());
$view_mode = isset($this->configuration['view_mode']) ? $this->configuration['view_mode'] : 'default';
return $render_controller
->view($entity, $view_mode);
}
}
return [];
}
}