You are here

function services_entityreference_services_resources in Services Entity API 7.2

Implements hook_services_resources_alter()

Parameters

type $resources:

File

modules/services_entityreference/services_entityreference.module, line 8

Code

function services_entityreference_services_resources() {
  $resources = array();
  $instances = field_info_instances();
  $field_types = field_info_field_types();
  $bundles = field_info_bundles();
  foreach ($instances as $entity_type => $type_bundles) {
    foreach ($type_bundles as $bundle => $bundle_instances) {
      foreach ($bundle_instances as $field_name => $instance) {
        $field = field_info_field($field_name);
        if ($field['type'] == 'entityreference') {
          $target_entity = $field['settings']['target_type'];

          // Provide a reverse relationship for entityrefence fields: on the
          // target entity type, list the entities that point here via the given
          // field.
          if (in_array($field['settings']['handler'], array(
            'base',
            'og',
          ))) {
            $name = services_entityreference_format_plural($entity_type);
            $resources["entity_{$target_entity}"]['relationships'][$name . '_' . $field_name] = array(
              'help' => t("Display an index of @referencing entities that have a reference to a @target via field @fieldname.", array(
                '@referencing' => $entity_type,
                '@target' => $target_entity,
                '@fieldname' => $field_name,
              )),
              'access callback' => '_services_entity_access_callback',
              'access callback file' => array(
                'type' => 'inc',
                'module' => 'services_entity',
                'name' => 'services_entity.resources',
              ),
              'access arguments' => array(
                'index',
              ),
              'access arguments append' => TRUE,
              'callback' => '_services_entityreference_relationship_query',
              'args' => array(
                array(
                  'name' => 'entity_type',
                  'optional' => TRUE,
                  // Otherwise throws an error
                  'default value' => $entity_type,
                  'type' => 'string',
                  'description' => 'The type of the entity to get',
                ),
                array(
                  'name' => 'field_name',
                  'optional' => TRUE,
                  // Otherwise throws an error
                  'default value' => $field_name,
                  'type' => 'string',
                  'description' => 'The type of the entity to get',
                ),
                array(
                  'name' => $target_entity . '_id',
                  'optional' => FALSE,
                  'source' => array(
                    'path' => 0,
                  ),
                  'type' => 'int',
                  'description' => 'The ID of the entity to get',
                ),
                /**
                 * Fields to return
                 *
                 * these should be specified in a comma separated list like ?fields=title,created,uid
                 */
                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
                 */
                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)
                 */
                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
                 */
                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
                 */
                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
                 */
                array(
                  'name' => 'direction',
                  'optional' => TRUE,
                  'type' => 'string',
                  'description' => 'Direction of the sort. ASC or DESC.',
                  'default value' => 'ASC',
                  'source' => array(
                    'param' => 'direction',
                  ),
                ),
              ),
            );
          }

          // settings handler
          // Provide a forward relationship for entityrefence fields: on the host
          // entity type, list the entities that are pointed to in the given field.
          $resources["entity_{$entity_type}"]['relationships']['entityreference_' . $field_name] = array(
            'help' => t("Display an index of @referenced entities referenced via field @fieldname.", array(
              '@referenced' => $target_entity,
              '@fieldname' => $field_name,
            )),
            'access callback' => '_services_entity_access_callback',
            'access callback file' => array(
              'type' => 'inc',
              'module' => 'services_entity',
              'name' => 'services_entity.resources',
            ),
            'access arguments' => array(
              'index',
            ),
            'access arguments append' => TRUE,
            'callback' => '_services_entityreference_relationship_query_forward',
            'args' => array(
              array(
                'name' => 'entity_type',
                'optional' => TRUE,
                // Otherwise throws an error
                'default value' => $entity_type,
                'type' => 'string',
                'description' => 'The type of the entity to get',
              ),
              array(
                'name' => $target_entity . '_id',
                'optional' => FALSE,
                'source' => array(
                  'path' => 0,
                ),
                'type' => 'int',
                'description' => 'The ID of the entity to get',
              ),
              array(
                'name' => 'field_name',
                'optional' => TRUE,
                // Otherwise throws an error
                'default value' => $field_name,
                'type' => 'string',
                'description' => 'The type of the entity to get',
              ),
              /**
               * Fields to return
               *
               * these should be specified in a comma separated list like ?fields=title,created,uid
               */
              array(
                'name' => 'fields',
                'optional' => TRUE,
                'type' => 'string',
                'description' => 'A comma separated list of fields to get.',
                'default value' => '*',
                'source' => array(
                  'param' => 'fields',
                ),
              ),
            ),
          );
        }
      }
    }
  }
  return $resources;
}