You are here

class ConfigurableResourceType in JSON:API Extras 8

Same name and namespace in other branches
  1. 8.3 src/ResourceType/ConfigurableResourceType.php \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType
  2. 8.2 src/ResourceType/ConfigurableResourceType.php \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType

Defines a configurable resource type.

Hierarchy

Expanded class hierarchy of ConfigurableResourceType

1 file declares its use of ConfigurableResourceType
SchemaFieldDefinitionNormalizer.php in src/Normalizer/SchemaFieldDefinitionNormalizer.php

File

src/ResourceType/ConfigurableResourceType.php, line 14

Namespace

Drupal\jsonapi_extras\ResourceType
View source
class ConfigurableResourceType extends ResourceType {

  /**
   * The JsonapiResourceConfig entity.
   *
   * @var \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig
   */
  protected $jsonapiResourceConfig;

  /**
   * Plugin manager for enhancers.
   *
   * @var \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager
   */
  protected $enhancerManager;

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Instantiates a ResourceType object.
   *
   * @param string $entity_type_id
   *   An entity type ID.
   * @param string $bundle
   *   A bundle.
   * @param string $deserialization_target_class
   *   The deserialization target class.
   * @param \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig $resource_config
   *   The configuration entity.
   * @param \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager $enhancer_manager
   *   Plugin manager for enhancers.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   */
  public function __construct($entity_type_id, $bundle, $deserialization_target_class, JsonapiResourceConfig $resource_config, ResourceFieldEnhancerManager $enhancer_manager, ConfigFactoryInterface $config_factory) {
    parent::__construct($entity_type_id, $bundle, $deserialization_target_class, (bool) $resource_config
      ->get('disabled'));
    $this->jsonapiResourceConfig = $resource_config;
    $this->enhancerManager = $enhancer_manager;
    $this->configFactory = $config_factory;
    if ($resource_config
      ->get('resourceType')) {

      // Set the type name.
      $this->typeName = $resource_config
        ->get('resourceType');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getPublicName($field_name) {
    return $this
      ->translateFieldName($field_name, 'fieldName', 'publicName');
  }

  /**
   * {@inheritdoc}
   */
  public function getInternalName($field_name) {
    return $this
      ->translateFieldName($field_name, 'publicName', 'fieldName');
  }

  /**
   * Returns the jsonapi_resource_config.
   *
   * @return \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig
   *   The jsonapi_resource_config entity.
   */
  public function getJsonapiResourceConfig() {
    return $this->jsonapiResourceConfig;
  }

  /**
   * {@inheritdoc}
   */
  public function isFieldEnabled($field_name) {
    $resource_field = $this
      ->getResourceFieldConfiguration($field_name);
    return $resource_field ? empty($resource_field['disabled']) : parent::isFieldEnabled($field_name);
  }

  /**
   * {@inheritdoc}
   */
  public function includeCount() {
    return $this->configFactory
      ->get('jsonapi_extras.settings')
      ->get('include_count');
  }

  /**
   * {@inheritdoc}
   */
  public function getPath() {
    $resource_config = $this
      ->getJsonapiResourceConfig();
    if (!$resource_config) {
      return parent::getPath();
    }
    $config_path = $resource_config
      ->get('path');
    if (!$config_path) {
      return parent::getPath();
    }
    return $config_path;
  }

  /**
   * Get the resource field configuration.
   *
   * @param string $field_name
   *   The internal field name.
   * @param string $from
   *   The realm of the provided field name.
   *
   * @return array
   *   The resource field definition. NULL if none can be found.
   */
  public function getResourceFieldConfiguration($field_name, $from = 'fieldName') {
    $resource_fields = $this->jsonapiResourceConfig
      ->get('resourceFields');

    // Find the resource field in the config entity for the given field name.
    $found = array_filter($resource_fields, function ($resource_field) use ($field_name, $from) {
      return !empty($resource_field[$from]) && $field_name == $resource_field[$from];
    });
    if (empty($found)) {
      return NULL;
    }
    return reset($found);
  }

  /**
   * Get the field enhancer plugin.
   *
   * @param string $field_name
   *   The internal field name.
   * @param string $from
   *   The realm of the provided field name.
   *
   * @return \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerInterface|null
   *   The enhancer plugin. NULL if not found.
   */
  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 (PluginNotFoundException $exception) {
      return NULL;
    }
  }

  /**
   * Given the internal or public field name, get the other one.
   *
   * @param string $field_name
   *   The name of the field.
   * @param string $from
   *   The realm of the provided field name.
   * @param string $to
   *   The realm of the desired field name.
   *
   * @return string
   *   The field name in the desired realm.
   */
  private function translateFieldName($field_name, $from, $to) {
    $resource_field = $this
      ->getResourceFieldConfiguration($field_name, $from);
    return empty($resource_field[$to]) ? $field_name : $resource_field[$to];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurableResourceType::$configFactory protected property The configuration factory.
ConfigurableResourceType::$enhancerManager protected property Plugin manager for enhancers.
ConfigurableResourceType::$jsonapiResourceConfig protected property The JsonapiResourceConfig entity.
ConfigurableResourceType::getFieldEnhancer public function Get the field enhancer plugin.
ConfigurableResourceType::getInternalName public function Translates the public field name to the entity field name. Overrides ResourceType::getInternalName
ConfigurableResourceType::getJsonapiResourceConfig public function Returns the jsonapi_resource_config.
ConfigurableResourceType::getPath public function Get the resource path. Overrides ResourceType::getPath
ConfigurableResourceType::getPublicName public function Translates the entity field name to the public field name. Overrides ResourceType::getPublicName
ConfigurableResourceType::getResourceFieldConfiguration public function Get the resource field configuration.
ConfigurableResourceType::includeCount public function Determine whether to include a collection count. Overrides ResourceType::includeCount
ConfigurableResourceType::isFieldEnabled public function Checks if a field is enabled or not. Overrides ResourceType::isFieldEnabled
ConfigurableResourceType::translateFieldName private function Given the internal or public field name, get the other one.
ConfigurableResourceType::__construct public function Instantiates a ResourceType object. Overrides ResourceType::__construct
ResourceType::$bundle protected property The bundle ID.
ResourceType::$deserializationTargetClass protected property The class to which a payload converts to.
ResourceType::$entityTypeId protected property The entity type ID.
ResourceType::$fieldMapping protected property The mapping for field aliases: keys=public names, values=internal names.
ResourceType::$fields protected property The list of fields on the underlying entity type + bundle.
ResourceType::$internal protected property Whether this resource type is internal.
ResourceType::$isLocatable protected property Whether this resource type's resources are locatable.
ResourceType::$isMutable protected property Whether this resource type's resources are mutable.
ResourceType::$isVersionable protected property Whether this resource type's resources are versionable.
ResourceType::$relatableResourceTypesByField protected property An array of arrays of relatable resource types, keyed by public field name.
ResourceType::$typeName protected property The type name.
ResourceType::getBundle public function Gets the bundle.
ResourceType::getDeserializationTargetClass public function Gets the deserialization target class.
ResourceType::getEntityTypeId public function Gets the entity type ID.
ResourceType::getFieldByInternalName public function Gets a particular attribute or relationship field by internal field name.
ResourceType::getFieldByPublicName public function Gets a particular attribute or relationship field by public field name.
ResourceType::getFields public function Gets the attribute and relationship fields of this resource type.
ResourceType::getRelatableResourceTypes public function Get all resource types with which this type may have a relationship.
ResourceType::getRelatableResourceTypesByField public function Get all resource types with which the given field may have a relationship.
ResourceType::getTypeName public function Gets the type name.
ResourceType::hasField public function Checks if the field exists.
ResourceType::isInternal public function Whether this resource type is internal.
ResourceType::isLocatable public function Whether resources of this resource type are locatable.
ResourceType::isMutable public function Whether resources of this resource type are mutable.
ResourceType::isVersionable public function Whether resources of this resource type are versionable.
ResourceType::setRelatableResourceTypes public function Sets the relatable resource types.
ResourceType::updateDeprecatedFieldMapping Deprecated private function Takes a deprecated field mapping and converts it to ResourceTypeFields.
ResourceType::__get public function