Problem/Motivation

We want to be able to disable resources by altering the JSON API resource plugin definition.

Proposed resolution

Add a enabled property in the annotation to support enabling/disabling resources.

CommentFileSizeAuthor
#2 2781459--disable-resources--2.patch5.17 KBe0ipso

Comments

e0ipso created an issue. See original summary.

e0ipso’s picture

Status: Active » Needs review
StatusFileSize
new5.17 KB

Pretty straight forward thanks to the plugin system!

  • e0ipso committed 34ce5a6 on 8.x-1.x
    Issue #2781459 by e0ipso: [FEATURE] Allow disabling resources
    
e0ipso’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

e0ipso’s picture

EDIT The following code has been deprecated, use the variant in the next comment instead to disable resources.

/**
 * Alter the resource definitions.
 *
 * @param array $plugins
 *   An array of plugin definitions.
 */
function jsonapi_jsonapi_resource_info_alter(&$plugins) {
  foreach (array_keys($plugins) as $plugin_id) {
    // Set the 'enabled' property to false if the plugin definition is not for a
    // node.
    $plugins[$plugin_id]['enabled'] = $plugins[$plugin_id]['entityType'] === 'node';
  }
}
e0ipso’s picture

The previous code has been deprecated in favor of:

/**
 * Alter the resource definitions.
 *
 * @param \Drupal\jsonapi\Configuration\ResourceConfigInterface[] $resources
 *   An array of plugin definitions.
 */
function jsonapi_jsonapi_resources_alter(&$resources) {
  foreach ($resources as &$resource) {
    // Set the 'enabled' property to false if the plugin definition is for a
    // node_type.
    if (in_array($resource->getEntityTypeId(), ['node_type', 'taxonomy_term'])) {
      $resource->disable();
    }
  }
}

This was introduced in #2807185: [BUGFIX] Handle relationships when target resource is not accessible.

shadcn’s picture