Repository navigation
Expand file tree
/
Copy patheyecatcher_queue.module
More file actions
153 lines (135 loc) · 4.78 KB
/
Copy patheyecatcher_queue.module
File metadata and controls
153 lines (135 loc) · 4.78 KB
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
<?php
/**
* @file
* Module file.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Render\Element;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormComplex;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\cache\CachePluginBase;
use Drupal\eyecatcher_queue\Plugin\views\area\EyecatcherArea;
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function eyecatcher_queue_form_entity_subqueue_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\entityqueue\Form\EntitySubqueueForm $subqueue_form */
$subqueue_form = $form_state->getBuildInfo()['callback_object'];
/** @var \Drupal\eyecatcher_queue\Service\EyecatcherService $service */
$service = \Drupal::service('eyecatcher_queue.service.eyecatcher');
if (!$service->isEyecatcherQueue($subqueue_form->getEntity())) {
return;
}
$form['#title'] = $subqueue_form->getEntity()->label();
$form['actions']['clear']['#value'] = t('Clear all');
$form['items']['widget']['actions']['ief_add']['#value'] = t('Add Eyecatcher');
// Disable queue specific actions.
unset($form['actions']['shuffle']);
unset($form['actions']['reverse']);
foreach (Element::children($form['actions']) as $action) {
$current_action = &$form['actions'][$action];
if ($current_action['#type'] == 'submit') {
$current_action['#submit'][] = 'eyecatcher_queue_form_entity_subqueue_eyecatcher_edit_form_submit';
}
}
}
/**
* Custom submit handler for the queue config.
*/
function eyecatcher_queue_form_entity_subqueue_eyecatcher_edit_form_submit(&$form, FormStateInterface $form_state) {
$form_state->setRedirectUrl(Url::createFromRequest(\Drupal::request()));
// Clear entityqueue status message and set our own.
drupal_get_messages('status', TRUE);
drupal_set_message(t('The Eyecatcher Config has been updated.'));
}
/**
* Implements hook_field_widget_form_alter().
*/
function eyecatcher_queue_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
/** @var InlineEntityFormComplex $widget */
$widget = &$context['widget'];
/** @var FieldItemListInterface $items */
$items = &$context['items'];
/** @var \Drupal\eyecatcher_queue\Service\EyecatcherService $service */
$service = \Drupal::service('eyecatcher_queue.service.eyecatcher');
if (
!$widget instanceof InlineEntityFormComplex
|| !$items instanceof FieldItemListInterface
|| !$service->isEyecatcherQueue($items->getEntity())
) {
return;
}
foreach (Element::children($element['entities']) as $key) {
$current_element = &$element['entities'][$key];
/** @var \Drupal\Core\Entity\Entity $entity */
$entity = $current_element['#entity'];
$current_element['#label'] = $entity->label();
}
}
/**
* Implements hook_inline_entity_form_table_fields_alter().
*/
function eyecatcher_queue_inline_entity_form_table_fields_alter(&$fields, $context) {
/** @var \Drupal\eyecatcher_queue\Service\EyecatcherService $service */
$service = \Drupal::service('eyecatcher_queue.service.eyecatcher');
if (
empty($context['parent_entity_type']) || empty($context['parent_bundle'])
|| $context['parent_entity_type'] != 'entity_subqueue'
|| !in_array($context['parent_bundle'], $service->getEyecatcherQueueBundles())
) {
return;
}
$fields['type'] = [
'type' => 'callback',
'label' => 'Type',
'weight' => 10,
'callback' => 'eyecatcher_queue_inline_entity_form_fields_callback',
'callback_arguments' => [
'field' => 'type',
],
];
}
/**
* Callback to return display value for custom fields.
*
* @param EntityInterface $entity
* The current entity.
* @param array $variables
* - form: The form.
* - theme_hook_original: Original theme hook.
* @param string $field
* The field id.
*
* @see eyecatcher_queue_inline_entity_form_table_fields_alter()
*
* @return string|null
*/
function eyecatcher_queue_inline_entity_form_fields_callback(EntityInterface $entity, array $variables, $field) {
switch ($field) {
case 'type':
return $entity->bundle();
}
return NULL;
}
/**
* Implements hook_views_post_render().
*
* Sets up the eyecatcher, it will be rendered at the end of every page load.
*/
function eyecatcher_queue_views_post_render(ViewExecutable $view, &$output, CachePluginBase $cache) {
foreach (['footer', 'header'] as $area) {
if (isset($view->{$area}) && is_array($view->{$area})) {
/** @var \Drupal\views\Plugin\views\area\AreaPluginBase[] $view_area */
$view_area = $view->{$area};
foreach ($view_area as $plugin) {
if ($plugin instanceof EyecatcherArea) {
/** @var EyecatcherArea $plugin */
$plugin->postRender($output);
}
}
}
}
}