function tripal_replace_entity_tokens

3.x tripal.entities.api.inc tripal_replace_entity_tokens($string, &$entity, $bundle_entity = NULL)

Replace all Tripal Tokens in a given string.

NOTE: If there is no value for a token then the token is removed.

Parameters

string $string: The string containing tokens.

TripalEntity $entity: The entity with field values used to find values of tokens.

TripalBundle $bundle_entity: The bundle enitity containing special values sometimes needed for token replacement.

Return value

The string will all tokens replaced with values.

Related topics

3 calls to tripal_replace_entity_tokens()
remote__data::load in tripal_ws/includes/TripalFields/remote__data/remote__data.inc
TripalEntityController::setAlias in tripal/includes/TripalEntityController.inc
Sets the URL alias for an entity.
TripalEntityController::setTitle in tripal/includes/TripalEntityController.inc
Sets the title for an entity.

File

tripal/api/tripal.entities.api.inc, line 1082
Provides an application programming interface (API) for working with TripalEntity content types (bundles) and their entities.

Code

function tripal_replace_entity_tokens($string, &$entity, $bundle_entity = NULL) {
  // Determine which tokens were used in the format string
  $used_tokens = array();
  if (preg_match_all('/\[\w+\]/', $string, $matches)) {
    $used_tokens = $matches[0];
  }

  // If there are no tokens then just return the string.
  if (count($used_tokens) == 0) {
    return $string;
  }

  // If the fields are not loaded for the entity then we want to load them
  // but we won't do a field_attach_load() as that will load all of the
  // fields. For syncing (publishing) of content loading all fields for
  // all synced entities causes extreme slowness, so we'll only attach
  // the necessary fields for replacing tokens.
  $attach_fields = array();
  foreach ($used_tokens as $token) {
    $field_name = str_replace(array('.', '[', ']'), array('__', '', ''), $token);

    if (!property_exists($entity, $field_name)) {
      $field = field_info_field($field_name);
      $storage = $field['storage'];
      $attach_fields[$storage['type']]['storage'] = $storage;
      $attach_fields[$storage['type']]['fields'][] = $field;
    }
  }

  // If we have any fields that need attaching, then do so now.
  if (count(array_keys($attach_fields)) > 0) {
    foreach ($attach_fields as $storage_type => $details) {
      $storage = $details['storage'];
      $fields = $details['fields'];
      $field_ids = array();
      foreach ($fields as $field) {
        $field_ids[$field['id']] = array($entity->id);
      }
      $entities = array($entity->id => $entity);
    }
    module_invoke($storage['module'], 'field_storage_load', 'TripalEntity', 
    $entities, FIELD_LOAD_CURRENT, $field_ids, array());
  }

  // Now that all necessary fields are attached process the tokens.
  foreach ($used_tokens as $token) {
    $field_name = str_replace(array('.', '[', ']'), array('__', '', ''), $token);
    $value = '';

    if (property_exists($entity, $field_name)) {
      // Note: there is a memory leak in field_get_items() so we can't use it
      // here or bulk publishing will slowly erode memory.
      //$field_value = field_get_items('TripalEntity', $entity, $field_name);
      if (array_key_exists(0, $entity->{$field_name}['und'])) {
        $value = $entity->{$field_name}['und'][0]['value'];
      }
      // TODO: deal with the value when it is not a scalar.
    }
    // The TripalBundle__bundle_id is a special token for substituting the
    // bundle id.
    elseif ($field_name === 'TripalBundle__bundle_id') {
      // Load the bundle entity if we weren't given it.
      if (!$bundle_entity) {
        $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
      }
      // This token should be the id of the TripalBundle.
      $value = $bundle_entity->id;
    }
    // The TripalBundle__bundle_id is a special token for substituting the
    // entty id.
    elseif ($field_name === 'TripalEntity__entity_id') {
      // This token should be the id of the TripalEntity.
      $value = $entity->id;
    }

    // We can't support tokens that have multiple elements (i.e. in an array).
    if (is_array($value)) {
      $string = str_replace($token, '', $string);
    }
    else {
      $string = str_replace($token, $value, $string);
    }
  }

  return $string;
}