class DataProviderVariable in RESTful 7.2
Class DataProviderVariable.
@package Drupal\restful_example\Plugin\resource\variables
Hierarchy
- class \Drupal\restful\Plugin\resource\DataProvider\DataProvider implements DataProviderInterface
- class \Drupal\restful_example\Plugin\resource\variables\DataProviderVariable implements DataProviderInterface
Expanded class hierarchy of DataProviderVariable
File
- modules/
restful_example/ src/ Plugin/ resource/ variables/ DataProviderVariable.php, line 24 - Contains \Drupal\restful_example\Plugin\resource\variables\DataProviderVariable.
Namespace
Drupal\restful_example\Plugin\resource\variablesView source
class DataProviderVariable extends DataProvider implements DataProviderInterface {
/**
* {@inheritdoc}
*/
public function __construct(RequestInterface $request, ResourceFieldCollectionInterface $field_definitions, $account, $plugin_id, $resource_path = NULL, array $options = array(), $langcode = NULL) {
parent::__construct($request, $field_definitions, $account, $plugin_id, $resource_path, $options, $langcode);
if (empty($this->options['urlParams'])) {
$this->options['urlParams'] = array(
'filter' => TRUE,
'sort' => TRUE,
'fields' => TRUE,
);
}
}
/**
* {@inheritdoc}
*/
public function count() {
return count($this
->getIndexIds());
}
/**
* {@inheritdoc}
*/
public function create($object) {
// Overly simplified update method. Search for the name and value fields,
// and set the variable.
$name_key = $this
->searchPublicFieldByProperty('name');
$value_key = $this
->searchPublicFieldByProperty('value');
if (empty($object[$name_key]) || empty($object[$value_key])) {
throw new BadRequestException('You need to provide the variable name and value.');
}
$identifier = $object[$name_key];
if (!empty($GLOBALS['conf'][$identifier])) {
throw new UnprocessableEntityException('The selected variable already exists.');
}
variable_set($identifier, $object[$value_key]);
return array(
$this
->view($identifier),
);
}
/**
* {@inheritdoc}
*/
public function view($identifier) {
$resource_field_collection = $this
->initResourceFieldCollection($identifier);
$input = $this
->getRequest()
->getParsedInput();
$limit_fields = !empty($input['fields']) ? explode(',', $input['fields']) : array();
foreach ($this->fieldDefinitions as $resource_field_name => $resource_field) {
/* @var \Drupal\restful\Plugin\resource\Field\ResourceFieldInterface $resource_field */
if ($limit_fields && !in_array($resource_field_name, $limit_fields)) {
// Limit fields doesn't include this property.
continue;
}
if (!$this
->methodAccess($resource_field) || !$resource_field
->access('view', $resource_field_collection
->getInterpreter())) {
// The field does not apply to the current method or has denied
// access.
continue;
}
$resource_field_collection
->set($resource_field
->id(), $resource_field);
}
return $resource_field_collection;
}
/**
* {@inheritdoc}
*/
public function viewMultiple(array $identifiers) {
$return = array();
foreach ($identifiers as $identifier) {
try {
$row = $this
->view($identifier);
} catch (InaccessibleRecordException $e) {
$row = NULL;
}
$return[] = $row;
}
return array_values(array_filter($return));
}
/**
* {@inheritdoc}
*/
public function update($identifier, $object, $replace = FALSE) {
// Overly simplified update method. Search for the name and value fields,
// and set the variable.
$name_key = $this
->searchPublicFieldByProperty('name');
$value_key = $this
->searchPublicFieldByProperty('value');
if (empty($object[$value_key])) {
if (!$replace) {
return array(
$this
->view($identifier),
);
}
$object[$value_key] = NULL;
}
if (!empty($object[$name_key]) && $object[$name_key] != $identifier) {
// If the variable name is changed, then remove the old one.
$this
->remove($identifier);
$identifier = $object[$name_key];
}
variable_set($identifier, $object[$value_key]);
return array(
$this
->view($identifier),
);
}
/**
* {@inheritdoc}
*/
public function remove($identifier) {
variable_del($identifier);
}
/**
* {@inheritdoc}
*/
public function getIndexIds() {
$output = array();
foreach ($GLOBALS['conf'] as $key => $value) {
$output[] = array(
'name' => $key,
'value' => $value,
);
}
// Apply filters.
$output = $this
->applyFilters($output);
$output = $this
->applySort($output);
return array_map(function ($item) {
return $item['name'];
}, $output);
}
/**
* Removes plugins from the list based on the request options.
*
* @param \Drupal\restful\Plugin\resource\ResourceInterface[] $variables
* The array of resource plugins keyed by instance ID.
*
* @return \Drupal\restful\Plugin\resource\ResourceInterface[]
* The same array minus the filtered plugins.
*
* @throws \Drupal\restful\Exception\BadRequestException
* @throws \Drupal\restful\Exception\ServiceUnavailableException
*/
protected function applyFilters(array $variables) {
$filters = $this
->parseRequestForListFilter();
// Apply the filter to the list of plugins.
foreach ($variables as $delta => $variable) {
$variable_name = $variable['name'];
// A filter on a result needs the ResourceFieldCollection representing the
// result to return.
$interpreter = $this
->initDataInterpreter($variable_name);
$this->fieldDefinitions
->setInterpreter($interpreter);
foreach ($filters as $filter) {
if (!$this->fieldDefinitions
->evalFilter($filter)) {
unset($variables[$delta]);
}
}
}
$this->fieldDefinitions
->setInterpreter(NULL);
return $variables;
}
/**
* Sorts plugins on the list based on the request options.
*
* @param \Drupal\restful\Plugin\resource\ResourceInterface[] $variables
* The array of resource plugins keyed by instance ID.
*
* @return \Drupal\restful\Plugin\resource\ResourceInterface[]
* The sorted array.
*
* @throws \Drupal\restful\Exception\BadRequestException
* @throws \Drupal\restful\Exception\ServiceUnavailableException
*/
protected function applySort(array $variables) {
if ($sorts = $this
->parseRequestForListSort()) {
uasort($variables, function ($variable1, $variable2) use ($sorts) {
$interpreter1 = $this
->initDataInterpreter($variable1['name']);
$interpreter2 = $this
->initDataInterpreter($variable2['name']);
foreach ($sorts as $key => $order) {
$property = $this->fieldDefinitions
->get($key)
->getProperty();
$value1 = $interpreter1
->getWrapper()
->get($property);
$value2 = $interpreter2
->getWrapper()
->get($property);
if ($value1 == $value2) {
continue;
}
return ($order == 'DESC' ? -1 : 1) * strcmp($value1, $value2);
}
return 0;
});
}
return $variables;
}
/**
* {@inheritdoc}
*/
protected function initDataInterpreter($identifier) {
return new DataInterpreterVariable($this
->getAccount(), new ArrayWrapper(array(
'name' => $identifier,
'value' => variable_get($identifier),
)));
}
/**
* Finds the public field name that has the provided property.
*
* @param string $property
* The property to find.
*
* @return string
* The name of the public name.
*/
protected function searchPublicFieldByProperty($property) {
foreach ($this->fieldDefinitions as $public_name => $resource_field) {
if ($resource_field
->getProperty() == $property) {
return $public_name;
}
}
return NULL;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DataProvider:: |
protected | property | The account authenticated from the request for entity access checks. | |
DataProvider:: |
protected | property | The field definitions. | |
DataProvider:: |
protected | property | Determines the language of the items that should be returned. | |
DataProvider:: |
protected | property | Array of metadata. Use this as a mean to pass info to the render layer. | |
DataProvider:: |
protected | property | User defined options. | |
DataProvider:: |
protected | property | Resource identifier. | |
DataProvider:: |
protected | property | Determines the number of items that should be returned when viewing lists. | |
DataProvider:: |
protected | property | The request | |
DataProvider:: |
protected | property | The resource path. | |
DataProvider:: |
protected | function | Adds query tags and metadata to the EntityFieldQuery. | 2 |
DataProvider:: |
public | function |
Adds the options in the provided array to the data provider options. Overrides DataProviderInterface:: |
|
DataProvider:: |
public | function |
Generates the canonical path for a given path. Overrides DataProviderInterface:: |
1 |
DataProvider:: |
public | function |
Return the discovery information for the given entity. Overrides DataProviderInterface:: |
|
DataProvider:: |
public | function |
Gets the authenticated account. Overrides DataProviderInterface:: |
|
DataProvider:: |
public | function |
Gets the entity context. Overrides DataProviderInterface:: |
3 |
DataProvider:: |
public | function |
Get the language code. Overrides DataProviderInterface:: |
|
DataProvider:: |
protected static | function | Gets the global language. | |
DataProvider:: |
public | function |
Returns the metadata collection. Overrides DataProviderInterface:: |
|
DataProvider:: |
public | function |
Gets the data provider options. Overrides DataProviderInterface:: |
|
DataProvider:: |
public | function |
Gets the range. Overrides DataProviderInterface:: |
|
DataProvider:: |
public | function |
Gets the request. Overrides DataProviderInterface:: |
|
DataProvider:: |
public | function |
Get the resource path. Overrides DataProviderInterface:: |
|
DataProvider:: |
public | function |
List operation. Overrides CrudInterface:: |
2 |
DataProvider:: |
protected | function | Initialize the empty resource field collection to bundle the output. | |
DataProvider:: |
public static | function |
Checks if the passed in string is a dot-nested field. Overrides DataProviderInterface:: |
|
DataProvider:: |
protected static | function | Check if a conjunction is valid for filtering. | 1 |
DataProvider:: |
protected static | function | Check if an operator is valid for filtering. | 1 |
DataProvider:: |
public | function |
Checks if the provided field can be used with the current method. Overrides DataProviderInterface:: |
|
DataProvider:: |
protected | function | Filter the query for list. | |
DataProvider:: |
protected | function | Parses the request object to get the pagination options. | |
DataProvider:: |
protected | function | Parses the request to get the sorting options. | |
DataProvider:: |
public static | function |
Processes the input for a filter and adds the appropriate defaults. Overrides DataProviderInterface:: |
|
DataProvider:: |
public | function |
Sets the authenticated account. Overrides DataProviderInterface:: |
|
DataProvider:: |
protected | function | Sets an HTTP header. | |
DataProvider:: |
public | function |
Sets the language code. Overrides DataProviderInterface:: |
|
DataProvider:: |
public | function |
Sets the options. Overrides DataProviderInterface:: |
|
DataProvider:: |
public | function |
Sets the range. Overrides DataProviderInterface:: |
|
DataProvider:: |
public | function |
Sets the request. Overrides DataProviderInterface:: |
|
DataProvider:: |
public | function |
Set the resource path. Overrides DataProviderInterface:: |
|
DataProviderVariable:: |
protected | function | Removes plugins from the list based on the request options. | |
DataProviderVariable:: |
protected | function | Sorts plugins on the list based on the request options. | |
DataProviderVariable:: |
public | function |
Counts the total results for the index call. Overrides CrudInterface:: |
|
DataProviderVariable:: |
public | function |
Create operation. Overrides CrudInterface:: |
|
DataProviderVariable:: |
public | function |
Returns the ID to render for the current index GET request. Overrides DataProviderInterface:: |
|
DataProviderVariable:: |
protected | function |
Get the data interpreter. Overrides DataProvider:: |
|
DataProviderVariable:: |
public | function |
Delete operation. Overrides CrudInterface:: |
|
DataProviderVariable:: |
protected | function | Finds the public field name that has the provided property. | |
DataProviderVariable:: |
public | function |
Update operation. Overrides CrudInterface:: |
|
DataProviderVariable:: |
public | function |
Read operation. Overrides CrudInterface:: |
|
DataProviderVariable:: |
public | function |
Read operation. Overrides CrudInterface:: |
|
DataProviderVariable:: |
public | function |
Constructor. Overrides DataProvider:: |