You are here

public function DataProviderVariable::update in RESTful 7.2

Update operation.

Parameters

mixed $identifier: The ID of thing to be updated.

mixed $object: The thing that will be set.

bool $replace: TRUE if the contents of $object will replace $identifier entirely. FALSE if only what is set in $object will replace those properties in $identifier.

Return value

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

Overrides CrudInterface::update

File

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

Class

DataProviderVariable
Class DataProviderVariable.

Namespace

Drupal\restful_example\Plugin\resource\variables

Code

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