Skip to content
Snippets Groups Projects
Commit 75bb6a24 authored by Björn Brala's avatar Björn Brala
Browse files

Include ResourceTypeBuildEvents in the repository

parent 1f4fed9a
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,8 @@ use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\jsonapi\ResourceType\ResourceTypeBuildEvent;
use Drupal\jsonapi\ResourceType\ResourceTypeBuildEvents;
use Drupal\jsonapi\ResourceType\ResourceTypeRepository;
use Drupal\jsonapi_extras\Entity\JsonapiResourceConfig;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager;
......@@ -138,17 +140,34 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
);
$resource_config = $this->getResourceConfig($resource_config_id);
// Make sure to respect the overrides coming from JSON:API if there is no
// input in JSON:API Extras.
$field_names = $this->getAllFieldNames($entity_type, $bundle);
$fields = $this->getFields($field_names, $entity_type, $bundle);
// Create subclassed ResourceType object with the same parameters as the
// parent implementation.
$internalize_resource_type = $entity_type->isInternal();
if (!$internalize_resource_type) {
$event = ResourceTypeBuildEvent::createFromEntityTypeAndBundle($entity_type, $bundle, $fields);
$this->eventDispatcher->dispatch($event, ResourceTypeBuildEvents::BUILD);
$internalize_resource_type = $event->resourceTypeShouldBeDisabled();
$fields = $event->getFields();
}
$fields = $this->overrideFields($resource_config, $fields);
$resource_type = new ConfigurableResourceType(
$entity_type->id(),
$bundle,
$entity_type->getClass(),
$entity_type->isInternal() || (bool) $resource_config->get('disabled'),
$internalize_resource_type || (bool) $resource_config->get('disabled'),
static::isLocatableResourceType($entity_type, $bundle),
TRUE,
static::isVersionableResourceType($entity_type),
$this->overrideFields($resource_config)
$fields
);
// Inject additional services through setters. By using setter injection
......@@ -197,7 +216,7 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
if (!$this->resourceConfigs) {
$resource_config_ids = [];
foreach ($this->getEntityTypeBundleTuples() as $tuple) {
list($entity_type_id, $bundle) = $tuple;
[$entity_type_id, $bundle] = $tuple;
$resource_config_ids[] = static::buildResourceConfigId(
$entity_type_id,
$bundle
......@@ -249,13 +268,13 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function overrideFields(JsonapiResourceConfig $resource_config) {
protected function overrideFields(JsonapiResourceConfig $resource_config, $fields) {
// This is not ideal, but we cannot load the resource type to get the entity
// type object. That is because this is used during the creation of the
// ResourceType.
list($entity_type_id, $bundle) = explode('--', $resource_config->getOriginalId());
[$entity_type_id, $bundle] = explode('--', $resource_config->getOriginalId());
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
$field_names = $this->getAllFieldNames($entity_type, $bundle);
// Use the base class to fetch the non-configurable field mappings.
$mappings = $resource_config->getFieldMapping();
// Ignore all the fields that don't have aliases.
......@@ -263,9 +282,6 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
return $field_info !== TRUE;
});
// Make sure to respect the overrides coming from JSON:API if there is no
// input in JSON:API Extras.
$fields = $this->getFields($field_names, $entity_type, $bundle);
foreach ($mappings as $internal_name => $mapping) {
if (!isset($fields[$internal_name])) {
continue;
......@@ -291,7 +307,7 @@ class ConfigurableResourceTypeRepository extends ResourceTypeRepository {
}
if (strpos($type_name, '--') !== FALSE) {
list($entity_type_id, $bundle) = explode('--', $type_name);
[$entity_type_id, $bundle] = explode('--', $type_name);
return static::lookupResourceType($resource_types, $entity_type_id, $bundle);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment