View source
<?php
namespace Drupal\views\Plugin\views\field;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\views\Entity\Render\EntityTranslationRenderTrait;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityOperations extends FieldPluginBase {
use EntityTranslationRenderTrait;
use RedirectDestinationTrait;
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) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager;
$this->entityRepository = $entity_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('language_manager'), $container
->get('entity.repository'));
}
public function usesGroupBy() {
return FALSE;
}
public function defineOptions() {
$options = parent::defineOptions();
$options['destination'] = [
'default' => FALSE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['destination'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Include destination'),
'#description' => $this
->t('Enforce a <code>destination</code> parameter in the link to return the user to the original view upon completing the link action. Most operations include a destination by default and this setting is no longer needed.'),
'#default_value' => $this->options['destination'],
];
}
public function render(ResultRow $values) {
$entity = $this
->getEntityTranslation($this
->getEntity($values), $values);
$operations = $this->entityTypeManager
->getListBuilder($entity
->getEntityTypeId())
->getOperations($entity);
if ($this->options['destination']) {
foreach ($operations as &$operation) {
if (!isset($operation['query'])) {
$operation['query'] = [];
}
$operation['query'] += $this
->getDestinationArray();
}
}
$build = [
'#type' => 'operations',
'#links' => $operations,
];
return $build;
}
public function query() {
if ($this->languageManager
->isMultilingual()) {
$this
->getEntityTranslationRenderer()
->query($this->query, $this->relationship);
}
}
public function getEntityTypeId() {
return $this
->getEntityType();
}
protected function getEntityTypeManager() {
return $this->entityTypeManager;
}
protected function getEntityRepository() {
return $this->entityRepository;
}
protected function getLanguageManager() {
return $this->languageManager;
}
protected function getView() {
return $this->view;
}
public function clickSortable() {
return FALSE;
}
}