forked from openeuropa/oe_content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oe_content.module
108 lines (93 loc) · 3.19 KB
/
oe_content.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
<?php
/**
* @file
* The OpenEuropa Content module.
*/
declare(strict_types = 1);
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Session\AccountInterface;
use Drupal\rdf_entity\Entity\Rdf;
use Drupal\Core\Database\Query\AlterableInterface;
/**
* Implements hook_entity_base_field_info().
*/
function oe_content_entity_base_field_info(EntityTypeInterface $entity_type): array {
// Add the "provenance_uri" field to the RDF entity type.
if ($entity_type->id() !== 'rdf_entity') {
return [];
}
$fields = [];
$fields['provenance_uri'] = BaseFieldDefinition::create('uri')
->setLabel(t('Provenance URI'))
->setDescription(t('A URI that indicates the provenance of the entity.'))
->setTranslatable(FALSE);
return $fields;
}
/**
* Implements hook_entity_presave().
*/
function oe_content_entity_presave(EntityInterface $entity): void {
// RDF entities need to be saved with a provenance URI.
if (!$entity instanceof Rdf) {
return;
}
$uri = \Drupal::config('oe_content.settings')->get('provenance_uri');
if (!$uri) {
throw new \Exception('The Provenance URI is not set.');
}
$entity->set('provenance_uri', $uri);
}
/**
* Implements hook_rdf_apply_default_fields_alter().
*/
function oe_content_rdf_apply_default_fields_alter(string $type, array &$values): void {
// Since the "full_html" is not guaranteed to exist, we default to plain_text
// as the format for the text_long field type.
if ($type == 'text_long') {
foreach ($values as &$value) {
if ($value['format'] == 'full_html') {
$value['format'] = 'plain_text';
}
}
}
}
/**
* Implements hook_ENTITY_TYPE_access().
*/
function oe_content_rdf_entity_access(EntityInterface $entity, string $operation, AccountInterface $account): AccessResultInterface {
if ($operation == 'view') {
return AccessResult::neutral();
}
$provenance_uri = \Drupal::config('oe_content.settings')->get('provenance_uri');
return $entity->get('provenance_uri')->value === $provenance_uri ? AccessResult::neutral() : AccessResult::forbidden();
}
/**
* Implements hook_ENTITY_TYPE_storage_load().
*/
function oe_content_taxonomy_term_storage_load(array $entities): void {
// Taxonomy terms have a status field for handling publishing state. We need
// to default to TRUE for all terms because the RDF storage which handles
// taxonomy does not know about Status.
// @todo Remove once we refactor away from RDF taxonomy.
foreach ($entities as $entity) {
$entity->set('status', TRUE);
}
}
/**
* Implements hook_query_QUERY_TAG_alter() for the taxonomy_term_access tag.
*/
function oe_content_query_taxonomy_term_access_alter(AlterableInterface $query) {
// We need to remove the Status condition for RDF taxonomies when they are
// queried. Same reason as oe_content_taxonomy_term_load().
// @todo Remove once we refactor away from RDF taxonomy.
$conditions = &$query->conditions();
foreach ($conditions as $key => $condition) {
if ($condition['field'] === 'status') {
unset($conditions[$key]);
}
}
}