You are here

public function ServicesResourceControllerAbstract::resourceInfo in Services Entity API 7.2

Implemented to define basic CRUD/I operations.

May be extended to add arguments to these operations, or to add additional actions or relationships.

Overrides ServicesResourceControllerInterface::resourceInfo

See also

ServicesResourceControllerInterface::resourceInfo()

File

plugins/services_entity_abstract.inc, line 122
Services Entity module integration for entities.

Class

ServicesResourceControllerAbstract
An abstract controller providing basic CRUD resource info for entities.

Code

public function resourceInfo($entity_type) {
  $info = array();
  $entity_info = entity_get_info($entity_type);
  $replacements = array(
    '@type' => $entity_info['label'],
  );

  // Create.
  if (entity_type_supports($entity_type, 'create')) {
    $info['create'] = $this
      ->getInfoElement($entity_type, 'create', 'create', FALSE, TRUE);
    $info['create']['help'] = t("Creates an entity of type @type.", $replacements);
  }

  // Retrieve.
  $info['retrieve'] = $this
    ->getInfoElement($entity_type, 'retrieve', 'view', TRUE, FALSE);
  $info['retrieve']['help'] = t("Retrieves an entity of type @type.", $replacements);
  $info['retrieve']['args'][] = array(
    'name' => 'fields',
    'optional' => TRUE,
    'type' => 'string',
    'description' => 'A comma separated list of fields to get.',
    'default value' => '*',
    'source' => array(
      'param' => 'fields',
    ),
  );
  $info['retrieve']['args'][] = array(
    'name' => 'revision',
    'optional' => TRUE,
    'type' => 'int',
    'description' => 'The specific revision to retrieve.',
    'default value' => NULL,
    'source' => array(
      'param' => 'revision',
    ),
  );

  // Update.
  if (entity_type_supports($entity_type, 'save')) {
    $info['update'] = $this
      ->getInfoElement($entity_type, 'update', 'update', TRUE, TRUE);
    $info['update']['help'] = t("Updates an entity of type @type.", $replacements);
  }

  // Delete.
  if (entity_type_supports($entity_type, 'delete')) {
    $info['delete'] = $this
      ->getInfoElement($entity_type, 'delete', 'delete', TRUE, FALSE);
    $info['delete']['help'] = t("Deletes an entity of type @type.", $replacements);
  }

  // Index.
  $info['index'] = $this
    ->getInfoElement($entity_type, 'index', 'index', FALSE, FALSE);
  $info['index']['help'] = t("Retrieves a list of entities of type @type.", $replacements);

  /**
   * Fields to return.
   *
   * These should be specified in a comma separated list like ?fields=title,created,uid
   */
  $info['index']['args'][] = array(
    'name' => 'fields',
    'optional' => TRUE,
    'type' => 'string',
    'description' => 'A comma separated list of fields to get.',
    'default value' => '*',
    'source' => array(
      'param' => 'fields',
    ),
  );

  /**
   * Filter parameters.
   *
   * These should be specified by ?parameters[title]=My Title&param[created]=4403305
   */
  $info['index']['args'][] = array(
    'name' => 'parameters',
    'optional' => TRUE,
    'type' => 'array',
    'description' => 'Filter parameters array such as parameters[title]="test"',
    'default value' => array(),
    'source' => array(
      'param' => 'parameters',
    ),
  );

  /**
   * Page number.
   *
   * A zero based page number like ?page=3 (returns the fourth page)
   */
  $info['index']['args'][] = array(
    'name' => 'page',
    'optional' => TRUE,
    'type' => 'int',
    'description' => 'The zero-based index of the page to get, defaults to 0.',
    'default value' => 0,
    'source' => array(
      'param' => 'page',
    ),
  );

  /**
   * Page Size.
   *
   * How many records per page to return. ?pagesize=20
   */
  $info['index']['args'][] = array(
    'name' => 'pagesize',
    'optional' => TRUE,
    'type' => 'int',
    'description' => 'Number of records to get per page.',
    'default value' => variable_get('services_entity_' . $entity_type . '_index_page_size', 20),
    'source' => array(
      'param' => 'pagesize',
    ),
  );

  /**
   * Sort field.
   *
   * Which field to sort on. ?sort=created
   */
  $info['index']['args'][] = array(
    'name' => 'sort',
    'optional' => TRUE,
    'type' => 'string',
    'description' => 'Field to sort by.',
    'default value' => '',
    'source' => array(
      'param' => 'sort',
    ),
  );

  /**
   * Sort Direction.
   *
   * Which direction to sort. Possible Values = "ASC|DESC" ?direction=DESC
   */
  $info['index']['args'][] = array(
    'name' => 'direction',
    'optional' => TRUE,
    'type' => 'string',
    'description' => 'Direction of the sort. ASC or DESC.',
    'default value' => 'ASC',
    'source' => array(
      'param' => 'direction',
    ),
  );
  return $info;
}