Skip to content

Commit

Permalink
Access test with caching fix. #10
Browse files Browse the repository at this point in the history
  • Loading branch information
ekes committed Sep 19, 2021
1 parent 15f435c commit ed4d239
Show file tree
Hide file tree
Showing 5 changed files with 379 additions and 13 deletions.
3 changes: 2 additions & 1 deletion config/install/views.view.localgov_openreferral_services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ display:
options: { }
display_description: ''
cache:
type: none
type: search_api_tag
options: { }
defaults:
cache: false
cache_metadata:
Expand Down
22 changes: 22 additions & 0 deletions localgov_openreferral.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* 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;

/**
Expand All @@ -29,3 +32,22 @@ function localgov_openreferral_localgov_openreferral_mapping_delete(PropertyMapp
->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']);
}
}
1 change: 0 additions & 1 deletion src/MappingInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Drupal\localgov_openreferral;

use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
Expand Down
33 changes: 22 additions & 11 deletions src/Normalizer/EntityReferenceFieldNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public function normalize($field, $format = NULL, array $context = []) {
$direction = $reference_parent[$context['field']['public_name']] == $parent_type;

foreach ($field->referencedEntities() as $entity) {
$this->addCacheableDependency($context, $entity);
if (!$entity->access('view')) {
continue;
}
$type = $this->mappingInformation->getPublicType($entity->getEntityTypeId(), $entity->bundle());
$id = $direction ?
$parent->uuid() . '-' . $entity->uuid() :
Expand All @@ -85,21 +89,28 @@ public function normalize($field, $format = NULL, array $context = []) {
}
elseif (!empty($reference_single[$context['field']['public_name']])) {
$refrenced_entities = $field->referencedEntities();
$entity = reset($refrenced_entities);
if (count($context['parents']) < 3) {
$attributes = $this->serializer->normalize($entity, $format, $context);
}
else {
$attributes = $entity->uuid();
if ($entity = reset($refrenced_entities)) {
$this->addCacheableDependency($context, $entity);
if ($entity->access('view')) {
if (count($context['parents']) < 3) {
$attributes = $this->serializer->normalize($entity, $format, $context);
}
else {
$attributes = $entity->uuid();
}
}
}
}
else {
foreach ($field->referencedEntities() as $entity) {
if (count($context['parents']) < 3) {
$attributes[] = $this->serializer->normalize($entity, $format, $context);
}
else {
$attributes[] = ['id' => $entity->uuid()];
$this->addCacheableDependency($context, $entity);
if ($entity->access('view')) {
if (count($context['parents']) < 3) {
$attributes[] = $this->serializer->normalize($entity, $format, $context);
}
else {
$attributes[] = ['id' => $entity->uuid()];
}
}
}
}
Expand Down
Loading

0 comments on commit ed4d239

Please sign in to comment.