class ConfigurableResourceType in JSON:API Extras 8.3
Same name and namespace in other branches
- 8 src/ResourceType/ConfigurableResourceType.php \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType
- 8.2 src/ResourceType/ConfigurableResourceType.php \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType
Defines a configurable resource type.
Hierarchy
- class \Drupal\jsonapi\ResourceType\ResourceType
- class \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType uses DependencySerializationTrait
Expanded class hierarchy of ConfigurableResourceType
4 files declare their use of ConfigurableResourceType
- EntityResource.php in modules/
jsonapi_defaults/ src/ Controller/ EntityResource.php - ResourceIdentifierNormalizer.php in src/
Normalizer/ ResourceIdentifierNormalizer.php - ResourceObjectNormalizer.php in src/
Normalizer/ ResourceObjectNormalizer.php - SchemaFieldDefinitionNormalizer.php in src/
Normalizer/ SchemaFieldDefinitionNormalizer.php
File
- src/
ResourceType/ ConfigurableResourceType.php, line 15
Namespace
Drupal\jsonapi_extras\ResourceTypeView source
class ConfigurableResourceType extends ResourceType {
use DependencySerializationTrait;
/**
* 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;
/**
* The static cache.
*
* @var array
*/
protected $cache = [];
/**
* Returns the jsonapi_resource_config.
*
* @return \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig
* The jsonapi_resource_config entity.
*/
public function getJsonapiResourceConfig() {
return $this->jsonapiResourceConfig;
}
/**
* Sets the jsonapi_resource_config.
*
* @param \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig $resource_config
* The jsonapi_resource_config entity.
*/
public function setJsonapiResourceConfig(JsonapiResourceConfig $resource_config) {
$this->jsonapiResourceConfig = $resource_config;
if ($name = $resource_config
->get('resourceType')) {
// Set the type name.
$this->typeName = $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 '/' . ltrim($config_path, '/');
}
/**
* Get the resource field configuration.
*
* @todo https://www.drupal.org/node/3007820
*
* @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') {
$cid = "{$field_name}:{$from}";
if (isset($this->cache[$cid]) || array_key_exists($cid, $this->cache)) {
return $this->cache[$cid];
}
$resource_fields = $this
->getJsonapiResourceConfig()
->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];
});
$result = empty($found) ? NULL : reset($found);
$this->cache[$cid] = $result;
return $result;
}
/**
* Injects the config factory.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The field enhancer manager.
*/
public function setConfigFactory(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}
/**
* Injects the field enhancer manager.
*
* @param \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager $enhancer_manager
* The field enhancer manager.
*/
public function setEnhancerManager(ResourceFieldEnhancerManager $enhancer_manager) {
$this->enhancerManager = $enhancer_manager;
}
/**
* 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 (PluginException $exception) {
return NULL;
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigurableResourceType:: |
protected | property | The static cache. | |
ConfigurableResourceType:: |
protected | property | The configuration factory. | |
ConfigurableResourceType:: |
protected | property | Plugin manager for enhancers. | |
ConfigurableResourceType:: |
protected | property | The JsonapiResourceConfig entity. | |
ConfigurableResourceType:: |
public | function | Get the field enhancer plugin. | |
ConfigurableResourceType:: |
public | function | Returns the jsonapi_resource_config. | |
ConfigurableResourceType:: |
public | function |
Get the resource path. Overrides ResourceType:: |
|
ConfigurableResourceType:: |
public | function | Get the resource field configuration. | |
ConfigurableResourceType:: |
public | function |
Determine whether to include a collection count. Overrides ResourceType:: |
|
ConfigurableResourceType:: |
public | function | Injects the config factory. | |
ConfigurableResourceType:: |
public | function | Injects the field enhancer manager. | |
ConfigurableResourceType:: |
public | function | Sets the jsonapi_resource_config. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
ResourceType:: |
protected | property | The bundle ID. | |
ResourceType:: |
protected | property | The class to which a payload converts to. | |
ResourceType:: |
protected | property | The entity type ID. | |
ResourceType:: |
protected | property | The mapping for field aliases: keys=public names, values=internal names. | |
ResourceType:: |
protected | property | The list of fields on the underlying entity type + bundle. | |
ResourceType:: |
protected | property | Whether this resource type is internal. | |
ResourceType:: |
protected | property | Whether this resource type's resources are locatable. | |
ResourceType:: |
protected | property | Whether this resource type's resources are mutable. | |
ResourceType:: |
protected | property | Whether this resource type's resources are versionable. | |
ResourceType:: |
protected | property | An array of arrays of relatable resource types, keyed by public field name. | |
ResourceType:: |
protected | property | The type name. | |
ResourceType:: |
public | function | Gets the bundle. | |
ResourceType:: |
public | function | Gets the deserialization target class. | |
ResourceType:: |
public | function | Gets the entity type ID. | |
ResourceType:: |
public | function | Gets a particular attribute or relationship field by internal field name. | |
ResourceType:: |
public | function | Gets a particular attribute or relationship field by public field name. | |
ResourceType:: |
public | function | Gets the attribute and relationship fields of this resource type. | |
ResourceType:: |
public | function | Translates the public field name to the entity field name. | |
ResourceType:: |
public | function | Translates the entity field name to the public field name. | |
ResourceType:: |
public | function | Get all resource types with which this type may have a relationship. | |
ResourceType:: |
public | function | Get all resource types with which the given field may have a relationship. | |
ResourceType:: |
public | function | Gets the type name. | |
ResourceType:: |
public | function | Checks if the field exists. | |
ResourceType:: |
public | function | Checks if a field is enabled or not. | |
ResourceType:: |
public | function | Whether this resource type is internal. | |
ResourceType:: |
public | function | Whether resources of this resource type are locatable. | |
ResourceType:: |
public | function | Whether resources of this resource type are mutable. | |
ResourceType:: |
public | function | Whether resources of this resource type are versionable. | |
ResourceType:: |
public | function | Sets the relatable resource types. | |
ResourceType:: |
private | function | Takes a deprecated field mapping and converts it to ResourceTypeFields. | |
ResourceType:: |
public | function | Instantiates a ResourceType object. | |
ResourceType:: |
public | function |