-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvarbase_workflow.module
154 lines (124 loc) · 6.14 KB
/
varbase_workflow.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
<?php
/**
* @file
* Contains varbase_workflow.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Vardot\Entity\EntityDefinitionUpdateManager;
// Include all helpers.
include_once __DIR__ . '/includes/helpers.inc';
/**
* Implements hook_form_alter().
*/
function varbase_workflow_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'node_type_add_form' || $form_id == 'node_type_edit_form') {
$default_workflow_option = '_none';
// List of workflows.workflow.* config in the site.
$content_moderation_workflow_options = ['_none' => t('- none -')];
$config_factory = \Drupal::service('config.factory');
$workflows = $config_factory->listAll('workflows.workflow.');
foreach ($workflows as $workflow) {
$content_moderation_workflow_options[$workflow] = $config_factory->getEditable($workflow)->get('label');
if ($workflow == 'workflows.workflow.varbase_simple_workflow'
&& $default_workflow_option == '_none') {
$default_workflow_option = 'workflows.workflow.varbase_simple_workflow';
}
}
$workflows_configuration_page = Link::fromTextAndUrl(t('Workflows configuration page'), new Url('entity.workflow.collection'));
$form['workflow']['content_moderation_workflow'] = [
'#type' => 'select',
'#title' => t('Content moderation workflow'),
'#default_value' => $default_workflow_option,
'#options' => $content_moderation_workflow_options,
'#description' => t('Select the workflow you would like to use for this content type. Once selected, you can only change it for this content type from the @link.', ['@link' => $workflows_configuration_page->toString()]),
];
if ($form_id == 'node_type_add_form') {
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = '_varbase_workflow_node_type_add_form';
}
}
}
else {
$form['workflow']['content_moderation_workflow']['#attributes']['readonly'] = 'readonly';
$form['workflow']['content_moderation_workflow']['#attributes']['disabled'] = 'disabled';
$form['workflow']['content_moderation_workflow']['#default_value'] = '_none';
foreach ($workflows as $workflow) {
$workflow_type_settings = $config_factory->getEditable($workflow)->get('type_settings');
$node_type = $form_state->getFormObject()->getEntity()->get('type');
if (isset($workflow_type_settings['entity_types'])
&& isset($workflow_type_settings['entity_types']['node'])) {
if (in_array($node_type, $workflow_type_settings['entity_types']['node'])) {
$form['workflow']['content_moderation_workflow']['#default_value'] = $workflow;
}
}
}
}
}
elseif (preg_match('/^node_.*._form$/', $form_id) && isset($form['moderation_state'])) {
if (isset($form['publish_state'])) {
$form['publish_state']['#access'] = FALSE;
$form['publish_state']['widget'][0]['#default_value'] = 'published';
}
if (isset($form['unpublish_state'])) {
$form['unpublish_state']['#access'] = FALSE;
$form['unpublish_state']['widget'][0]['#default_value'] = 'archived';
}
// Move the moderation status to actions when the Gin admin theme.
$active_theme = \Drupal::theme()->getActiveTheme();
$base_themes = (array) $active_theme->getBaseThemeExtensions();
if ($active_theme->getName() === 'gin' || array_key_exists('gin', $base_themes)) {
// Copy the moderation status form.
$form['moderation_state_sticky_form'] = $form['moderation_state'];
$form['moderation_state_sticky_form']['#group'] = 'status';
// Change the title of the submit button.
$form['actions']['submit']['#value'] = t('Save as');
// Add the Varbase Workflow moderation status theme styling library to fit in with actions.
$form['#attached']['library'][] = 'varbase_workflow/moderation-state';
}
}
return $form;
}
/**
* Varbase workflow node type add form.
*/
function _varbase_workflow_node_type_add_form(array &$form, FormStateInterface &$form_state) {
$node_type = $form_state->getFormObject()->getEntity()->get('type');
$content_moderation_workflow = $form_state->getFormObject()->getEntity()->get('content_moderation_workflow');
if (isset($content_moderation_workflow)
&& $content_moderation_workflow != ''
&& $content_moderation_workflow != '_none') {
$config_factory = \Drupal::service('config.factory');
$workflow_type_settings = $config_factory->getEditable($content_moderation_workflow)->get('type_settings');
if (isset($workflow_type_settings['entity_types'])) {
if (isset($workflow_type_settings['entity_types']['node'])) {
if (!in_array($node_type, $workflow_type_settings['entity_types']['node'])) {
$workflow_type_settings['entity_types']['node'][] = $node_type;
$config_factory->getEditable($content_moderation_workflow)->set('type_settings', $workflow_type_settings)->save(TRUE);
}
}
else {
$workflow_type_settings['entity_types']['node'] = [];
$workflow_type_settings['entity_types']['node'][] = $node_type;
$config_factory->getEditable($content_moderation_workflow)->set('type_settings', $workflow_type_settings)->save(TRUE);
}
// Entity updates to clear up any mismatched entity
// and/or field definitions
// And Fix changes were detected in the entity type
// and field definitions.
\Drupal::classResolver()
->getInstanceFromDefinition(EntityDefinitionUpdateManager::class)
->applyUpdates();
}
}
}
/**
* Implements hook_entity_bundle_create().
*/
function varbase_workflow_entity_bundle_create($entity_type_id, $bundle) {
// Grant new access unpublished permissions for anonymous and all authenticated user roles.
varbase_workflow__grant_access_unpublished_permissions('anonymous', $entity_type_id, $bundle);
varbase_workflow__grant_access_unpublished_permissions('authenticated', $entity_type_id, $bundle);
}