Issue #3110831 by bbrala, z3cka, smustgrave, mglaman, wim leers, alexpott, matthand: Method to enable a resource type field disabled by a previous ResourceTypeBuildEvent subscriber
parent
2a549f1645
commit
a1c6ae78c3
|
@ -151,4 +151,19 @@ class ResourceTypeBuildEvent extends Event {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables the given field on the resource type to be built.
|
||||
*
|
||||
* @param \Drupal\jsonapi\ResourceType\ResourceTypeField $field
|
||||
* The field for which to set a public name.
|
||||
*/
|
||||
public function enableField(ResourceTypeField $field): void {
|
||||
foreach ($this->fields as $index => $value) {
|
||||
if ($field === $value) {
|
||||
$this->fields[$index] = $value->enabled();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -105,6 +105,16 @@ abstract class ResourceTypeField {
|
|||
return new static($this->internalName, $this->publicName, FALSE, $this->hasOne);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a new instance of the field that is enabled.
|
||||
*
|
||||
* @return static
|
||||
* A new instance of the field that is enabled.
|
||||
*/
|
||||
public function enabled(): static {
|
||||
return new static($this->internalName, $this->publicName, TRUE, $this->hasOne);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the field is enabled.
|
||||
*
|
||||
|
|
|
@ -3,3 +3,9 @@ services:
|
|||
autoconfigure: true
|
||||
jsonapi_test_resource_type_building.build_subscriber:
|
||||
class: Drupal\jsonapi_test_resource_type_building\EventSubscriber\ResourceTypeBuildEventSubscriber
|
||||
tags:
|
||||
- { name: event_subscriber, priority: 1000 }
|
||||
jsonapi_test_resource_type_building.late_build_subscriber:
|
||||
class: Drupal\jsonapi_test_resource_type_building\EventSubscriber\LateResourceTypeBuildEventSubscriber
|
||||
tags:
|
||||
- { name: event_subscriber, priority: 999 }
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\jsonapi_test_resource_type_building\EventSubscriber;
|
||||
|
||||
use Drupal\jsonapi\ResourceType\ResourceTypeBuildEvents;
|
||||
use Drupal\jsonapi\ResourceType\ResourceTypeBuildEvent;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
/**
|
||||
* Event subscriber which tests enabling disabled resource type fields.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class LateResourceTypeBuildEventSubscriber implements EventSubscriberInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getSubscribedEvents(): array {
|
||||
return [
|
||||
ResourceTypeBuildEvents::BUILD => [
|
||||
['enableResourceTypeFields'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables any resource type fields that have been aliased by a test.
|
||||
*
|
||||
* @param \Drupal\jsonapi\ResourceType\ResourceTypeBuildEvent $event
|
||||
* The build event.
|
||||
*/
|
||||
public function enableResourceTypeFields(ResourceTypeBuildEvent $event): void {
|
||||
$aliases = \Drupal::state()->get('jsonapi_test_resource_type_builder.enabled_resource_type_fields', []);
|
||||
$resource_type_name = $event->getResourceTypeName();
|
||||
if (in_array($resource_type_name, array_keys($aliases), TRUE)) {
|
||||
foreach ($event->getFields() as $field) {
|
||||
if (isset($aliases[$resource_type_name][$field->getInternalName()]) && $aliases[$resource_type_name][$field->getInternalName()] === TRUE) {
|
||||
$event->enableField($field);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -213,6 +213,40 @@ class ResourceTypeRepositoryTest extends JsonapiKernelTestBase {
|
|||
$this->assertTrue($this->resourceTypeRepository->getByTypeName('node--page')->isFieldEnabled('uid'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that resource type fields can be re-enabled per resource type.
|
||||
*/
|
||||
public function testResourceTypeFieldEnabling(): void {
|
||||
$this->assertTrue($this->resourceTypeRepository->getByTypeName('node--article')->isFieldEnabled('uid'));
|
||||
$this->assertTrue($this->resourceTypeRepository->getByTypeName('node--page')->isFieldEnabled('uid'));
|
||||
$disabled_resource_type_fields = [
|
||||
'node--article' => [
|
||||
'uid' => TRUE,
|
||||
],
|
||||
'node--page' => [
|
||||
'uid' => TRUE,
|
||||
],
|
||||
];
|
||||
\Drupal::state()->set('jsonapi_test_resource_type_builder.disabled_resource_type_fields', $disabled_resource_type_fields);
|
||||
Cache::invalidateTags(['jsonapi_resource_types']);
|
||||
$this->assertFalse($this->resourceTypeRepository->getByTypeName('node--article')->isFieldEnabled('uid'));
|
||||
$this->assertFalse($this->resourceTypeRepository->getByTypeName('node--page')->isFieldEnabled('uid'));
|
||||
|
||||
$enabled_resource_type_fields = [
|
||||
'node--article' => [
|
||||
'uid' => TRUE,
|
||||
],
|
||||
'node--page' => [
|
||||
'uid' => TRUE,
|
||||
],
|
||||
];
|
||||
\Drupal::state()->set('jsonapi_test_resource_type_builder.enabled_resource_type_fields', $enabled_resource_type_fields);
|
||||
Cache::invalidateTags(['jsonapi_resource_types']);
|
||||
$this->assertTrue($this->resourceTypeRepository->getByTypeName('node--article')->isFieldEnabled('uid'));
|
||||
$this->assertTrue($this->resourceTypeRepository->getByTypeName('node--page')->isFieldEnabled('uid'));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that resource types can be renamed.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue