abstract class ResourceEntity in RESTful 7.2
Hierarchy
- class \Drupal\restful\Plugin\resource\Resource extends \Drupal\Component\Plugin\PluginBase implements ResourceInterface uses ConfigurablePluginTrait
- class \Drupal\restful\Plugin\resource\ResourceEntity
Expanded class hierarchy of ResourceEntity
11 files declare their use of ResourceEntity
- Articles__1_0.php in modules/
restful_example/ src/ Plugin/ resource/ node/ article/ v1/ Articles__1_0.php - Contains \Drupal\restful_example\Plugin\resource\node\article\v1\Articles__1_0.
- Articles__1_1.php in modules/
restful_example/ src/ Plugin/ resource/ node/ article/ v1/ Articles__1_1.php - Contains \Drupal\restful_example\Plugin\resource\node\article\v1\Articles__1_1.
- Comments__1_0.php in modules/
restful_example/ src/ Plugin/ resource/ comment/ Comments__1_0.php - Contains \Drupal\restful_example\Plugin\resource\comment\Comments__1_0.
- DataProviderEntity.php in src/
Plugin/ resource/ DataProvider/ DataProviderEntity.php - Contains \Drupal\restful\Plugin\resource\DataProvider\DataProviderEntity.
- EntityTests__1_0.php in tests/
modules/ restful_test/ src/ Plugin/ resource/ entity_test/ EntityTests__1_0.php - Contains \Drupal\restful_test\Plugin\resource\entity_test\EntityTests__1_0.
File
- src/
Plugin/ resource/ ResourceEntity.php, line 18 - Contains \Drupal\restful\Plugin\resource\ResourceEntity.
Namespace
Drupal\restful\Plugin\resourceView source
abstract class ResourceEntity extends Resource {
/**
* The entity type.
*
* @var string
*/
protected $entityType;
/**
* The entity bundles.
*
* @var array
*/
protected $bundles = array();
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
if (empty($plugin_definition['dataProvider']['entityType'])) {
throw new InternalServerErrorException('The entity type was not provided.');
}
$this->entityType = $plugin_definition['dataProvider']['entityType'];
if (isset($plugin_definition['dataProvider']['bundles'])) {
$this->bundles = $plugin_definition['dataProvider']['bundles'];
}
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* Data provider factory.
*
* @return DataProviderEntityInterface
* The data provider for this resource.
*
* @throws ServerConfigurationException
*/
public function dataProviderFactory() {
$plugin_definition = $this
->getPluginDefinition();
$field_definitions = $this
->getFieldDefinitions();
if (!empty($plugin_definition['dataProvider']['viewMode'])) {
$field_definitions_array = $this
->viewModeFields($plugin_definition['dataProvider']['viewMode']);
$field_definitions = ResourceFieldCollection::factory($field_definitions_array, $this
->getRequest());
}
$class_name = $this
->dataProviderClassName();
if (!class_exists($class_name)) {
throw new ServerConfigurationException(sprintf('The DataProvider could not be found for this resource: %s.', $this
->getResourceMachineName()));
}
return new $class_name($this
->getRequest(), $field_definitions, $this
->getAccount(), $this
->getPluginId(), $this
->getPath(), $plugin_definition['dataProvider']);
}
/**
* Data provider class.
*
* @return string
* The name of the class of the provider factory.
*/
protected function dataProviderClassName() {
// This helper function allows to map a resource to a different data
// provider class.
if ($this
->getEntityType() == 'node') {
return '\\Drupal\\restful\\Plugin\\resource\\DataProvider\\DataProviderNode';
}
elseif ($this
->getEntityType() == 'taxonomy_term') {
return '\\Drupal\\restful\\Plugin\\resource\\DataProvider\\DataProviderTaxonomyTerm';
}
elseif ($this
->getEntityType() == 'file') {
return '\\Drupal\\restful\\Plugin\\resource\\DataProvider\\DataProviderFile';
}
return '\\Drupal\\restful\\Plugin\\resource\\DataProvider\\DataProviderEntity';
}
/**
* {@inheritdoc}
*/
protected function publicFields() {
$public_fields = array();
$public_fields['id'] = array(
'wrapper_method' => 'getIdentifier',
'wrapper_method_on_entity' => TRUE,
'methods' => array(
RequestInterface::METHOD_GET,
RequestInterface::METHOD_OPTIONS,
),
'discovery' => array(
// Information about the field for human consumption.
'info' => array(
'label' => t('ID'),
'description' => t('Base ID for the entity.'),
),
// Describe the data.
'data' => array(
'cardinality' => 1,
'read_only' => TRUE,
'type' => 'integer',
'required' => TRUE,
),
),
);
$public_fields['label'] = array(
'wrapper_method' => 'label',
'wrapper_method_on_entity' => TRUE,
'discovery' => array(
// Information about the field for human consumption.
'info' => array(
'label' => t('Label'),
'description' => t('The label of the resource.'),
),
// Describe the data.
'data' => array(
'type' => 'string',
),
// Information about the form element.
'form_element' => array(
'type' => 'textfield',
'size' => 255,
),
),
);
$public_fields['self'] = array(
'callback' => array(
$this,
'getEntitySelf',
),
);
return $public_fields;
}
/**
* Gets the entity type.
*
* @return string
* The entity type.
*/
public function getEntityType() {
return $this->entityType;
}
/**
* Gets the entity bundle.
*
* @return array
* The bundles.
*/
public function getBundles() {
return $this->bundles;
}
/**
* Get the "self" url.
*
* @param DataInterpreterInterface $interpreter
* The wrapped entity.
*
* @return string
* The self URL.
*/
public function getEntitySelf(DataInterpreterInterface $interpreter) {
return $this
->versionedUrl($interpreter
->getWrapper()
->getIdentifier());
}
/**
* Get the public fields with the default values applied to them.
*
* @param array $field_definitions
* The field definitions to process.
*
* @throws \Drupal\restful\Exception\ServerConfigurationException
* For resources without ID field.
*
* @return array
* The field definition array.
*/
protected function processPublicFields(array $field_definitions) {
// The fields that only contain a property need to be set to be
// ResourceFieldEntity. Otherwise they will be considered regular
// ResourceField.
return array_map(function ($field_definition) {
$field_entity_class = '\\Drupal\\restful\\Plugin\\resource\\Field\\ResourceFieldEntity';
$class_name = ResourceFieldEntity::fieldClassName($field_definition);
if (!$class_name || is_subclass_of($class_name, $field_entity_class)) {
$class_name = $field_entity_class;
}
return $field_definition + array(
'class' => $class_name,
'entityType' => $this
->getEntityType(),
);
}, $field_definitions);
}
/**
* Get the public fields with default values based on view mode information.
*
* @param array $view_mode_info
* View mode configuration array.
*
* @return array
* The public fields.
*
* @throws ServerConfigurationException
*/
protected function viewModeFields(array $view_mode_info) {
$field_definitions = array();
$entity_type = $this
->getEntityType();
$bundles = $this
->getBundles();
$view_mode = $view_mode_info['name'];
if (count($bundles) != 1) {
throw new ServerConfigurationException('View modes can only be used in resources with a single bundle.');
}
$bundle = reset($bundles);
foreach ($view_mode_info['fieldMap'] as $field_name => $public_field_name) {
$field_instance = field_info_instance($entity_type, $field_name, $bundle);
$formatter_info = $field_instance['display'][$view_mode];
unset($formatter_info['module']);
unset($formatter_info['weight']);
unset($formatter_info['label']);
$field_definitions[$public_field_name] = array(
'property' => $field_name,
'formatter' => $formatter_info,
'entityType' => $this
->getEntityType(),
);
}
return $field_definitions;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigurablePluginTrait:: |
protected | property | Plugin instance configuration. | |
ConfigurablePluginTrait:: |
public | function | ||
ConfigurablePluginTrait:: |
public | function | ||
ConfigurablePluginTrait:: |
public | function | ||
Resource:: |
protected | property | The authentication manager. | |
Resource:: |
protected | property | The data provider. | |
Resource:: |
protected | property | Indicates if the resource is enabled. | |
Resource:: |
protected | property | The field definition object. | |
Resource:: |
protected | property | The requested path. | |
Resource:: |
protected | property | The current request. | |
Resource:: |
public | function |
Determine if user can access the handler. Overrides ResourceInterface:: |
1 |
Resource:: |
protected | function | Checks access based on the referer header and the allowOrigin setting. | |
Resource:: |
public | function |
Gets the controllers. Overrides ResourceInterface:: |
6 |
Resource:: |
public | function |
Basic implementation for create. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Overrides ConfigurablePluginTrait:: |
|
Resource:: |
public | function |
Disable the resource. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Discovery controller callback. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Shorthand method to perform a quick DELETE request. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Shorthand method to perform a quick GET request. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Shorthand method to perform a quick PATCH request. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Shorthand method to perform a quick POST request. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Shorthand method to perform a quick PUT request. Overrides ResourceInterface:: |
|
Resource:: |
private | function | ||
Resource:: |
public | function |
Enable the resource. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Get the user from for request. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Return the controller for a given path. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Gets the controllers for this resource. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Gets the data provider. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Gets the field definitions. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Gets the path of the resource. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Get the request object. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Gets the resource machine name. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Gets the resource name. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Helper method; Get the URL of the resource and query strings. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Return array keyed with the major and minor version of the resource. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Basic implementation for listing. Overrides ResourceInterface:: |
1 |
Resource:: |
protected | function | Initializes the authentication manager and adds the appropriate providers. | |
Resource:: |
public | function |
Checks if the resource is enabled. Overrides ResourceInterface:: |
|
Resource:: |
protected | function | Adds the Allowed-Origin headers. | |
Resource:: |
public | function |
Controller function that passes the data along and executes right action. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Basic implementation for update. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Basic implementation for update. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Sets the data provider. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Sets the field definitions. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Sets the path of the resource. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Sets the plugin definition to the provided array. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Sets the request object. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Switches the user back from the original user for the session. Overrides ResourceInterface:: |
1 |
Resource:: |
public | function |
Basic implementation for update. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Gets a resource URL based on the current version. Overrides ResourceInterface:: |
|
Resource:: |
public | function |
Basic implementation for view. Overrides ResourceInterface:: |
|
ResourceEntity:: |
protected | property | The entity bundles. | |
ResourceEntity:: |
protected | property | The entity type. | |
ResourceEntity:: |
protected | function |
Data provider class. Overrides Resource:: |
3 |
ResourceEntity:: |
public | function |
Data provider factory. Overrides Resource:: |
|
ResourceEntity:: |
public | function | Gets the entity bundle. | |
ResourceEntity:: |
public | function | Get the "self" url. | |
ResourceEntity:: |
public | function | Gets the entity type. | |
ResourceEntity:: |
protected | function |
Get the public fields with the default values applied to them. Overrides Resource:: |
2 |
ResourceEntity:: |
protected | function |
Public fields. Overrides Resource:: |
20 |
ResourceEntity:: |
protected | function | Get the public fields with default values based on view mode information. | |
ResourceEntity:: |
public | function |
Constructs a Drupal\Component\Plugin\PluginBase object. Overrides Resource:: |
2 |
ResourceInterface:: |
constant | The string that separates multiple ids. |