You are here

public function DataProviderVariable::create in RESTful 7.2

Create operation.

Parameters

mixed $object: The thing to be created.

Return value

array An array of structured data for the thing that was created.

Overrides CrudInterface::create

File

modules/restful_example/src/Plugin/resource/variables/DataProviderVariable.php, line 50
Contains \Drupal\restful_example\Plugin\resource\variables\DataProviderVariable.

Class

DataProviderVariable
Class DataProviderVariable.

Namespace

Drupal\restful_example\Plugin\resource\variables

Code

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),
  );
}