-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_draft.module
323 lines (272 loc) · 11.5 KB
/
entity_draft.module
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* @file
* Contains entity_draft.module.
*/
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function entity_draft_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.entity_draft':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Allow saving of entities in draft state.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_form_alter().
*/
function entity_draft_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\Core\Form\EntityForm $form_object */
$form_object = $form_state->getFormObject();
if (!method_exists($form_object, 'getEntity')) {
return;
}
if ($form_object->getEntity() instanceof ConfigEntityBundleBase &&
!$form_object instanceof EntityDeleteForm &&
!$form_object->getEntity()->isNew()) {
$entity = $form_object->getEntity();
$form['entity_draft'] = [
'#type' => 'details',
'#title' => t('Entity draft'),
'#group' => 'additional_settings',
'#weight' => 50,
];
$form['entity_draft']['enable_draft'] = [
'#type' => 'checkbox',
'#title' => t('Enable draft support'),
'#description' => t('Check this to enable draft support for this entity bundle.'),
'#default_value' => $entity->getThirdPartySetting('entity_draft', 'enable_draft', 0),
];
$form['entity_draft']['enable_draft']['#attributes']['class'][] = 'entity_draft_enabled';
$form['entity_draft']['entity_draft_unpublished'] = [
'#type' => 'checkbox',
'#title' => t('Drafts are unpublished'),
'#description' => t('Check this to set this entity bundle to unpublished when it is a draft. Note that this requires a status field.'),
'#default_value' => $entity->getThirdPartySetting('entity_draft', 'entity_draft_unpublished', 1),
'#states' => [
'visible' => [
'.entity_draft_enabled' => ['checked' => TRUE],
],
],
];
$form['entity_draft']['entity_draft_submit'] = [
'#type' => 'textfield',
'#title' => t('Button label'),
'#description' => t('Label on the button used to save a draft'),
'#default_value' => $entity->getThirdPartySetting('entity_draft', 'entity_draft_submit', t('Save Draft')),
'#states' => [
'visible' => [
'.entity_draft_enabled' => ['checked' => TRUE],
],
],
];
$form['entity_draft']['entity_draft_help'] = [
'#type' => 'textarea',
'#title' => t('Help message'),
'#description' => t('Optional help message to show underneath the entity draft button.'),
'#default_value' => $entity->getThirdPartySetting('entity_draft', 'entity_draft_help', ''),
'#states' => [
'visible' => [
'.entity_draft_enabled' => ['checked' => TRUE],
],
],
];
$entity_draft_message = $entity->getThirdPartySetting('entity_draft', 'entity_draft_message');
$form['entity_draft']['entity_draft_message'] = [
'#type' => 'text_format',
'#title' => t('Submit message'),
'#description' => t('Optional message to show on submit.'),
'#format' => empty($entity_draft_message['format']) ? filter_default_format() : $entity_draft_message['format'],
'#default_value' => empty($entity_draft_message['value']) ? '' : $entity_draft_message['value'],
'#states' => [
'visible' => [
'.entity_draft_enabled' => ['checked' => TRUE],
],
],
];
$form['entity_draft']['entity_draft_redirect'] = [
'#type' => 'textfield',
'#title' => t('Redirect'),
'#description' => t('The route to redirect to. Leave blank to reload page.'),
'#default_value' => $entity->getThirdPartySetting('entity_draft', 'entity_draft_redirect', ''),
];
$form['actions']['submit']['#submit'][] = 'entity_draft_entity_type_submit';
}
elseif (isset($form['#entity_builders'])) {
// @todo a config screen to specify when to add this in.
entity_draft_entity_edit_form_alter($form, $form_state, $form_id);
}
}
/**
* Alter entity edit form.
*/
function entity_draft_entity_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form_display = $form_state->getStorage()['form_display'];
$bundle = $form_display->getTargetBundle();
$entity_type = $form_display->getTargetEntityTypeId();
$entity_type_manager = \Drupal::service('entity_type.manager');
$bundle_entity_type = $entity_type_manager->getDefinition($entity_type)->get('bundle_entity_type');
if ($bundle_entity_type === NULL) {
return;
}
$config = $entity_type_manager->getStorage($bundle_entity_type)->load($bundle);
if (empty($config->getThirdPartySetting('entity_draft', 'enable_draft'))) {
return;
}
$form['actions']['draft'] = [
'#type' => 'submit',
'#value' => $config->getThirdPartySetting('entity_draft', 'entity_draft_submit'),
'#weight' => 0,
'#validate' => ['entity_draft_entity_validate_draft_edit_form'],
'#submit' => [
'::submitForm',
'::save',
'entity_draft_entity_save_draft_edit_form',
],
'#attributes' => [
'formnovalidate' => TRUE,
],
];
if (!empty($config->getThirdPartySetting('entity_draft', 'entity_draft_help'))) {
$form['actions']['draft_help_container'] = [
'#type' => 'fieldset',
'#weight' => -101,
];
$form['actions']['draft_help_container']['draft_help'] = [
'#type' => 'item',
'#description' => $config->getThirdPartySetting('entity_draft', 'entity_draft_help'),
];
}
$form['actions']['submit']['#submit'][] = 'entity_draft_entity_submit_edit_form';
}
/**
* Submit entity edit form.
*/
function entity_draft_entity_submit_edit_form(&$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Form\EntityForm $form_object */
$form_object = $form_state->getFormObject();
$entity = $form_object->getEntity();
$entity->set('is_draft', FALSE);
$entity->save();
}
/**
* Validate entity draft edit form.
*/
function entity_draft_entity_validate_draft_edit_form(&$form, FormStateInterface $form_state) {
// Trigger entity validation.
$form_state->getFormObject()->validateForm($form, $form_state);
// Determine if any fields are required in the database (IE can't be NULL).
// If these fields have errors, set the error for the field and return early.
$configs = $form_state->getStorage()['form_display']->getPluginCollections()['widgets']->getConfiguration();
$errors = $form_state->getErrors();
// Clearing errors now because we don't want all form errors to appear, just
// the ones for fields that can't be NULL in the database.
$form_state->clearErrors();
foreach ($errors as $key => $error) {
$field_name = substr($key, 0, strpos($key, ']'));
if (empty($field_name)) {
continue;
}
foreach ($configs as $config) {
if ($config['field_definition']->getName() !== $field_name) {
continue;
}
if ($config['field_definition']->isRequired()) {
$form_state->setErrorByName($key, $error);
return;
}
}
}
}
/**
* Save entity via the draft button.
*/
function entity_draft_entity_save_draft_edit_form(&$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Form\EntityForm $form_object */
$form_object = $form_state->getFormObject();
$entity = $form_object->getEntity();
$form_display = $form_state->getStorage()['form_display'];
$bundle = $form_display->getTargetBundle();
$entity_type = $form_display->getTargetEntityTypeId();
$entity_type_manager = \Drupal::service('entity_type.manager');
$bundle_entity_type = $entity_type_manager->getDefinition($entity_type)->get('bundle_entity_type');
$config = $entity_type_manager->getStorage($bundle_entity_type)->load($bundle);
$entity->set('is_draft', TRUE);
if ($entity->hasField('status')) {
$entity->set('status', !$config->getThirdPartySetting('entity_draft', 'entity_draft_unpublished'));
}
$entity->save();
$entity_draft_message = $config->getThirdPartySetting('entity_draft', 'entity_draft_message', FALSE);
if ($entity_draft_message !== FALSE && !empty($entity_draft_message['value'])) {
\Drupal::messenger()->addMessage(
t(check_markup($entity_draft_message['value'], $entity_draft_message['format'])->__toString())
);
}
$entity_draft_redirect = $config->getThirdPartySetting('entity_draft', 'entity_draft_redirect', '');
if (!empty($entity_draft_redirect)) {
$form_state->setRedirect($entity_draft_redirect);
}
}
/**
* Custom entity type form submit handling.
*/
function entity_draft_entity_type_submit(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Form\EntityForm $form_object */
$form_object = $form_state->getFormObject();
$entity = $form_object->getEntity();
$entity->setThirdPartySetting('entity_draft', 'enable_draft', $form_state->getValue('enable_draft'))
->setThirdPartySetting('entity_draft', 'entity_draft_unpublished', $form_state->getValue('entity_draft_unpublished'))
->setThirdPartySetting('entity_draft', 'entity_draft_submit', $form_state->getValue('entity_draft_submit'))
->setThirdPartySetting('entity_draft', 'entity_draft_help', $form_state->getValue('entity_draft_help'))
->setThirdPartySetting('entity_draft', 'entity_draft_message', $form_state->getValue('entity_draft_message'))
->setThirdPartySetting('entity_draft', 'entity_draft_redirect', $form_state->getValue('entity_draft_redirect'))
->save();
// Set required handling using controlled_fields.
$config_entity_type = $entity->getEntityType();
$bundle_of = $config_entity_type->getBundleOf();
$bundle = $entity->id();
$field_definitions = controlled_fields_fetch_field_definitions($bundle_of, $bundle);
$entity_manager = \Drupal::entityTypeManager();
$entity_display_repository = \Drupal::service('entity_display.repository');
$entity_form_modes = $entity_display_repository->getFormModes($bundle_of);
$valid_form_modes = ['default'];
foreach ($entity_form_modes as $form_mode_id => $form_mode) {
/** @var Drupal\Core\Entity\Entity\EntityFormDisplay $entity_form_display */
$entity_form_display = $entity_manager->getStorage('entity_form_display')->load($bundle_of . '.' . $entity->get('type') . '.' . $form_mode_id);
if ($entity_form_display !== NULL && $entity_form_display->get('id')) {
array_push($valid_form_modes, $form_mode_id);
}
}
foreach ($valid_form_modes as $form_mode_id) {
controlled_fields_enable_form_display($bundle_of, $bundle, $form_mode_id, $form_state->getValue('enable_draft'));
}
controlled_fields_field_config_change_required($field_definitions, $form_state->getValue('enable_draft'));
}
/**
* Implements hook_entity_base_field_info().
*/
function entity_draft_entity_base_field_info(EntityTypeInterface $entity_type) {
// Add in is_draft to all bundle enabled entities.
$entity_manager = \Drupal::entityTypeManager();
foreach ($entity_manager->getDefinitions() as $entity_type_id => $entity_type_config) {
$bundle_of = $entity_type_config->get('bundle_of');
if ($bundle_of == $entity_type->id()) {
$fields = [];
$fields['is_draft'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Is draft'))
->setDescription(t('Whether this entity is currently in draft state.'))
->setDefaultValue(FALSE);
return $fields;
}
}
}