You are here

public function RestfulDataProviderDbQuery::create in RESTful 7

Create an item from the request object.

Return value

array The structured array for the item ready to be rendered.

Overrides RestfulBase::create

File

plugins/restful/RestfulDataProviderDbQuery.php, line 457
Contains \RestfulDataProviderDbQuery

Class

RestfulDataProviderDbQuery
@file Contains \RestfulDataProviderDbQuery

Code

public function create() {
  $request = $this
    ->getRequest();
  static::cleanRequest($request);
  $save = FALSE;
  $original_request = $request;
  $public_fields = $this
    ->getPublicFields();
  $id_columns = $this
    ->getIdColumn();
  $record = array();
  foreach ($public_fields as $public_field_name => $info) {

    // Ignore passthrough public fields.
    if (!empty($info['create_or_update_passthrough'])) {
      unset($original_request[$public_field_name]);
      continue;
    }

    // If this is the primary field, skip.
    if ($this
      ->isPrimaryField($info['property'])) {
      unset($original_request[$public_field_name]);
      continue;
    }
    if (isset($request[$public_field_name])) {
      $record[$info['property']] = $request[$public_field_name];
    }
    unset($original_request[$public_field_name]);
    $save = TRUE;
  }

  // No request was sent.
  if (!$save) {
    throw new \RestfulBadRequestException('No values were sent with the request.');
  }

  // If the original request is not empty, then illegal values are present.
  if (!empty($original_request)) {
    $error_message = format_plural(count($original_request), 'Property @names is invalid.', 'Property @names are invalid.', array(
      '@names' => implode(', ', array_keys($original_request)),
    ));
    throw new \RestfulBadRequestException($error_message);
  }

  // Once the record is built, write it and view it.
  if (drupal_write_record($this
    ->getTableName(), $record)) {

    // Handle multiple id columns.
    $id_values = array();
    foreach ($id_columns as $id_column) {
      $id_values[$id_column] = $record[$id_column];
    }
    $id = implode(self::COLUMN_IDS_SEPARATOR, $id_values);
    return $this
      ->view($id);
  }
  return;
}