You are here

function sf_entity_menu in Salesforce Suite 7.2

Same name and namespace in other branches
  1. 7 sf_entity/sf_entity.module \sf_entity_menu()

Implements hook_menu(). Creates a %/salesforce menu callback for all fieldable entities.

File

sf_entity/sf_entity.module, line 12
Integrates fieldable entities with the Salesforce API.

Code

function sf_entity_menu() {
  $entities = field_info_bundles();
  $items = array();
  foreach ($entities as $entity => $bundles) {
    $info = entity_get_info($entity);

    // entity needs to be fieldable with  URI callback
    if (empty($info) || empty($info['fieldable']) || empty($info['uri callback'])) {
      continue;
    }

    // Entity uses Entity API.
    if (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {

      // Entity defines bundles.
      if (!empty($info['bundle keys']['bundle'])) {
        $stub = entity_create($entity, array(
          $info['entity keys']['id'] => 0,
          $info['bundle keys']['bundle'] => '__ENTITY_BUNDLE__',
        ));
      }
      else {
        $stub = entity_create($entity, array(
          $info['entity keys']['id'] => 0,
        ));
      }
    }
    else {
      $fake_ids = array(
        0,
        '__ENTITY_VID__',
        '__ENTITY_BUNDLE__',
      );
      if (!$info['entity keys']['revision']) {
        $fake_ids[1] = NULL;
      }
      $stub = entity_create_stub_entity($entity, $fake_ids);
    }
    $uri_callback = $info['uri callback'];
    $uri = $uri_callback($stub);

    // Don't create a local task if the uri callback could not be obtained.
    if (!isset($uri['path']) || !is_array($uri)) {
      continue;
    }
    $parts = explode('/', $uri['path']);
    $page_args = array(
      'sf_entity_salesforce_form',
      $entity,
    );
    $access_args = array(
      $entity,
    );
    foreach ($parts as $i => $part) {
      if ($part === '0') {
        $parts[$i] = '%' . $entity;
        $page_args[] = $i;
        $access_args[] = $i;
      }
      elseif ($part == '__ENTITY_VID__' || $part == '__ENTITY_BUNDLE__') {
        $parts[$i] = '%';
      }
    }
    $parts[] = 'salesforce';
    $uri = implode('/', $parts);
    $items[$uri] = array(
      'title' => 'Salesforce',
      'page callback' => 'drupal_get_form',
      'page arguments' => $page_args,
      'access callback' => 'sf_entity_salesforce_form_access',
      'access arguments' => $access_args,
      'type' => MENU_LOCAL_TASK,
    );
  }
  return $items;
}