JsonapiResourceConfig.php 3.06 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\jsonapi_extras\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
6
use Drupal\Core\Entity\EntityStorageInterface;
7
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

/**
 * Defines the JSON API Resource Config entity.
 *
 * @ConfigEntityType(
 *   id = "jsonapi_resource_config",
 *   label = @Translation("JSON API Resource Config"),
 *   handlers = {
 *     "list_builder" = "Drupal\jsonapi_extras\JsonapiResourceConfigListBuilder",
 *     "form" = {
 *       "add" = "Drupal\jsonapi_extras\Form\JsonapiResourceConfigForm",
 *       "edit" = "Drupal\jsonapi_extras\Form\JsonapiResourceConfigForm",
 *       "delete" = "Drupal\jsonapi_extras\Form\JsonapiResourceConfigDeleteForm"
 *     },
 *     "route_provider" = {
23
 *       "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider"
24 25 26 27
 *     },
 *   },
 *   config_prefix = "jsonapi_resource_config",
 *   admin_permission = "administer site configuration",
28
 *   static_cache = TRUE,
29 30 31 32 33 34
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "uuid" = "uuid"
 *   },
 *   links = {
35 36 37 38
 *     "add-form" = "/admin/config/services/jsonapi/add/{entity_type_id}/{bundle}",
 *     "edit-form" = "/admin/config/services/jsonapi/{jsonapi_resource_config}/edit",
 *     "delete-form" = "/admin/config/services/jsonapi/{jsonapi_resource_config}/delete",
 *     "collection" = "/admin/config/services/jsonapi"
39 40 41 42 43 44 45 46 47 48 49 50
 *   }
 * )
 */
class JsonapiResourceConfig extends ConfigEntityBase {

  /**
   * The JSON API Resource Config ID.
   *
   * @var string
   */
  protected $id;

51 52 53 54 55
  /**
   * {@inheritdoc}
   */
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);
56 57
    static::rebuildRoutes();
  }
58

59 60 61 62 63 64
  /**
   * {@inheritdoc}
   */
  public static function postDelete(EntityStorageInterface $storage, array $entities) {
    parent::postDelete($storage, $entities);
    static::rebuildRoutes();
65 66
  }

67 68 69 70 71
  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    parent::calculateDependencies();
72
    $id = explode('--', $this->id);
73 74
    $typeManager = $this->entityTypeManager();
    $dependency = $typeManager->getDefinition($id[0])->getBundleConfigDependency($id[1]);
75
    $this->addDependency($dependency['type'], $dependency['name']);
76
  }
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91
  /**
   * {@inheritdoc}
   */
  protected function urlRouteParameters($rel) {
    $uri_route_parameters = parent::urlRouteParameters($rel);
    // The add-form route depends on entity_type_id and bundle.
    if (in_array($rel, ['add-form'])) {
      $parameters = explode('--', $this->id);
      $uri_route_parameters['entity_type_id'] = $parameters[0];
      $uri_route_parameters['bundle'] = $parameters[1];
    }
    return $uri_route_parameters;
  }

92 93 94 95
  /**
   * Triggers rebuilding of JSON API routes.
   */
  protected static function rebuildRoutes() {
96 97 98 99
    try {
      \Drupal::service('jsonapi.resource_type.repository')->reset();
      \Drupal::service('router.builder')->setRebuildNeeded();
    }
100 101 102
    catch (ServiceNotFoundException $exception) {
      // This is intentionally empty.
    }
103 104
  }

105
}