You are here

services_entityreference.module in Services Entity API 7.2

File

modules/services_entityreference/services_entityreference.module
View source
<?php

/**
 * Implements hook_services_resources_alter()
 *
 * @param type $resources
 */
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;
}

/**
 * Perform a relationship query
 */
function _services_entityreference_relationship_query($entity_type, $field_name, $target_id, $fields, $parameters, $page, $pagesize, $sort, $direction) {
  $parameters[$field_name] = $target_id;
  $resourceclass = variable_get('services_entity_resource_class', 'ServicesEntityResourceController');
  $resource = new $resourceclass();
  return $resource
    ->index($entity_type, $fields, $parameters, $page, $pagesize, $sort, $direction);
}

/**
 * Retrieve entities in an entityreference field.
 *
 * @param $entity_type
 *  The entity type.
 * @param $entity_id
 *  The id of the entity on which the field values are present.
 * @param $field_name
 *  The name of the entityreference field.
 * @param $fields
 *  A comma-separated list of the fields to return on the returned entities, or
 *  '*' to return all fields.
 *
 * @return
 *  An array of entity data.
 */
function _services_entityreference_relationship_query_forward($entity_type, $entity_id, $field_name, $fields) {
  $resourceclass = variable_get('services_entity_resource_class', 'ServicesEntityResourceController');
  $resource = new $resourceclass();
  return $resource
    ->field($entity_type, $entity_id, $field_name, $fields);
}

/**
 * Format an entity type as plural
 */
function services_entityreference_format_plural($name) {

  // For most cases adding an 's' will suffice
  $name = $name . 's';

  // Allow other modules to change the name if they want to.
  drupal_alter('services_entityreference_format_plural', $name);
  return $name;
}

Functions

Namesort descending Description
services_entityreference_format_plural Format an entity type as plural
services_entityreference_services_resources Implements hook_services_resources_alter()
_services_entityreference_relationship_query Perform a relationship query
_services_entityreference_relationship_query_forward Retrieve entities in an entityreference field.