Skip to content
Snippets Groups Projects
Verified Commit a1c6ae78 authored by Dave Long's avatar Dave Long
Browse files

Issue #3110831 by bbrala, z3cka, smustgrave, mglaman, wim leers, alexpott,...

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 2a549f16
No related branches found
No related tags found
17 merge requests!11131[10.4.x-only-DO-NOT-MERGE]: Issue ##2842525 Ajax attached to Views exposed filter form does not trigger callbacks,!8736Update the Documention As per the Function uses.,!3878Removed unused condition head title for views,!3818Issue #2140179: $entity->original gets stale between updates,!3742Issue #3328429: Create item list field formatter for displaying ordered and unordered lists,!3731Claro: role=button on status report items,!3154Fixes #2987987 - CSRF token validation broken on routes with optional parameters.,!3133core/modules/system/css/components/hidden.module.css,!2964Issue #2865710 : Dependencies from only one instance of a widget are used in display modes,!2812Issue #3312049: [Followup] Fix Drupal.Commenting.FunctionComment.MissingReturnType returns for NULL,!2062Issue #3246454: Add weekly granularity to views date sort,!10223132456: Fix issue where views instances are emptied before an ajax request is complete,!877Issue #2708101: Default value for link text is not saved,!617Issue #3043725: Provide a Entity Handler for user cancelation,!579Issue #2230909: Simple decimals fail to pass validation,!560Move callback classRemove outside of the loop,!555Issue #3202493
Pipeline #294120 passed with warnings
Pipeline: drupal

#294126

    ......@@ -151,4 +151,19 @@ public function disableField(ResourceTypeField $field) {
    }
    }
    /**
    * 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 @@ public function disabled() {
    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 }
    <?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 @@ public function testResourceTypeFieldDisabling(): void {
    $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.
    */
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment