You are here

function uuid_services_services_resources_alter in Universally Unique IDentifier 7

Implements hook_services_resources_alter().

Alter all resources that support UUIDs, to make use this functionality when exposing them through Services.

Since we are working with UUID enabled entities, the 'create' method is redundant. Instead, clients should do a PUT to '<entity_type>/<uuid>'. This will route through the 'update' method and create the entity if it doesn't exist. This is the most logical thing to do, since it's up to the client to generate and set the UUID on the entity.

File

uuid_services/uuid_services.module, line 43
UUID Services module functions.

Code

function uuid_services_services_resources_alter(&$resources, &$endpoint) {
  foreach (entity_get_info() as $entity_type => $entity_info) {
    if (isset($entity_info['uuid']) && $entity_info['uuid'] == TRUE && (isset($resources[$entity_type]) || variable_get('uuid_services_support_all_entity_types', FALSE))) {
      unset($resources[$entity_type]['operations']['create']);

      // Alter 'retrieve' method to use UUID enabled functions and arguments.
      $resources[$entity_type]['operations']['retrieve']['help'] = t('Retrieve %label entities based on UUID.', array(
        '%label' => $entity_info['label'],
      ));
      $resources[$entity_type]['operations']['retrieve']['callback'] = '_uuid_services_entity_retrieve';
      $resources[$entity_type]['operations']['retrieve']['access callback'] = '_uuid_services_entity_access';
      $resources[$entity_type]['operations']['retrieve']['access arguments'] = array(
        'view',
      );
      $resources[$entity_type]['operations']['retrieve']['access arguments append'] = TRUE;
      $resources[$entity_type]['operations']['retrieve']['args'] = array(
        // This argument isn't exposed in the service, only used internally..
        array(
          'name' => 'entity_type',
          'description' => t('The entity type.'),
          'type' => 'string',
          'default value' => $entity_type,
          'optional' => TRUE,
        ),
        array(
          'name' => 'uuid',
          'description' => t('The %label UUID.', array(
            '%label' => $entity_info['label'],
          )),
          'type' => 'text',
          'source' => array(
            'path' => 0,
          ),
        ),
      );

      // Alter 'update' method to use UUID enabled functions and arguments.
      $resources[$entity_type]['operations']['update']['help'] = t('Update or create %label entities based on UUID. The payload must be formatted according to the <a href="!url">OData protocol</a>.', array(
        '%label' => $entity_info['label'],
        '!url' => 'http://www.odata.org/developers/protocols',
      ));
      $resources[$entity_type]['operations']['update']['callback'] = '_uuid_services_entity_update';
      $resources[$entity_type]['operations']['update']['access callback'] = '_uuid_services_entity_access';
      $resources[$entity_type]['operations']['update']['access arguments'] = array(
        'update',
      );
      $resources[$entity_type]['operations']['update']['access arguments append'] = TRUE;
      $resources[$entity_type]['operations']['update']['args'] = array(
        // This argument isn't exposed in the service, only used internally..
        array(
          'name' => 'entity_type',
          'description' => t('The entity type.'),
          'type' => 'string',
          'default value' => $entity_type,
          'optional' => TRUE,
        ),
        array(
          'name' => 'uuid',
          'description' => t('The %label UUID.', array(
            '%label' => $entity_info['label'],
          )),
          'type' => 'text',
          'source' => array(
            'path' => 0,
          ),
        ),
        array(
          'name' => 'entity',
          'description' => t('The %label entity object.', array(
            '%label' => $entity_info['label'],
          )),
          'type' => 'struct',
          'source' => 'data',
        ),
      );

      // Alter 'delete' method to use UUID enabled functions and arguments.
      $resources[$entity_type]['operations']['delete']['help'] = t('Delete %label entities based on UUID.', array(
        '%label' => $entity_info['label'],
      ));
      $resources[$entity_type]['operations']['delete']['callback'] = '_uuid_services_entity_delete';
      $resources[$entity_type]['operations']['delete']['access callback'] = '_uuid_services_entity_access';
      $resources[$entity_type]['operations']['delete']['access arguments'] = array(
        'delete',
      );
      $resources[$entity_type]['operations']['delete']['access arguments append'] = TRUE;
      $resources[$entity_type]['operations']['delete']['args'] = array(
        // This argument isn't exposed in the service, only used internally..
        array(
          'name' => 'entity_type',
          'description' => t('The entity type.'),
          'type' => 'string',
          'default value' => $entity_type,
          'optional' => TRUE,
        ),
        array(
          'name' => 'uuid',
          'description' => t('The %label UUID.', array(
            '%label' => $entity_info['label'],
          )),
          'type' => 'text',
          'source' => array(
            'path' => 0,
          ),
        ),
      );
    }
  }
}