-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachinevision_setup.module
81 lines (66 loc) · 1.76 KB
/
machinevision_setup.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
<?php
/**
* @file
* Primary module hooks for MachineVision module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeForm;
use Drupal\views\ViewExecutable;
/**
* Implements hook_form_alter().
*
* Custom overrides of admin theme node forms.
*
* @param $form
* @param FormStateInterface $form_state
* @param $form_id
*/
function machinevision_setup_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_state->getFormObject() instanceof NodeForm) {
$form['#attached']['library'][] = 'machinevision_setup/forms';
foreach ($form as $name => $FormElement) {
if (!is_array($FormElement)) {
continue;
}
if (isFormElementLarge($FormElement)) {
addMultiColumnToForm($form, $name);
}
}
}
}
function addMultiColumnToForm(&$form, $name) {
$form[$name]['#attributes']['class'][] = 'form-multi-column';
}
function isFormElementLarge(array $formElement): bool
{
if (!isset($formElement['widget'])) {
return false;
}
if (!isset($formElement['widget']['#options'])) {
return false;
}
return !(strripos($formElement['widget']['#field_name'], 'field_') === false) && (count($formElement['widget']['#options']) >= 15);
}
/**
* Implements hook_views_pre_view().
*/
function machinevision_setup_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
if ($view->id() !== 'taxonomy_term') {
return;
}
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($args[0]);
if (!isset($term)) {
return;
}
$vocabulariesUsedByCharacters = [
'age',
'gender',
'race',
'sexuality',
'species',
];
$bundle = $term->bundle();
if (in_array($term->bundle(), $vocabulariesUsedByCharacters, true)) {
$view->setDisplay('page_2');
}
}