-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocalgov_openreferral.module
53 lines (48 loc) · 1.89 KB
/
localgov_openreferral.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
<?php
/**
* @file
* Primary module hooks for Open Referral module.
*/
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityInterface;
use Drupal\localgov_openreferral\Entity\PropertyMappingInterface;
use Drupal\localgov_openreferral\Entity\PropertyMappingStorage;
use Drupal\localgov_openreferral\SearchApiIndexConfig;
/**
* Implements hook_entity_insert().
*/
function localgov_openreferral_localgov_openreferral_mapping_insert(PropertyMappingInterface $map) {
if ($map->getPublicType() == 'service') {
\Drupal::service('class_resolver')
->getInstanceFromDefinition(SearchApiIndexConfig::class)
->addToServicesIndex($map);
}
}
/**
* Implements hook_entity_delete().
*/
function localgov_openreferral_localgov_openreferral_mapping_delete(PropertyMappingInterface $map) {
if ($map->mappedEntityType() == 'node') {
\Drupal::service('class_resolver')
->getInstanceFromDefinition(SearchApiIndexConfig::class)
->removeFromServicesIndex($map);
}
}
/**
* Implements hook_entity_presave().
*/
function localgov_openreferral_entity_presave(EntityInterface $entity) {
// If the entity is one that can appear in the Views services list invalidate
// that tag. search_api_list:openreferral_services.
// Search API does a reasonable job of clearing the list if it is a referenced
// entity in the list; but not if it's not explicitly in the search index and
// just something that gets rendered as referenced.
//
// Having the bubbleable caching metadata ala jsonapi #15 would probably
// negate the need to do this, as long as the render plugin passes it on.
$mapping_storage = \Drupal::entityTypeManager()->getStorage('localgov_openreferral_mapping');
assert($mapping_storage instanceof PropertyMappingStorage);
if ($mapping_storage->loadByIds($entity->getEntityTypeId(), $entity->bundle())) {
Cache::invalidateTags(['search_api_list:openreferral_services']);
}
}