You are here

node_service.module in Services 5

The module which exposes services related to nodes

File

services/node_service/node_service.module
View source
<?php

/**
 * @file
 * The module which exposes services related to nodes
 */

/**
 * Implementation of hook_help().
 */
function node_service_help($section) {
  switch ($section) {
    case 'admin/help#services_node':
      return t('<p>Provides node methods to services applications. Requires services.module.</p>');
    case 'admin/modules#description':
      return t('Provides node methods to services applications. Requires services.module.');
  }
}
function node_service_perm() {
  return array(
    'load raw node data',
    'load formatted node data',
  );
}

/**
 * Implementation of hook_service().
 */
function node_service_service() {
  return array(
    // node.load
    array(
      '#method' => 'node.load',
      '#callback' => 'node_service_load',
      '#access callback' => 'node_service_load_access',
      '#args' => array(
        array(
          '#name' => 'nid',
          '#type' => 'int',
          '#description' => t('A node id.'),
        ),
        array(
          '#name' => 'fields',
          '#type' => 'array',
          '#optional' => TRUE,
          '#description' => t('A list of fields to return.'),
        ),
      ),
      '#return' => 'struct',
      '#help' => t('Returns a node.'),
    ),
    // node.view
    array(
      '#method' => 'node.view',
      '#callback' => 'node_service_view',
      '#access callback' => 'node_service_view_access',
      '#args' => array(
        array(
          '#name' => 'nid',
          '#type' => 'int',
          '#description' => t('A node id.'),
        ),
        array(
          '#name' => 'fields',
          '#type' => 'array',
          '#optional' => TRUE,
          '#description' => t('A list of fields to return.'),
        ),
      ),
      array(
        '#name' => 'teaser',
        '#type' => 'boolean',
        '#optional' => TRUE,
        '#description' => t('Is this a teaser version of the node?'),
      ),
      array(
        '#name' => 'page',
        '#type' => 'boolean',
        '#optional' => TRUE,
        '#description' => t('Is this a page version of the node?'),
      ),
      '#return' => 'struct',
      '#help' => t('Returns a node.'),
    ),
    // node.save
    array(
      '#method' => 'node.save',
      '#callback' => 'node_service_save',
      '#access callback' => 'node_service_save_access',
      '#args' => array(
        array(
          '#name' => 'node',
          '#type' => 'struct',
          '#description' => t('A node object. Upon creation, node object must include "type". Upon update, node object must include "nid" and "changed".'),
        ),
      ),
      '#return' => 'struct',
      '#help' => t('Save a node object into the database.'),
    ),
    // node.delete
    array(
      '#method' => 'node.delete',
      '#callback' => 'node_delete',
      '#access callback' => 'node_service_delete_access',
      '#args' => array(
        array(
          '#name' => 'nid',
          '#type' => 'int',
          '#description' => t('A node id.'),
        ),
      ),
      '#help' => t('Delete a node.'),
    ),
  );
}

/**
 * Returns a specified node.
 */
function node_service_load($nid, $fields = array()) {
  $node = services_node_load(node_load($nid), $fields);
  if (!$node) {
    return services_error(t("Could not find the node."));
  }
  return $node;
}
function node_service_view($nid, $fields = array(), $teaser = FALSE, $page = FALSE) {
  $node = services_node_load(node_build_content(node_load($nid), $teaser, $page), $fields);
  if (!$node) {
    return services_error(t("Could not find the node."));
  }
  return $node;
}
function node_service_load_access($nid) {
  $node = node_load($nid);
  return node_access('view', $node) && user_access('load raw node data');
}
function node_service_view_access($nid) {
  $node = node_load($nid);
  return node_access('view', $node) && user_access('load formatted node data');
}
function node_service_save($edit) {
  if ($edit['nid']) {
    $node = node_load($edit['nid']);
    if ($node->nid) {
      $ret = drupal_execute($node->type . '_node_form', $edit, $node);
      $nid = $node->nid;
    }
  }
  else {
    $ret = drupal_execute($edit['type'] . '_node_form', $edit, $edit);
    $nid = (int) substr($ret, strpos($ret, '/') + 1);
  }
  if ($errors = form_get_errors()) {
    return services_error(implode("\n", $errors));
  }
  return $nid;
}
function node_service_save_access($node) {
  if (isset($node['nid'])) {
    return node_access('update', $node);
  }
  return node_access('create', $node['type']);
}
function node_service_delete_access($nid) {
  $node = node_load($nid);
  return node_access('delete', $node);
}