Skip to content

Instantly share code, notes, and snippets.

@csymlstd
Last active October 25, 2019 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csymlstd/39293d17683824ab5660374008d760d5 to your computer and use it in GitHub Desktop.
Save csymlstd/39293d17683824ab5660374008d760d5 to your computer and use it in GitHub Desktop.
Drupal 8: allow users to filter custom entities on the json api
<?php
// Related to SA-CONTRIB-2018-081
// https://www.drupal.org/node/3036792
function mymodule_jsonapi_asset_filter_access(\Drupal\Core\Entity\EntityTypeInterface $entity_type, \Drupal\Core\Session\AccountInterface $account) {
return ([
JSONAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'administer asset entities'),
JSONAPI_FILTER_AMONG_PUBLISHED => AccessResult::allowedIfHasPermission($account, 'view published asset entities'),
JSONAPI_FILTER_AMONG_OWN => AccessResult::allowedIfHasPermissions($account, ['view published asset entities', 'view published asset entities'], 'AND'),
]);
}
function mymodule_jsonapi_asset_field_filter_access(\Drupal\Core\Field\FieldDefinitionInterface $field_definition, \Drupal\Core\Session\AccountInterface $account) {
return AccessResult::neutral();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment