Skip to content

Instantly share code, notes, and snippets.

@kostajh
Created March 24, 2014 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kostajh/9742462 to your computer and use it in GitHub Desktop.
Save kostajh/9742462 to your computer and use it in GitHub Desktop.
Example API for redirecting CasperJS to Drupal data.
<?php
/**
* Implements hook_menu().
*/
function example_api_menu() {
$items = array();
$items['api'] = array(
'page callback' => 'example_api_redirect',
'page arguments' => array(1, 2),
'access callback' => TRUE,
);
return $items;
}
/**
* Page callback for api/%s/%s.
*
* Redirect users from e.g. api/publications/12405 to node/2351.
*/
function example_api_redirect($type, $id) {
// Check if $type is allowed.
$allowed_types = array(
'publication',
'journal',
// etc etc
);
if (!in_array(check_plain($type), $allowed_types)) {
drupal_access_denied();
}
$id = check_plain($id);
_example_api_get_content_by_source_id($type, $id);
}
/**
* Redirects visitor to Drupal entity that matches source content type and ID.
*/
function _example_api_get_content_by_source_id($type, $id) {
$drupal_id = NULL;
$query = new EntityFieldQuery();
$admin_user = user_load(1);
$entity_type = 'node';
switch ($type) {
case 'publication':
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'publication')
->fieldCondition('field_publication_id', 'value', $id)
->addMetaData('account', $admin_user);
break;
case 'journal':
$entity_type = 'journal';
$query->entityCondition('entity_type', 'journal')
->entityCondition('bundle', 'journal')
->fieldCondition('field_journal_id', 'value', $id)
->addMetaData('account', $admin_user);
break;
// Etc etc.
}
$result = $query->execute();
if (isset($result[$entity_type])) {
// Check if there is more than one result.
$count = count($result[$entity_type]);
// Get the first item in the results.
$entity = array_shift($result[$entity_type]);
if ($count > 1) {
// Build a list of IDs of duplicate entities.
if ($entity_type == 'entity') {
$ids = implode(', ', array_keys($result[$entity_type]));
}
else {
$ids = array();
foreach ($result[$entity_type] as $item) {
$ids[] = $item->nid;
}
$ids = implode(', ', $ids);
}
drupal_set_message(t('We found more than one entity matching id %id. Other entity IDs found are %entities',
array(
'%id' => $id,
'%entities' => $ids,
)
),
'warning');
}
$drupal_id = ($entity_type == 'node') ? $entity->nid : $entity->id;
}
$success_message = t('Successfully located Drupal entity for type %type and id %id', array('%type' => $type, '%id' => $id));
if (!$drupal_id) {
drupal_set_message(t('Could not find a Drupal entity for type %type and id %id', array('%type' => $type, '%id' => $id)), 'warning');
drupal_not_found();
}
elseif ($entity_type == 'node') {
drupal_set_message($success_message, 'success');
drupal_goto('node/' . $drupal_id);
}
else {
drupal_set_message($success_message, 'success');
drupal_goto($entity_type . '/' . $entity_type . '/' . $drupal_id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment