This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtide_data_driven_component.module
80 lines (74 loc) · 2.37 KB
/
tide_data_driven_component.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
<?php
/**
* @file
* Data Driven Component module functionality.
*/
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Set dynamic allowed values for the data drive component field.
*
* @param \Drupal\field\Entity\FieldStorageConfig $definition
* The field definition.
* @param \Drupal\Core\Entity\ContentEntityInterface|null $entity
* The entity being created if applicable.
* @param bool $cacheable
* Boolean indicating if the results are cacheable.
*
* @return array
* An array of possible key and value options.
*
* @see options_allowed_values()
*/
function tide_data_driven_component_items_list(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable) {
$options = [
'myvic_vicfreewifi' => 'Vic Free Wifi Map',
'myvic_family_violence_women_programs' => 'Prevention of Family Violence Against Women Programs Map',
];
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties([
'vid' => 'data_driven_component',
]);
if ($terms) {
$options = [];
foreach ($terms as $term) {
if ($term->hasField('field_machine_name') && !$term->field_machine_name->isEmpty()) {
$options[$term->field_machine_name->value] = $term->label();
}
}
}
return $options;
}
/**
* Implements hook_entity_bundle_field_info_alter().
*/
function tide_data_driven_component_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
if ($entity_type->id() == 'taxonomy_term' && $bundle == 'data_driven_component') {
if (!empty($fields['field_machine_name'])) {
$fields['field_machine_name']->addConstraint('TideMachineNameField');
}
}
}
/**
* Sets the default value for the data driven component field.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity being created.
* @param \Drupal\field\Entity\FieldConfig $config
* The field config definition.
*
* @return array
* An array of default value keys with each entry keyed with the “value” key.
*
* @see \Drupal\Core\Field\FieldConfigBase::getDefaultValue()
*/
function tide_data_driven_component_default_value(
ContentEntityInterface $entity,
FieldConfig $config
) {
$default = 'myvic_vicfreewifi';
return [
['value' => $default],
];
}