-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_mode_conditional_fields.module
More file actions
127 lines (116 loc) · 4.61 KB
/
Copy pathview_mode_conditional_fields.module
File metadata and controls
127 lines (116 loc) · 4.61 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
<?php
/**
* @file
* Allows to disable field of entities on the forms.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\BlockContentForm;
/**
* Implements hook_help().
*/
function view_mode_conditional_fields_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the view_mode_conditional_fields module.
case 'help.page.view_mode_conditional_fields':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('View Mode Conditional module provide settings to administrator to control fields visibility for block entity.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*
* Attach the settings block entity manage field, to control the visibility.
*/
function view_mode_conditional_fields_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (isset($form['#entity']) && $form['#entity'] instanceof BlockContent) {
$view_modes = \Drupal::entityQuery('entity_view_mode')
->condition('targetEntityType', 'block_content')
->execute();
$view_modes['block_content.default'] = 'block_content.default';
$options = [];
foreach ($view_modes as $view_mode) {
$expStr = explode(".", $view_mode);
$options[$expStr[1]] = $expStr[1];
}
$field = $form_state->getFormObject()->getEntity();
$form['third_party_settings']['view_mode_conditional_fields']['field_visibility_display'] = [
'#type' => 'checkboxes',
'#options' => $options,
'#title' => t("View Mode Conditional Fields Settings"),
'#description' => t("Field will be available on selected view mode. If no view mode selected then field will be available on form display's."),
'#default_value' => $field->getThirdPartySetting('view_mode_conditional_fields', 'field_visibility_display'),
'#weight' => -10,
];
$form['#validate'][] = 'config_edit_form_validation';
}
}
/**
* Attaching settings to control visibility of fields on display.
*
* @param array $element
* The elements of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
* @param array $context
* The context.
*/
function view_mode_conditional_fields_field_widget_form_alter(array &$element, FormStateInterface $form_state, array $context) {
$applicable_forms = [
'layout_builder_add_block',
'layout_builder_update_block',
];
if ($form_state->getFormObject() instanceof BlockContentForm || in_array($form_state->getFormObject()->getFormId(), $applicable_forms)) {
$field_definition = $context['items']->getFieldDefinition();
if ($field_definition instanceof FieldConfig) {
if ($field_definition->getThirdPartySetting('view_mode_conditional_fields', 'field_visibility_display')) {
if (\Drupal::request()->query->get('element_parents')) {
return TRUE;
}
$class_list = $field_definition->getThirdPartySetting('view_mode_conditional_fields', 'field_visibility_display');
if (!empty($class_list) && is_array($class_list)) {
$classes = '';
foreach ($class_list as $item) {
$classes .= ' block_content.' . $item;
}
$classes = "'" . $classes . " field-visibility-identifier'";
$element['#prefix'] = "<div class=" . $classes . ">";
$element['#suffix'] = "</div>";
}
}
}
}
}
/**
* Implements hook_form_alter().
*/
function view_mode_conditional_fields_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$applicable_forms = [
'layout_builder_add_block',
'layout_builder_update_block',
];
if ($form_state->getFormObject() instanceof BlockContentForm || in_array($form_id, $applicable_forms)) {
$form['#attached']['library'][] = 'view_mode_conditional_fields/view_mode_conditional_fields.integration';
}
}
/**
* The configuration edit form validation.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
*/
function config_edit_form_validation(array &$form, FormStateInterface $form_state) {
$field = $form_state->getFormObject()->getEntity();
if (($field->get('required') == 1)) {
if (!empty($field->get('third_party_settings')['view_mode_conditional_fields']['field_visibility_display'])) {
$form_state->setErrorByName('third_party_settings', t('Required and View Mode Conditional Fields Settings both cant work together.'));
}
}
}