You are here

protected function JsonApiBuildSubscriber::overrideFields in JSON:API Extras 8.3

Gets the fields for the given field names and entity type + bundle.

Parameters

\Drupal\jsonapi_extras\Entity\JsonapiResourceConfig $resource_config: The associated resource config.

\Drupal\jsonapi\ResourceType\ResourceTypeBuildEvent $event: The associated resource config.

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to JsonApiBuildSubscriber::overrideFields()
JsonApiBuildSubscriber::applyResourceConfig in src/EventSubscriber/JsonApiBuildSubscriber.php
Apply resource config through the event.

File

src/EventSubscriber/JsonApiBuildSubscriber.php, line 96

Class

JsonApiBuildSubscriber
JSON API build subscriber that applies all changes from extra's to the API.

Namespace

Drupal\jsonapi_extras\EventSubscriber

Code

protected function overrideFields(JsonapiResourceConfig $resource_config, ResourceTypeBuildEvent $event) {

  // Use the base class to fetch the non-configurable field mappings.
  $mappings = $resource_config
    ->getFieldMapping();

  // Ignore all the fields that don't have aliases.
  $mappings = array_filter($mappings, function ($field_info) {
    return $field_info !== TRUE;
  });
  $fields = $event
    ->getFields();
  foreach ($mappings as $internal_name => $mapping) {
    if (!isset($fields[$internal_name])) {
      continue;
    }
    if (is_string($mapping)) {
      $event
        ->setPublicFieldName($fields[$internal_name], $mapping);
    }
    if ($mapping === FALSE) {
      $event
        ->disableField($fields[$internal_name]);
    }
  }
}