EntityOperations.php in Zircon Profile 8
File
core/modules/views/src/Plugin/views/field/EntityOperations.php
View source
<?php
namespace Drupal\views\Plugin\views\field;
use Drupal\Core\Entity\EntityManagerInterface;
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 $entityManager;
protected $languageManager;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityManager = $entity_manager;
$this->languageManager = $language_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity.manager'), $container
->get('language_manager'));
}
public function usesGroupBy() {
return FALSE;
}
public function defineOptions() {
$options = parent::defineOptions();
$options['destination'] = array(
'default' => TRUE,
);
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['destination'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Include destination'),
'#description' => $this
->t('Include a <code>destination</code> parameter in the link to return the user to the original view upon completing the link action.'),
'#default_value' => $this->options['destination'],
);
}
public function render(ResultRow $values) {
$entity = $this
->getEntityTranslation($this
->getEntity($values), $values);
$operations = $this->entityManager
->getListBuilder($entity
->getEntityTypeId())
->getOperations($entity);
if ($this->options['destination']) {
foreach ($operations as &$operation) {
if (!isset($operation['query'])) {
$operation['query'] = array();
}
$operation['query'] += $this
->getDestinationArray();
}
}
$build = array(
'#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 getEntityManager() {
return $this->entityManager;
}
protected function getLanguageManager() {
return $this->languageManager;
}
protected function getView() {
return $this->view;
}
}