You are here

public function ConfigurableResourceType::getFieldEnhancer in JSON:API Extras 8.2

Same name and namespace in other branches
  1. 8.3 src/ResourceType/ConfigurableResourceType.php \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType::getFieldEnhancer()
  2. 8 src/ResourceType/ConfigurableResourceType.php \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType::getFieldEnhancer()

Get the field enhancer plugin.

Parameters

string $field_name: The internal field name.

string $from: The realm of the provided field name.

Return value

\Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerInterface|null The enhancer plugin. NULL if not found.

File

src/ResourceType/ConfigurableResourceType.php, line 198

Class

ConfigurableResourceType
Defines a configurable resource type.

Namespace

Drupal\jsonapi_extras\ResourceType

Code

public function getFieldEnhancer($field_name, $from = 'fieldName') {
  if (!($resource_field = $this
    ->getResourceFieldConfiguration($field_name, $from))) {
    return NULL;
  }
  if (empty($resource_field['enhancer']['id'])) {
    return NULL;
  }
  try {
    $enhancer_info = $resource_field['enhancer'];

    // Ensure that the settings are in a suitable format.
    $settings = [];
    if (!empty($enhancer_info['settings']) && is_array($enhancer_info['settings'])) {
      $settings = $enhancer_info['settings'];
    }

    // Get the enhancer instance.

    /** @var \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerInterface $enhancer */
    $enhancer = $this->enhancerManager
      ->createInstance($enhancer_info['id'], $settings);
    return $enhancer;
  } catch (PluginException $exception) {
    return NULL;
  }
}