Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / views / src / Plugin / views / field / EntityOperations.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\field;
4
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;
12
13 /**
14  * Renders all operations links for an entity.
15  *
16  * @ingroup views_field_handlers
17  *
18  * @ViewsField("entity_operations")
19  */
20 class EntityOperations extends FieldPluginBase {
21
22   use EntityTranslationRenderTrait;
23   use RedirectDestinationTrait;
24
25   /**
26    * The entity manager.
27    *
28    * @var \Drupal\Core\Entity\EntityManagerInterface
29    */
30   protected $entityManager;
31
32   /**
33    * The language manager.
34    *
35    * @var \Drupal\Core\Language\LanguageManagerInterface
36    */
37   protected $languageManager;
38
39   /**
40    * Constructor.
41    *
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
49    *   The entity manager.
50    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
51    *   The language manager.
52    */
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);
55
56     $this->entityManager = $entity_manager;
57     $this->languageManager = $language_manager;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
64     return new static(
65       $configuration,
66       $plugin_id,
67       $plugin_definition,
68       $container->get('entity.manager'),
69       $container->get('language_manager')
70     );
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function usesGroupBy() {
77     return FALSE;
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function defineOptions() {
84     $options = parent::defineOptions();
85
86     $options['destination'] = [
87       'default' => FALSE,
88     ];
89
90     return $options;
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
97     parent::buildOptionsForm($form, $form_state);
98
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'],
104     ];
105   }
106
107   /**
108    * {@inheritdoc}
109    */
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'] = [];
117         }
118         $operation['query'] += $this->getDestinationArray();
119       }
120     }
121     $build = [
122       '#type' => 'operations',
123       '#links' => $operations,
124     ];
125
126     return $build;
127   }
128
129   /**
130    * {@inheritdoc}
131    */
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);
138     }
139   }
140
141   /**
142    * {@inheritdoc}
143    */
144   public function getEntityTypeId() {
145     return $this->getEntityType();
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   protected function getEntityManager() {
152     return $this->entityManager;
153   }
154
155   /**
156    * {@inheritdoc}
157    */
158   protected function getLanguageManager() {
159     return $this->languageManager;
160   }
161   /**
162    * {@inheritdoc}
163    */
164   protected function getView() {
165     return $this->view;
166   }
167
168   /**
169    * {@inheritdoc}
170    */
171   public function clickSortable() {
172     return FALSE;
173   }
174
175 }