You are here

function uuid_link_tokens in UUID Link 7

Implements hook_tokens().

File

./uuid_link.module, line 153
Provides a filter and UI for adding links to entities that are not affected by changes in URL alias.

Code

function uuid_link_tokens($type, $tokens, array $data = array(), array $options = array()) {
  if ($type != 'uuid-link') {
    return array();
  }
  static $seen_uuids = array();
  static $replacements = array();
  $entity_types = entity_get_info();
  foreach ($entity_types as $entity_type => $info) {
    if (empty($info['entity keys']['label']) || empty($info['entity keys']['uuid'])) {
      continue;
    }
    if ($uuid_link_tokens = token_find_with_prefix($tokens, $entity_type)) {
      foreach ($uuid_link_tokens as $uuid => $original) {

        // Prevent infinite loops and wasted lookups.
        if (isset($replacements[$original])) {
          continue;
        }
        if ($entity_type == 'node') {

          // We create a dummy node with only nid and language, as an attempt
          // to get a working URL for the node.
          $dummy_node = db_select('node', 'n')
            ->fields('n', array(
            'nid',
            'language',
          ))
            ->condition($info['entity keys']['uuid'], $uuid)
            ->execute()
            ->fetchObject();
          if (!empty($dummy_node)) {
            $replacements[$original] = _uuid_link_entity_url(node_uri($dummy_node), $dummy_node);
            continue;
          }
        }
        else {

          // Prevent infinite loops for non-nodes. This has the potential to
          // result in failed token replacements in the case of circular references.
          if (isset($seen_uuids[$uuid])) {
            continue;
          }
          $seen_uuids[$uuid] = TRUE;
          $entities = entity_uuid_load($entity_type, array(
            $uuid,
          ));
          $entity = array_shift($entities);

          // If the entity we just loaded itself has a [uuid-link] token for another entity which
          // in turn has a [uuid-link] token for the original entity, (ie. a circular reference)
          // then the value will already have been set in the static $replacements[$original], so we
          // don't need to load it.
          if (isset($replacements[$original])) {
            continue;
          }
        }
        if (!empty($entity)) {
          $replacements[$original] = uuid_link_entity_url($entity_type, $entity);
        }
        else {

          // Set link URL to this hash, so the link tag can be removed later
          // in the filter process function.
          $replacements[$original] = '#uuid-link-not-found';
          watchdog('uuid_link', 'This content contains a link to content that no longer exists. Please edit the page and remove or change this link to get rid of this notice.');
        }
      }
    }
  }

  // Return replacements only for the tokens it was asked for.
  return array_intersect_key($replacements, array_flip($tokens));
}