Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
namespace Drupal\eva\Plugin\views\display;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
/**
* The plugin that handles an EVA display in views.
*
*
* @ingroup views_display_plugins
*
* @ViewsDisplay(
* id = "entity_view",
* title = @Translation("EVA"),
* admin = @Translation("EVA"),
* help = @Translation("Attach a view to an entity"),
* theme = "eva_display_entity_view",
* uses_menu_links = FALSE,
* uses_hook_entity_view = TRUE,
* )
*/
class Eva extends DisplayPluginBase {
/**
* Overrides \Drupal\views\Plugin\views\display\PathPluginBase::defineOptions().
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['entity_type']['default'] = '';
$options['bundles']['default'] = array();
$options['argument_mode']['default'] = 'id';
$options['default_argument']['default'] = '';
$options['title']['default'] = '';
$options['defaults']['default']['title'] = FALSE;
return $options;
}
/**
* Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::optionsSummary().
*/
public function optionsSummary(&$categories, &$options) {
parent::optionsSummary($categories, $options);
$categories['entity_view'] = array(
'title' => $this->t('Entity content settings'),
'column' => 'second',
'build' => array(
'#weight' => -10,
),
);
if ($entity_type = $this->getOption('entity_type')) {
$entity_info = \Drupal::entityManager()->getDefinition($entity_type);
$type_name = $entity_info->get('label');
$bundle_names = array();
$bundle_info = \Drupal::entityManager()->getBundleInfo($entity_type);
foreach ($this->getOption('bundles') as $bundle) {
$bundle_names[] = $bundle_info[$bundle]['label'];
}
}
$options['entity_type'] = array(
'category' => 'entity_view',
'title' => $this->t('Entity type'),
'value' => empty($type_name) ? $this->t('None') : $type_name,
);
$options['bundles'] = array(
'category' => 'entity_view',
'title' => $this->t('Bundles'),
'value' => empty($bundle_names) ? $this->t('All') : implode(', ', $bundle_names),
);
$argument_mode = $this->getOption('argument_mode');
$options['arguments'] = array(
'category' => 'entity_view',
'title' => $this->t('Arguments'),
'value' => empty($argument_mode) ? $this->t('None') : SafeMarkup::checkPlain($argument_mode),
);
$options['show_title'] = array(
'category' => 'entity_view',
'title' => $this->t('Show title'),
'value' => $this->getOption('show_title') ? $this->t('Yes') : $this->t('No'),
);
}
/**
* Overrides \Drupal\views\Plugin\views\display\callbackPluginBase::buildOptionsForm().
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$entity_info = \Drupal::entityManager()->getDefinitions();
$entity_type = $this->getOption('entity_type');
switch ($form_state->get('section')) {
case 'entity_type':
foreach ($entity_info as $type => $info) {
// is this a content/front-facing entity?
if ($info instanceof \Drupal\Core\Entity\ContentEntityType) {
$entity_names[$type] = $info->get('label');
}
}
$form['#title'] .= $this->t('Entity type');
$form['entity_type'] = array(
'#type' => 'radios',
'#required' => TRUE,
'#validated' => TRUE,
'#title' => $this->t('Attach this display to the following entity type'),
'#options' => $entity_names,
'#default_value' => $this->getOption('entity_type'),
);
break;
case 'bundles':
$options = array();
foreach (\Drupal::entityManager()->getBundleInfo($entity_type) as $bundle => $info) {
$options[$bundle] = $info['label'];
}
$form['#title'] .= $this->t('Bundles');
$form['bundles'] = array(
'#type' => 'checkboxes',
'#title' => $this->t('Attach this display to the following bundles. If no bundles are selected, the display will be attached to all.'),
'#options' => $options,
'#default_value' => $this->getOption('bundles'),
);
break;
case 'arguments':
$form['#title'] .= $this->t('Arguments');
$default = $this->getOption('argument_mode');
$options = array(
'None' => $this->t("No special handling"),
'id' => $this->t("Use the ID of the entity the view is attached to"),
'token' => $this->t("Use tokens from the entity the view is attached to"),
);
$form['argument_mode'] = array(
'#type' => 'radios',
'#title' => $this->t("How should this display populate the view's arguments?"),
'#options' => $options,
'#default_value' => $default,
);
$form['token'] = array(
'#type' => 'fieldset',
'#title' => $this->t('Token replacement'),
'#collapsible' => TRUE,
'#states' => array(
'visible' => array(
':input[name=argument_mode]' => array('value' => 'token'),
),
),
);
$form['token']['default_argument'] = array(
'#title' => $this->t('Arguments'),
'#type' => 'textfield',
'#default_value' => $this->getOption('default_argument'),
'#description' => $this->t('You may use token replacement to provide arguments based on the current entity. Separate arguments with "/".'),
);
break;
case 'show_title':
$form['#title'] .= $this->t('Show title');
$form['show_title'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Show the title of the view above the attached view.'),
'#default_value' => $this->getOption('show_title'),
);
break;
}
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
switch ($form_state->get('section')) {
case 'entity_type':
if (empty($form_state->getValue('entity_type'))) {
$form_state->setError($form['entity_type'], $this->t('Must select an entity'));
}
break;
}
}
public function validate() {
$errors = array();
if (empty($this->getOption('entity_type'))) {
$errors[] = $this->t('Display "@display" must be attached to an entity.', array('@display' => $this->display['display_title']));
}
return $errors;
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
parent::submitOptionsForm($form, $form_state);
switch ($form_state->get('section')) {
case 'entity_type':
$new_entity = $form_state->getValue('entity_type');
$old_entity = $this->getOption('entity_type');
$this->setOption('entity_type', $new_entity);
if ($new_entity != $old_entity) {
// Each entity has its own list of bundles and view modes. If there's
// only one on the new type, we can select it automatically. Otherwise
// we need to wipe the options and start over.
$new_entity_info = \Drupal::entityManager()->getDefinition($new_entity);
$new_bundles_keys = \Drupal::entityManager()->getBundleInfo($new_entity);
$new_bundles = array();
if (count($new_bundles_keys) == 1) {
$new_bundles[] = $new_bundles_keys[0];
}
$this->setOption('bundles', $new_bundles);
}
break;
case 'bundles':
$this->setOption('bundles', array_values(array_filter($form_state->getValue('bundles'))));
break;
case 'arguments':
$this->setOption('argument_mode', $form_state->getValue('argument_mode'));
if ($form_state->getValue('argument_mode') == 'token') {
$this->setOption('default_argument', $form_state->getValue('default_argument'));
}
else {
$this->setOption('default_argument', NULL);
}
break;
case 'show_title':
$this->setOption('show_title', $form_state->getValue('show_title'));
break;
}
}
public function preExecute() {
parent::preExecute();
if (isset($this->view->current_entity)) {
$entity = $this->view->current_entity;
$entity_type = $this->view->display_handler->getOption('entity_type');
$entity_info = \Drupal::entityManager()->getDefinition($entity_type);
$arg_mode = $this->view->display_handler->getOption('argument_mode');
if ($arg_mode == 'token') {
if ($token_string = $this->view->display_handler->getOption('default_argument')) {
// Now do the token replacement.
$token_values = eva_get_arguments_from_token_string($token_string, $entity_type, $entity);
$new_args = array();
// We have to be careful to only replace arguments that have tokens.
foreach ($token_values as $key => $value) {
$new_args[Html::escape($key)] = Html::escape($value);
}
$this->view->args = $new_args;
}
}
elseif ($arg_mode == 'id') {
$this->view->args = array($entity->id());
}
}
}
public function getPath() {
if (isset($this->view->current_entity)) {
$uri = $this->view->current_entity->url();
if ($uri) {
$uri['options']['absolute'] = TRUE;
return url($uri['path'], $uri['options']);
}
}
return parent::getPath();
}
function execute() {
// Prior to this being called, the $view should already be set to this
// display, and arguments should be set on the view.
if (!isset($this->view->override_path)) {
$this->view->override_path = \Drupal::service('path.current')->getPath();
}
$element = $this->view->render();
if (!empty($this->view->result) || $this->getOption('empty') || !empty($this->view->style_plugin->definition['even empty'])) {
return $element;
}
}
}