Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for cache tags in JSON-LD alter hook. #862

Merged
merged 8 commits into from
Mar 25, 2022
Merged

Conversation

alxp
Copy link

@alxp alxp commented Feb 2, 2022

GitHub Issue: (link) Islandora/documentation#2039

What does this Pull Request do?

Fixes a white screen when getting a JSON-LD representation of a node when the controller includes an entity access check.

What's new?

Adds an 'isset' to fix a null array access in the jsonld normalizer alter function.

  • Does this change add any new dependencies?
    no

  • Does this change require any other modifications to be made to the repository
    no

  • Could this change impact execution of existing code?
    No

How should this be tested?

Problem occurs when dealing with unpublished content.

If you have a controller that correctly checks for entity access, e.g., in routing.yml:

islandora_premis.premis:
  path: '/node/{node}/premis'
  defaults:
    _controller: '\Drupal\islandora_premis\Controller\IslandoraPremisPremisController::main'
  options:
    parameters:
      node:
        type: 'entity:node'
  requirements:
    _entity_access: 'node.view'

The entity access check makes it such that the output is not cacheable as it would be without it. Thus there is no 'cache ability' array element.

All entities accessible to the logged-in user should now be visible as JSON-LD, e.g.

/node/{nid}?_premis=jsonld

Documentation Status

  • Does this change existing behaviour that's currently documented? no
  • Does this change require new pages or sections of documentation? no

Interested parties

Tag (@ mention) interested parties or, if unsure, @Islandora/8-x-committers

Use valid license string per composer validate.
@alxp
Copy link
Author

alxp commented Mar 23, 2022

Looks like no problem requiring filehash:'^1.1 || ^2'

@alxp alxp requested a review from jordandukart March 23, 2022 18:48
@nigelgbanks
Copy link
Member

Was a bit concerned about changes to filehash so I had a look into where its used by islandora, and I could only find this instance:

islandora/islandora.module

Lines 178 to 203 in 2923a1a

/**
* Implements hook_file_update().
*/
function islandora_file_update(FileInterface $file) {
// Exit early if unchanged.
if ($file->filehash != NULL && $file->original->filehash != NULL && $file->filehash['sha1'] == $file->original->filehash['sha1']) {
return;
}
$utils = \Drupal::service('islandora.utils');
// Execute index reactions.
$utils->executeFileReactions('\Drupal\islandora\Plugin\ContextReaction\IndexReaction', $file);
// Execute derivative reactions.
foreach ($utils->getReferencingMedia($file->id()) as $media) {
$node = $utils->getParentNode($media);
if ($node) {
$utils->executeDerivativeReactions(
'\Drupal\islandora\Plugin\ContextReaction\DerivativeReaction',
$node,
$media
);
}
}
}
.

That being said it doesn't look like the hook_file_update actually exists in either Drupal 8.x / 9.x.

https://api.drupal.org/api/drupal/8.0.x/search/hook_file_update
https://api.drupal.org/api/drupal/9.4.x/search/hook_file_update

So I don't think this code is ever called? Perhaps the dependency along with this function could be removed completely?

@nigelgbanks
Copy link
Member

@seth-shaw-unlv
Copy link

@nigelgbanks, it won't show up in a search because it is covered by hook_entity_update. This allows us to trigger new derivatives when the file gets replaced.

@nigelgbanks
Copy link
Member

@seth-shaw-unlv ah that makes sense. Could a possible issue arise if the user chooses any other algorithm than sha1 for filehash (I've got a client currently pinned to MD5 only)? In that cause the function would always exit early? That being said I think the default behaviour is for Drupal to create a new File entity when changing the file of a Media Entity? In which case this wouldn't get called? Though I'm just speculating.

@seth-shaw-unlv
Copy link

@nigelgbanks, the Drupal web UI doesn't let you replace a file, but there are various other ways to do it (e.g. the Migrate API import update flag, custom code, and the endpoints our Crayfish microservices hit).

As for pinning MD5, 🤷‍♂️ I haven't tested it.

@jordandukart
Copy link
Member

Think if there's additional discussion required about Islandora's usage of filehash another issue should be made as this ticket is separate and bumping filehash for sake of the tests passing.

@jordandukart
Copy link
Member

JSON-LD really should be updated in the future to follow the new suggested way of handling cacheability.

Unrelated to this PR in general.

islandora.module Outdated Show resolved Hide resolved
Copy link
Member

@jordandukart jordandukart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me after discussion/testing. Tests are failing due to PHP8.1 deprecations now only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants