function subscriptions_tokens in Subscriptions 2.0.x
Same name and namespace in other branches
- 7 subscriptions.tokens.inc \subscriptions_tokens()
Implements hook_tokens().
Parameters
string $type:
array $tokens:
array $data:
array $options:
Return value
array
File
- ./
subscriptions.tokens.old.php, line 168 - Token callbacks for the subscriptions module.
Code
function subscriptions_tokens($type, array $tokens, array $data = array(), array $options = array()) {
global $user;
$url_options = array(
'absolute' => TRUE,
);
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->language;
}
else {
$language_code = NULL;
}
$sanitize = !empty($options['sanitize']);
$replacements = array();
if ($type == 'subs' && isset($data['subs'])) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'type':
$replacements[$original] = $data['subs']['type'];
break;
case 'manage-url':
$replacements[$original] = url('user/' . $user->uid . '/subscriptions', $url_options);
break;
case 'unsubscribe-url':
$replacements[$original] = url($data['subs']['unsubscribe_path'], $url_options);
break;
case 'subs-module':
$replacements[$original] = $data['subs']['module'];
break;
case 'subs-field':
$replacements[$original] = $data['subs']['field'];
break;
case 'subs-value':
$replacements[$original] = $data['subs']['value'];
break;
case 'forum':
if (isset($data['node']['forum_tid'])) {
$replacements[$original] = render(taxonomy_term_view(taxonomy_term_load($data['node']['forum_tid']), 'full', $language_code));
}
break;
}
}
// Chained token relationships.
if ($url_tokens = token_find_with_prefix($tokens, 'manage-url')) {
$replacements += token_generate('url', $url_tokens, array(
'path' => 'user/' . $user->uid . '/subscriptions',
), $options);
}
if ($url_tokens = token_find_with_prefix($tokens, 'unsubscribe-url')) {
$replacements += token_generate('url', $url_tokens, array(
'path' => $data['subs']['unsubscribe_path'],
), $options);
}
if (isset($data['node']->forum_tid) && ($forum_tokens = token_find_with_prefix($tokens, 'forum'))) {
$replacements += token_generate('term', $forum_tokens, array(
'term' => taxonomy_term_load($data['node']->forum_tid),
), $options);
}
if (isset($data['subs']['items']) && ($items_tokens = token_find_with_prefix($tokens, 'items'))) {
$replacements += token_generate('subs_list<subs_item>', $items_tokens, array(
'subs_list<subs_item>' => $data['subs']['items'],
), $options);
}
}
// subs_item tokens.
if ($type == 'subs_item' && isset($data['subs_item'])) {
foreach ($tokens as $name => $original) {
if ($name == ':' || $name == 'formatted') {
// Plain [subs:item:?] token: return the formatted item.
$replacements[$original] = $data['subs_item']['subs']['formatted_item'];
}
elseif (($subs_tokens = token_find_with_prefix($tokens, 'subs')) && !empty($data['subs_item']['subs']) && is_array($data['subs_item']['subs'])) {
// Return the Subscriptions information of the item.
$replacements += token_generate('subs', $subs_tokens, $data['subs_item'], $options);
}
elseif (preg_match('/is-([a-z_]*)/', $name, $match) && !in_array($xtype = $match[1], array(
'new',
'updated',
'old',
'published',
))) {
// Type query, like 'is-node': verify directly.
$replacements[$original] = !empty($data['subs_item'][$xtype]) && is_object($data['subs_item'][$xtype]);
}
elseif (preg_match('/as-([a-z_]*)/', $name, $match)) {
$xtype = $match[1];
if (($xtype_tokens = token_find_with_prefix($tokens, "as-{$xtype}")) && !empty($data['subs_item'][$xtype]) && is_object($data['subs_item'][$xtype])) {
// Cast, like 'as-node': pass on as the detected type.
$replacements += token_generate($xtype, $xtype_tokens, $data['subs_item'], $options);
}
}
else {
}
}
}
// subs_list<TYPE> tokens.
// We've tried to contribute this code to Token module, see
// http://drupal.org/node/1195874#comment-5070144
if (preg_match('/subs_list<([a-z_]*)>/', $type, $match) && !empty($data[$type]) && is_array($data[$type])) {
$array_type = $type;
$element_type = $match[1];
$array = $data[$array_type];
$sort = isset($options['array sort']) ? $options['array sort'] : TRUE;
$keys = element_children($array, $sort);
foreach ($tokens as $name => $original) {
if ($name == ':') {
// Plain [subs_list<>] token.
switch ($element_type) {
case 'term':
foreach ($array as $term) {
$term_names[] = $sanitize ? check_plain($term->name) : $term->name;
}
$replacements[$original] = implode(', ', $term_names);
break;
}
continue;
}
switch ($name) {
case 'first':
$value = $array[$keys[0]];
$value = is_array($value) ? render($value) : (string) $value;
$replacements[$original] = $sanitize ? check_plain($value) : $value;
break;
case 'last':
$value = $array[$keys[count($keys) - 1]];
$value = is_array($value) ? render($value) : (string) $value;
$replacements[$original] = $sanitize ? check_plain($value) : $value;
break;
case 'count':
$replacements[$original] = count($keys);
break;
case 'count-1':
$replacements[$original] = count($keys) - 1;
break;
case 'keys':
$replacements[$original] = token_render_array($keys, $options);
break;
case 'reversed':
$reversed = array_reverse($array, TRUE);
$replacements[$original] = token_render_array($reversed, $options);
break;
case 'join':
$replacements[$original] = token_render_array($array, array(
'join' => '',
) + $options);
break;
}
}
// [subs_list<>:key:*] dynamic tokens.
if (($value_tokens = token_find_with_prefix($tokens, 'key')) || ($value_tokens = token_find_with_prefix($tokens, 'value'))) {
$tokss = array();
foreach ($value_tokens as $key => $original) {
$k = strtok($key, ':');
$tokss[$k][$k == $key ? ':' : substr($key, strlen($k) + 1)] = $original;
}
foreach ($tokss as $key => $toks) {
if ($key[0] !== '#' && isset($array[$key])) {
$replacements += token_generate($element_type, $toks, array(
$element_type => $array[$key],
), $options);
}
}
}
// [subs_list<>:index:*] dynamic tokens.
if ($index_tokens = token_find_with_prefix($tokens, 'index')) {
$tokss = array();
foreach ($index_tokens as $index => $original) {
$i = strtok($index, ':');
$tokss[$i][substr($index, strlen($i) + 1)] = $original;
}
$array_keys = array_keys($array);
foreach ($tokss as $index => $toks) {
if ($index[0] !== '#' && isset($array[$array_keys[$index]])) {
$replacements += token_generate($element_type, $toks, array(
$element_type => $array[$array_keys[$index]],
), $options);
}
}
}
// [subs_list<>:first:*] chained tokens.
if ($first_tokens = token_find_with_prefix($tokens, 'first')) {
$replacements += token_generate($element_type, $first_tokens, array(
$element_type => $array[$keys[0]],
), $options);
}
// [subs_list<>:last:*] chained tokens.
if ($last_tokens = token_find_with_prefix($tokens, 'last')) {
$replacements += token_generate($element_type, $last_tokens, array(
$element_type => $array[count($keys) - 1],
), $options);
}
// [subs_list<>:join:?] dynamic tokens.
if ($join_tokens = token_find_with_prefix($tokens, 'join')) {
$formatted = array();
foreach ($array as $item) {
$formatted[] = is_string($item) ? $item : current(token_generate($element_type, array(
':',
), array(
$element_type => $item,
), array(
'sanitize' => FALSE,
) + $options));
}
foreach ($join_tokens as $join => $original) {
$replacements[$original] = token_render_array($formatted, array(
'join' => $join,
) + $options);
}
}
// [subs_list<>:keys:*] chained tokens.
if ($key_tokens = token_find_with_prefix($tokens, 'keys')) {
$replacements += token_generate('array', $key_tokens, array(
'array' => $keys,
), $options);
}
// [subs_list<>:reversed:*] chained tokens.
if ($reversed_tokens = token_find_with_prefix($tokens, 'reversed')) {
$reversed = array_reverse($array, TRUE);
$replacements += token_generate($array_type, $reversed_tokens, array(
$array_type => $reversed,
), $options);
}
}
$field_types = array(
'dates' => 'subs_list<date>',
// dates:field_DATE
'files' => 'subs_list<file>',
// files:field_FILE
'terms' => 'subs_list<term>',
// terms:field_TERM
'texts' => 'array',
// texts:field_TEXT
'users' => 'subs_list<user>',
);
// Entity field tokens.
if (isset($data['object_type'])) {
$type = $data['object_type'];
if (!empty($data[$type]) && ($entity_type = token_get_entity_mapping('token', $type))) {
$entity = NULL;
foreach ($field_types as $field_type => $array_type) {
if ($field_tokens = token_find_with_prefix($tokens, $field_type)) {
if (empty($entity)) {
$entity = clone $data[$type];
}
$tokenss = array();
foreach ($field_tokens as $field_token => $original) {
$field_name = strtok($field_token, ':');
$tokenss[$field_name][substr($field_token, strlen($field_name) + 1)] = $original;
}
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
$fields = field_info_instances($entity_type, $bundle);
foreach ($tokenss as $field_name => $tokens2) {
if (isset($fields[$field_name])) {
if (empty($entity->{$field_name})) {
if (isset($tokens2['count'])) {
$replacements[$tokens2['count']] = 0;
}
}
else {
$objs = array();
if ($array_type == 'array') {
$field_output = field_view_field($entity_type, $entity, $field_name, 'token', $language_code);
$field_output['#token_options'] = $options;
$field_output['#pre_render'][] = 'token_pre_render_field_token';
foreach (element_children($field_output) as $key) {
$obj = $field_output[$key]['#markup'];
if (preg_match('/^<p>(.*)<\\/p>\\n$/', $obj, $match)) {
$obj = $match[1];
}
$objs[$key] = $obj;
}
$replacements += token_generate($array_type, $tokens2, array(
$array_type => $objs,
), array(
'sanitize' => FALSE,
) + $options);
}
else {
if ($field_items = field_get_items($type, $entity, $field_name, $language_code)) {
switch ($field_type) {
case 'terms':
// Term references are stored as tids only.
$tids = array();
foreach ($field_items as $term) {
$tids[] = $term['tid'];
}
$objs = taxonomy_term_load_multiple($tids);
break;
case 'users':
// User references are stored as uids only.
$uids = array();
foreach ($field_items as $user_ref) {
$uids[] = $user_ref['target_id'];
}
$objs = user_load_multiple($uids);
break;
default:
foreach ($field_items as $key => $item) {
$objs[$key] = (object) $item;
}
}
$replacements += token_generate($array_type, $tokens2, array(
$array_type => $objs,
), $options);
}
}
}
}
}
}
}
unset($entity);
}
}
return $replacements;
}