RenderedEntity.php in Entity Browser 8.2
File
src/Plugin/EntityBrowser/FieldWidgetDisplay/RenderedEntity.php
View source
<?php
namespace Drupal\entity_browser\Plugin\EntityBrowser\FieldWidgetDisplay;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\entity_browser\FieldWidgetDisplayBase;
class RenderedEntity extends FieldWidgetDisplayBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $entityDisplayRepository;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->entityDisplayRepository = $entity_display_repository;
}
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 view(EntityInterface $entity) {
$build = $this->entityTypeManager
->getViewBuilder($this->configuration['entity_type'])
->view($entity, $this->configuration['view_mode']);
$build['#entity_browser_suppress_contextual'] = TRUE;
$build['#cache']['keys'][] = 'entity_browser';
return $build;
}
public function getViewModeLabel() {
if (!empty($this->configuration['entity_type']) && !empty($this->configuration['view_mode'])) {
$view_modes = $this->entityDisplayRepository
->getViewModeOptions($this->configuration['entity_type']);
return $view_modes[$this->configuration['view_mode']];
}
return $this
->t('Default');
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$options = [];
foreach ($this->entityDisplayRepository
->getViewModeOptions($this->configuration['entity_type']) as $id => $view_mode_label) {
$options[$id] = $view_mode_label;
}
return [
'view_mode' => [
'#type' => 'select',
'#title' => $this
->t('View mode'),
'#description' => $this
->t('Select view mode to be used when rendering entities.'),
'#default_value' => $this->configuration['view_mode'],
'#options' => $options,
],
];
}
public function defaultConfiguration() {
return [
'view_mode' => 'default',
] + parent::defaultConfiguration();
}
public function calculateDependencies() {
$dependencies = parent::calculateDependencies();
if ($view_mode = $this->entityTypeManager
->getStorage('entity_view_mode')
->load($this->configuration['entity_type'] . '.' . $this->configuration['view_mode'])) {
$dependencies[$view_mode
->getConfigDependencyKey()][] = $view_mode
->getConfigDependencyName();
}
return $dependencies;
}
}