ConfigurableResourceType.php in JSON:API Extras 8
File
src/ResourceType/ConfigurableResourceType.php
View source
<?php
namespace Drupal\jsonapi_extras\ResourceType;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\jsonapi\ResourceType\ResourceType;
use Drupal\jsonapi_extras\Entity\JsonapiResourceConfig;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager;
use Drupal\Core\Config\ConfigFactoryInterface;
class ConfigurableResourceType extends ResourceType {
protected $jsonapiResourceConfig;
protected $enhancerManager;
protected $configFactory;
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')) {
$this->typeName = $resource_config
->get('resourceType');
}
}
public function getPublicName($field_name) {
return $this
->translateFieldName($field_name, 'fieldName', 'publicName');
}
public function getInternalName($field_name) {
return $this
->translateFieldName($field_name, 'publicName', 'fieldName');
}
public function getJsonapiResourceConfig() {
return $this->jsonapiResourceConfig;
}
public function isFieldEnabled($field_name) {
$resource_field = $this
->getResourceFieldConfiguration($field_name);
return $resource_field ? empty($resource_field['disabled']) : parent::isFieldEnabled($field_name);
}
public function includeCount() {
return $this->configFactory
->get('jsonapi_extras.settings')
->get('include_count');
}
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;
}
public function getResourceFieldConfiguration($field_name, $from = 'fieldName') {
$resource_fields = $this->jsonapiResourceConfig
->get('resourceFields');
$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);
}
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'];
$settings = [];
if (!empty($enhancer_info['settings']) && is_array($enhancer_info['settings'])) {
$settings = $enhancer_info['settings'];
}
$enhancer = $this->enhancerManager
->createInstance($enhancer_info['id'], $settings);
return $enhancer;
} catch (PluginNotFoundException $exception) {
return NULL;
}
}
private function translateFieldName($field_name, $from, $to) {
$resource_field = $this
->getResourceFieldConfiguration($field_name, $from);
return empty($resource_field[$to]) ? $field_name : $resource_field[$to];
}
}