View source
<?php
namespace Drupal\views\Plugin\views\row;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\views\Entity\Render\EntityTranslationRenderTrait;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityRow extends RowPluginBase {
use EntityTranslationRenderTrait;
use DeprecatedServicePropertyTrait;
protected $deprecatedProperties = [
'entityManager' => 'entity.manager',
];
public $base_table;
public $base_field;
protected $entityTypeId;
protected $entityType;
protected $entityTypeManager;
protected $entityRepository;
protected $entityDisplayRepository;
protected $languageManager;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, EntityRepositoryInterface $entity_repository = NULL, EntityDisplayRepositoryInterface $entity_display_repository = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager;
if (!$entity_repository) {
@trigger_error('Calling EntityRow::__construct() with the $entity_repository argument is supported in drupal:8.7.0 and will be required before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_repository = \Drupal::service('entity.repository');
}
$this->entityRepository = $entity_repository;
if (!$entity_display_repository) {
@trigger_error('Calling EntityRow::__construct() with the $entity_display_repository argument is supported in drupal:8.7.0 and will be required before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_display_repository = \Drupal::service('entity_display.repository');
}
$this->entityDisplayRepository = $entity_display_repository;
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->entityTypeId = $this->definition['entity_type'];
$this->entityType = $this->entityTypeManager
->getDefinition($this->entityTypeId);
$this->base_table = $this->entityType
->getDataTable() ?: $this->entityType
->getBaseTable();
$this->base_field = $this->entityType
->getKey('id');
}
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('language_manager'), $container
->get('entity.repository'), $container
->get('entity_display.repository'));
}
public function getEntityTypeId() {
return $this->entityType
->id();
}
protected function getEntityManager() {
return $this->entityManager;
}
protected function getEntityTypeManager() {
return $this->entityTypeManager;
}
protected function getEntityRepository() {
return $this->entityRepository;
}
protected function getLanguageManager() {
return $this->languageManager;
}
protected function getView() {
return $this->view;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['view_mode'] = [
'default' => 'default',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['view_mode'] = [
'#type' => 'select',
'#options' => $this->entityDisplayRepository
->getViewModeOptions($this->entityTypeId),
'#title' => $this
->t('View mode'),
'#default_value' => $this->options['view_mode'],
];
}
public function summaryTitle() {
$options = $this->entityDisplayRepository
->getViewModeOptions($this->entityTypeId);
if (isset($options[$this->options['view_mode']])) {
return $options[$this->options['view_mode']];
}
else {
return $this
->t('No view mode selected');
}
}
public function query() {
parent::query();
$this
->getEntityTranslationRenderer()
->query($this->view
->getQuery());
}
public function preRender($result) {
parent::preRender($result);
if ($result) {
$this
->getEntityTranslationRenderer()
->preRender($result);
}
}
public function render($row) {
return $this
->getEntityTranslationRenderer()
->render($row);
}
public function calculateDependencies() {
$dependencies = parent::calculateDependencies();
$view_mode = $this->entityTypeManager
->getStorage('entity_view_mode')
->load($this->entityTypeId . '.' . $this->options['view_mode']);
if ($view_mode) {
$dependencies[$view_mode
->getConfigDependencyKey()][] = $view_mode
->getConfigDependencyName();
}
return $dependencies;
}
}