You are here

eck_services.module in Entity Construction Kit (ECK) 7.2

Same filename and directory in other branches
  1. 7.3 modules/eck_services/eck_services.module

File

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

/**
 * Implements hook_form_alter().
 *
 * Add option to entity type form to enable service resource.
 */
function eck_services_form_eck__entity_type__form_alter(&$form, &$form_state, $form_id) {
  if (module_exists('services') && array_key_exists(0, $form_state['build_info']['args'])) {
    $entity = $form_state['build_info']['args'][0];
    if (!empty($entity)) {
      $form['enable_services'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable generic service for this entity'),
        '#default_value' => variable_get('eck_services_' . $entity . '_enable', FALSE),
      );
      $form['#submit'][] = '_eck_services_entity_type_form_submit';
    }
  }
}
function _eck_services_entity_type_form_submit($form, &$form_state) {
  if (isset($form_state['values']['enable_services'])) {
    variable_set('eck_services_' . $form_state['build_info']['args'][0] . '_enable', $form_state['values']['enable_services']);
  }
}

/**
 *  Class to allow dynamic dispatch of entity resource callbacks
 */
class eckServicesCallbacks {
  public static function __callStatic($name, $args) {
    list($entity, $method) = explode('__', $name);
    switch ($method) {
      case 'retrieve':
        return entity_load($entity, array(
          $args[0],
        ));
        break;
      case 'create':
        $new_entity = entity_create($entity, $args[0]);
        return entity_save($entity, $new_entity);
        break;
      case 'update':
        $update_entity = array_shift(entity_load($entity, array(
          $args[0],
        )));
        $update_entity = (object) array_merge((array) $update_entity, (array) $args[1]);
        return entity_save($entity, $update_entity);
        break;
      case 'delete':
        return entity_delete($entity, $args[0]);
    }
  }

}

/**
 * Define a services resource for each enabled entity
 */
function eck_services_services_resources() {
  $resources = array();
  foreach (EntityType::loadAll() as $entity) {
    if (variable_get('eck_services_' . $entity->name . '_enable', FALSE)) {
      $resources[$entity->name] = array(
        'operations' => array(
          'retrieve' => array(
            'callback' => "eckServicesCallbacks::{$entity->name}__retrieve",
            'access arguments' => array(
              'administer content',
            ),
            'args' => array(
              array(
                'name' => 'id',
                'type' => 'int',
                'source' => array(
                  'path' => 0,
                ),
                'optional' => FALSE,
              ),
            ),
          ),
          'update' => array(
            'callback' => "eckServicesCallbacks::{$entity->name}__update",
            'access arguments' => array(
              'administer content',
            ),
            'args' => array(
              array(
                'name' => 'id',
                'type' => 'int',
                'source' => array(
                  'path' => 0,
                ),
                'optional' => FALSE,
              ),
              array(
                'name' => 'data',
                'type' => 'struct',
                'source' => 'data',
                'optional' => FALSE,
              ),
            ),
          ),
          'create' => array(
            'callback' => "eckServicesCallbacks::{$entity->name}__create",
            'access arguments' => array(
              'administer content',
            ),
            'args' => array(
              array(
                'name' => 'data',
                'type' => 'struct',
                'source' => 'data',
                'optional' => FALSE,
              ),
            ),
          ),
          'delete' => array(
            'callback' => "eckServicesCallbacks::{$entity->name}__delete",
            'access arguments' => array(
              'administer content',
            ),
            'args' => array(
              array(
                'name' => 'id',
                'type' => 'int',
                'source' => array(
                  'path' => 0,
                ),
                'optional' => FALSE,
              ),
            ),
          ),
        ),
      );
    }
  }
  return $resources;
}

Functions

Classes

Namesort descending Description
eckServicesCallbacks Class to allow dynamic dispatch of entity resource callbacks