-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference_value_pair.module
More file actions
156 lines (141 loc) · 6.01 KB
/
reference_value_pair.module
File metadata and controls
156 lines (141 loc) · 6.01 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
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
<?php
/**
* @file
* Contains reference_value_pair.module..
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\jsonld\Normalizer\NormalizerBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
/**
* Implements hook_help().
*/
function reference_value_pair_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the reference_value_pair module.
case 'help.page.reference_value_pair':
$build['title'] = [
'#type' => 'html_tag',
'#tag' => 'h3',
'#value' => t('About'),
];
$build['content'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('Defines a field type to store a pair of an entity reference and a value.'),
];
return $build;
default:
}
}
/**
* Implements hook_theme().
*/
function reference_value_pair_theme() {
return array(
'reference_value_pair_formatter' => array(
'variables' => array(
'item' => NULL,
'entity' => NULL,
'url' => NULL,
'element' => array(),
),
),
);
}
/**
* Prepares variables for reference value pair formatter templates.
*
* Default template: reference-value-pair-formatter.html.twig.
*
* @param array $variables
* An associative array containing:
* - item: A ReferenceValuePair object.
* - entity: An optional associative array of HTML attributes to be
* placed in the img tag.
*/
function template_preprocess_reference_value_pair_formatter(&$variables) {
$variables['value'] = $variables['item']->value;
$variables['label'] = $variables['entity'] ? $variables['entity']->label() : $variables['label'];
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function reference_value_pair_theme_suggestions_reference_value_pair_formatter(array $variables) {
$suggestions = array();
$element = $variables['element'];
$prefix = $variables['theme_hook_original'] . '__';
$suggestions[] = $prefix . $element['#field_type'];
$suggestions[] = $prefix . $element['#field_name'];
$suggestions[] = $prefix . $element['#entity_type'] . '__' . $element['#bundle'];
$suggestions[] = $prefix . $element['#entity_type'] . '__' . $element['#field_name'];
$suggestions[] = $prefix . $element['#entity_type'] . '__' . $element['#field_name'] . '__' . $element['#bundle'];
return $suggestions;
}
/**
* Implements hook_jsonld_alter_normalized_array().
*/
function reference_value_pair_jsonld_alter_normalized_array(EntityInterface $entity, array &$normalized, array $context) {
// TODO - make sure this checks to see if it's a language reference pair, once that Field Type logic is in
if (isset($normalized['@graph']) && is_array($normalized['@graph'])) {
foreach ($entity->getFieldDefinitions() as $field => $field_definition) {
//\Drupal::logger('reference_value_pair')->warning("field type: ". $field_definition->getType() . " Field: " . $field);
if (!empty($entity->get($field)->getValue())) {
if ($field_definition->getType() == 'language_value_pair' ||
$field_definition->getType() == 'reference_value_pair' ) {
if (isset($context['current_entity_rdf_mapping']->get('fieldMappings')[$field])) {
foreach ($context['current_entity_rdf_mapping']->get('fieldMappings')[$field]['properties'] as $predicate) {
// check to see if there are already values there, and if so, clear them out as we are
// going to override them below.
$predicate_normalized = NormalizerBase::escapePrefix($predicate, $context['namespaces']);
if (!empty($normalized['@graph'][0][$predicate_normalized])) {
$normalized['@graph'][0][$predicate_normalized] = [];
}
foreach ($entity->get($field)->getValue() as $value) {
if (empty($value['target_id'])) {
\Drupal::logger('reference_value_pair')->warning("Missing target entity for %field in %entity_type/%id (%bundle)",
[
'%field' => $field,
'%entity_type' => $entity->getEntityTypeId(),
'%bundle' => $entity->bundle(),
'%id' => $entity->id(),
]);
continue;
}
$referenced_entity = \Drupal::entityTypeManager()->getStorage($field_definition->getSetting('target_type'))->load($value['target_id']);
if (empty($referenced_entity)) {
\Drupal::logger('reference_value_pair')->warning("Invalid target entity for %field in %entity_type/%id (%bundle)",
[
'%field' => $field,
'%entity_type' => $entity->getEntityTypeId(),
'%bundle' => $entity->bundle(),
'%id' => $entity->id(),
]);
continue;
}
$language = $referenced_entity->field_language_code->getValue();
if (empty($language)) {
\Drupal::logger('reference_value_pair')->warning("Invalid Language Code in Language Taxonomy term %entity_type/%id (%bundle)",
[
'%entity_type' => $referenced_entity->getEntityTypeId(),
'%bundle' => $referenced_entity->bundle(),
'%id' => $referenced_entity->id(),
]);
continue;
}
$data = array('@value' => $value['value']);
if ($field_definition->getType() == 'language_value_pair') {
$data['@language'] = $referenced_entity->field_language_code->getValue()[0]['value'];
} else {
$data['@id'] = $referenced_entity->toUrl('canonical', ['absolute' => TRUE])->setRouteParameter('_format', 'jsonld')->toString();
}
$normalized['@graph'][0][$predicate_normalized][] = $data;
}
}
}
}
}
}
}
}