3 namespace Drupal\views\Plugin\views\field;
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Language\LanguageManagerInterface;
8 use Drupal\Core\Routing\RedirectDestinationTrait;
9 use Drupal\views\Entity\Render\EntityTranslationRenderTrait;
10 use Drupal\views\ResultRow;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Renders all operations links for an entity.
16 * @ingroup views_field_handlers
18 * @ViewsField("entity_operations")
20 class EntityOperations extends FieldPluginBase {
22 use EntityTranslationRenderTrait;
23 use RedirectDestinationTrait;
28 * @var \Drupal\Core\Entity\EntityManagerInterface
30 protected $entityManager;
33 * The language manager.
35 * @var \Drupal\Core\Language\LanguageManagerInterface
37 protected $languageManager;
42 * @param array $configuration
43 * A configuration array containing information about the plugin instance.
44 * @param string $plugin_id
45 * The plugin_id for the plugin instance.
46 * @param array $plugin_definition
47 * The plugin implementation definition.
48 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
50 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
51 * The language manager.
53 public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) {
54 parent::__construct($configuration, $plugin_id, $plugin_definition);
56 $this->entityManager = $entity_manager;
57 $this->languageManager = $language_manager;
63 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
68 $container->get('entity.manager'),
69 $container->get('language_manager')
76 public function usesGroupBy() {
83 public function defineOptions() {
84 $options = parent::defineOptions();
86 $options['destination'] = [
96 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
97 parent::buildOptionsForm($form, $form_state);
99 $form['destination'] = [
100 '#type' => 'checkbox',
101 '#title' => $this->t('Include destination'),
102 '#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.'),
103 '#default_value' => $this->options['destination'],
110 public function render(ResultRow $values) {
111 $entity = $this->getEntityTranslation($this->getEntity($values), $values);
112 $operations = $this->entityManager->getListBuilder($entity->getEntityTypeId())->getOperations($entity);
113 if ($this->options['destination']) {
114 foreach ($operations as &$operation) {
115 if (!isset($operation['query'])) {
116 $operation['query'] = [];
118 $operation['query'] += $this->getDestinationArray();
122 '#type' => 'operations',
123 '#links' => $operations,
132 public function query() {
133 // We purposefully do not call parent::query() because we do not want the
134 // default query behavior for Views fields. Instead, let the entity
135 // translation renderer provide the correct query behavior.
136 if ($this->languageManager->isMultilingual()) {
137 $this->getEntityTranslationRenderer()->query($this->query, $this->relationship);
144 public function getEntityTypeId() {
145 return $this->getEntityType();
151 protected function getEntityManager() {
152 return $this->entityManager;
158 protected function getLanguageManager() {
159 return $this->languageManager;
164 protected function getView() {
171 public function clickSortable() {