123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- <?php
- use Drupal\Core\Access\AccessResult;
- use Drupal\Core\Routing\RouteMatchInterface;
- use Drupal\Core\Entity\EntityInterface;
- use Drupal\Core\Entity\EntityTypeInterface;
- use Drupal\Core\Session\AccountInterface;
- use Drupal\jsonapi\Routing\Routes as JsonApiRoutes;
- const JSONAPI_FILTER_AMONG_ALL = 'filter_among_all';
- const JSONAPI_FILTER_AMONG_PUBLISHED = 'filter_among_published';
- const JSONAPI_FILTER_AMONG_ENABLED = 'filter_among_enabled';
- const JSONAPI_FILTER_AMONG_OWN = 'filter_among_own';
- function jsonapi_help($route_name, RouteMatchInterface $route_match) {
- switch ($route_name) {
- case 'help.page.jsonapi':
- $output = '<h3>' . t('About') . '</h3>';
- $output .= '<p>' . t('The JSON:API module is a fully compliant implementation of the <a href=":spec">JSON:API Specification</a>. By following shared conventions, you can increase productivity, take advantage of generalized tooling, and focus on what matters: your application. Clients built around JSON:API are able to take advantage of features like efficient response caching, which can sometimes eliminate network requests entirely. For more information, see the <a href=":docs">online documentation for the JSON:API module</a>.', [
- ':spec' => 'https://jsonapi.org',
- ':docs' => 'https://www.drupal.org/docs/8/modules/json-api',
- ]) . '</p>';
- $output .= '<dl>';
- $output .= '<dt>' . t('General') . '</dt>';
- $output .= '<dd>' . t('JSON:API is a particular implementation of REST that provides conventions for resource relationships, collections, filters, pagination, and sorting. These conventions help developers build clients faster and encourages reuse of code.') . '</dd>';
- $output .= '<dd>' . t('The <a href=":jsonapi-docs">JSON:API</a> and <a href=":rest-docs">RESTful Web Services</a> modules serve similar purposes. <a href=":comparison">Read the comparison of the RESTFul Web Services and JSON:API modules</a> to determine the best choice for your site.', [
- ':jsonapi-docs' => 'https://www.drupal.org/docs/8/modules/json-api',
- ':rest-docs' => 'https://www.drupal.org/docs/8/core/modules/rest',
- ':comparison' => 'https://www.drupal.org/docs/8/modules/jsonapi/jsonapi-vs-cores-rest-module',
- ]) . '</dd>';
- $output .= '<dd>' . t('Some multilingual features currently do not work well with JSON:API. See the <a href=":jsonapi-docs">JSON:API multilingual support documentation</a> for more information on the current status of multilingual support.', [
- ':jsonapi-docs' => 'https://www.drupal.org/docs/8/modules/jsonapi/translations',
- ]) . '</dd>';
- $output .= '<dd>' . t('Revision support is currently read-only and only for the "Content" and "Media" entity types in JSON:API. See the <a href=":jsonapi-docs">JSON:API revision support documentation</a> for more information on the current status of revision support.', [
- ':jsonapi-docs' => 'https://www.drupal.org/docs/8/modules/jsonapi/revisions',
- ]) . '</dd>';
- $output .= '</dl>';
- return $output;
- }
- return NULL;
- }
- function jsonapi_modules_installed($modules) {
- $potential_conflicts = [
- 'content_translation',
- 'config_translation',
- 'language',
- ];
- if (!empty(array_intersect($modules, $potential_conflicts))) {
- \Drupal::messenger()->addWarning(t('Some multilingual features currently do not work well with JSON:API. See the <a href=":jsonapi-docs">JSON:API multilingual support documentation</a> for more information on the current status of multilingual support.', [
- ':jsonapi-docs' => 'https://www.drupal.org/docs/8/modules/jsonapi/translations',
- ]));
- }
- }
- function jsonapi_entity_bundle_create() {
- JsonApiRoutes::rebuild();
- }
- function jsonapi_entity_bundle_delete() {
- JsonApiRoutes::rebuild();
- }
- function jsonapi_entity_create(EntityInterface $entity) {
- if (in_array($entity->getEntityTypeId(), ['field_storage_config', 'field_config'])) {
-
- JsonApiRoutes::rebuild();
- }
- }
- function jsonapi_entity_delete(EntityInterface $entity) {
- if (in_array($entity->getEntityTypeId(), ['field_storage_config', 'field_config'])) {
-
- JsonApiRoutes::rebuild();
- }
- }
- function jsonapi_jsonapi_entity_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
-
-
-
-
-
- if ($admin_permission = $entity_type->getAdminPermission()) {
- return ([
- JSONAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, $admin_permission),
- ]);
- }
- }
- function jsonapi_jsonapi_aggregator_feed_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
-
- return ([
- JSONAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'access news feeds'),
- ]);
- }
- function jsonapi_jsonapi_block_content_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
-
-
-
- return ([
- JSONAPI_FILTER_AMONG_PUBLISHED => AccessResult::allowed(),
- ]);
- }
- function jsonapi_jsonapi_comment_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
-
-
-
- return ([
- JSONAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'administer comments'),
- JSONAPI_FILTER_AMONG_PUBLISHED => AccessResult::allowedIfHasPermission($account, 'access comments'),
- ]);
- }
- function jsonapi_jsonapi_entity_test_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
-
- return ([
- JSONAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'view test entity'),
- ]);
- }
- function jsonapi_jsonapi_file_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
-
-
-
- return ([
- JSONAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'access content'),
- ]);
- }
- function jsonapi_jsonapi_media_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
-
- return ([
- JSONAPI_FILTER_AMONG_PUBLISHED => AccessResult::allowedIfHasPermission($account, 'view media'),
- ]);
- }
- function jsonapi_jsonapi_node_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
-
- if ($account->hasPermission('bypass node access')) {
- return ([
- JSONAPI_FILTER_AMONG_ALL => AccessResult::allowed()->cachePerPermissions(),
- ]);
- }
- if (!$account->hasPermission('access content')) {
- $forbidden = AccessResult::forbidden("The 'access content' permission is required.")->cachePerPermissions();
- return ([
- JSONAPI_FILTER_AMONG_ALL => $forbidden,
- JSONAPI_FILTER_AMONG_OWN => $forbidden,
- JSONAPI_FILTER_AMONG_PUBLISHED => $forbidden,
-
-
- JSONAPI_FILTER_AMONG_ENABLED => $forbidden,
- ]);
- }
- return ([
-
- JSONAPI_FILTER_AMONG_OWN => AccessResult::allowedIfHasPermission($account, 'view own unpublished content'),
-
-
-
-
-
-
- JSONAPI_FILTER_AMONG_PUBLISHED => AccessResult::allowed()->cachePerPermissions(),
- ]);
- }
- function jsonapi_jsonapi_shortcut_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
-
-
-
-
- return ([
- JSONAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'administer shortcuts')
- ->orIf(AccessResult::allowedIfHasPermissions($account, ['access shortcuts', 'customize shortcut links'])),
- ]);
- }
- function jsonapi_jsonapi_taxonomy_term_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
-
- return ([
- JSONAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'administer taxonomy'),
- JSONAPI_FILTER_AMONG_PUBLISHED => AccessResult::allowedIfHasPermission($account, 'access content'),
- ]);
- }
- function jsonapi_jsonapi_user_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
-
-
-
- return ([
- JSONAPI_FILTER_AMONG_OWN => AccessResult::allowed(),
- JSONAPI_FILTER_AMONG_ENABLED => AccessResult::allowedIfHasPermission($account, 'access user profiles'),
- ]);
- }
- function jsonapi_jsonapi_workspace_filter_access(EntityTypeInterface $entity_type, $published, $owner, AccountInterface $account) {
-
- return ([
- JSONAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'view any workspace'),
- JSONAPI_FILTER_AMONG_OWN => AccessResult::allowedIfHasPermission($account, 'view own workspace'),
- ]);
- }
|