Commit ad45ecb3 authored by Brad Jones's avatar Brad Jones Committed by Björn Brala
Browse files

Issue #3048718 by bradjones1, richgerdes, bbrala, e0ipso: Remove schema...

Issue #3048718 by bradjones1, richgerdes, bbrala, e0ipso: Remove schema alterations for ResourceType::getPublicName() and ResourceType::isFieldEnabled()
parent 3b321b21
Loading
Loading
Loading
Loading
+8 −18
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Drupal\jsonapi_extras\Normalizer;

use Drupal\Component\Utility\NestedArray;
use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface;
use Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType;
use Drupal\schemata_json_schema\Normalizer\jsonapi\SchemataSchemaNormalizer as SchemataJsonSchemaSchemataSchemaNormalizer;

/**
@@ -42,7 +43,7 @@ class SchemataSchemaNormalizer extends SchemataJsonSchemaSchemataSchemaNormalize
      $bundle
    );

    if (!$resource_type) {
    if (!$resource_type || !$resource_type instanceof ConfigurableResourceType) {
      return $normalized;
    }

@@ -57,26 +58,15 @@ class SchemataSchemaNormalizer extends SchemataJsonSchemaSchemataSchemaNormalize
      if (!isset($root[$property_type]['required'])) {
        $root[$property_type]['required'] = [];
      }
      $required_fields = [];
      $properties = NestedArray::getValue($root, [$property_type, 'properties']);
      $properties = $properties ?: [];
      $properties = NestedArray::getValue($root, [$property_type, 'properties']) ?: [];
      foreach ($properties as $fieldname => $schema) {
        unset($properties[$fieldname]);

        if (!$resource_type->isFieldEnabled($fieldname)) {
          // If the field is disabled, do nothing after removal.
          continue;
        }
        else {
          // Otherwise, substitute the public name.
          $public_name = $resource_type->getPublicName($fieldname);
          $properties[$public_name] = $schema;
          if (in_array($fieldname, $root[$property_type]['required'])) {
            $required_fields[] = $public_name;
          }
        if ($enhancer = $resource_type->getFieldEnhancer($resource_type->getFieldByPublicName($fieldname)->getInternalName())) {
          $root[$property_type]['properties'][$fieldname] = array_merge(
            array_intersect_key($root[$property_type]['properties'][$fieldname], array_flip(['title', 'description'])),
            $enhancer->getOutputJsonSchema()
          );
        }
      }
      $root[$property_type]['required'] = $required_fields;
    }

    return $normalized;