getFieldStorageDefinitions( $resource_type->getEntityTypeId() ); $data_internal = []; /** @var \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType $resource_type */ // Translate the public fields into the entity fields. foreach ($data as $public_field_name => $field_value) { // Skip any disabled field. if (!$resource_type->isFieldEnabled($public_field_name)) { continue; } $internal_name = $resource_type->getInternalName($public_field_name); $enhancer = $resource_type->getFieldEnhancer($public_field_name, 'publicName'); if (isset($field_storage_definitions[$internal_name])) { $field_storage_definition = $field_storage_definitions[$internal_name]; if ($field_storage_definition->getCardinality() === 1) { $field_value = $enhancer ? $enhancer->prepareForInput($field_value) : $field_value; } elseif (is_array($field_value)) { foreach ($field_value as $key => $individual_field_value) { $field_value[$key] = $enhancer ? $enhancer->prepareForInput($individual_field_value) : $individual_field_value; } } } $data_internal[$internal_name] = $field_value; } return $data_internal; } /** * {@inheritdoc} */ public function normalize($entity, $format = NULL, array $context = []) { $output = parent::normalize($entity, $format, $context); /** @var \Drupal\jsonapi\ResourceType\ResourceType $resource_type */ $resource_type = $context['resource_type']; $entity_type_id = $resource_type->getEntityTypeId(); $bundle_id = $resource_type->getBundle(); // The output depends on the configuration entity for caching. if ($resource_config = $this->getResourceConfig($entity_type_id, $bundle_id)) { $context['cacheable_metadata']->addCacheableDependency( $resource_config ); } $context['cacheable_metadata']->addCacheableDependency( \Drupal::config('jsonapi_extras.settings') ); return $output; } /** * Get the configuration entity based on the entity type and bundle. * * @param string $entity_type_id * The entity type ID. * @param string $bundle_id * The bundle ID. * * @return \Drupal\Core\Entity\EntityInterface|null * The resource config entity or NULL. */ protected function getResourceConfig($entity_type_id, $bundle_id) { $id = sprintf('%s--%s', $entity_type_id, $bundle_id); // TODO: Inject this service. $resource_config = \Drupal::entityTypeManager() ->getStorage('jsonapi_resource_config') ->load($id); return $resource_config; } }