123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- <?php
- function entity_token_types() {
- $return = entity_token_types_chained();
- return $return + drupal_map_assoc(array('text', 'integer', 'decimal', 'duration', 'boolean', 'uri'));
- }
- function entity_token_types_chained($type = NULL) {
-
-
- static $drupal_static_fast;
- if (!isset($drupal_static_fast)) {
- $drupal_static_fast['types'] = &drupal_static(__FUNCTION__, array());
- }
- $types = &$drupal_static_fast['types'];
- if (!$types) {
-
- foreach (entity_get_info() as $entity_type => $info) {
- if ($token_type = isset($info['token type']) ? $info['token type'] : $entity_type) {
- $types[$token_type] = $entity_type;
- }
- }
-
- $types['date'] = 'date';
- $types['site'] = 'site';
-
- $types['struct'] = 'struct';
- }
- if (isset($type)) {
- return isset($types[$type]) || entity_property_list_extract_type($type);
- }
- return $types;
- }
- function _entity_token_map_to_token_type($property_info) {
- $lookup = &drupal_static(__FUNCTION__);
- if (!$lookup) {
-
- $lookup = array_flip(entity_token_types());
- }
- $type = isset($property_info['type']) ? $property_info['type'] : 'text';
-
- if (!empty($property_info['property info'])) {
- $type = 'struct';
- }
- if ($item_type = entity_property_list_extract_type($type)) {
- return isset($lookup[$item_type]) ? "list<$lookup[$item_type]>" : FALSE;
- }
- return isset($lookup[$type]) ? $lookup[$type] : FALSE;
- }
- function entity_token_token_info_alter(&$info) {
- $entity_info = entity_get_info();
- $token_types = entity_token_types_chained();
-
-
- foreach ($token_types as $token_type => $type) {
-
-
- foreach (entity_get_all_property_info($type) as $name => $property) {
- $name = str_replace('_', '-', $name);
- $property += array('type' => 'text', 'description' => $property['label']);
- $property_token_type = _entity_token_map_to_token_type($property);
- if (!isset($info['tokens'][$token_type][$name]) && $property_token_type) {
- $info['tokens'][$token_type][$name] = array(
- 'name' => $property['label'],
- 'description' => $property['description'],
- 'type' => $property_token_type,
-
- 'entity-token' => TRUE,
- );
- }
- if ($property_token_type == 'struct' && !empty($property['property info'])) {
- $info['tokens'][$token_type][$name]['dynamic'] = TRUE;
- $help = array();
- foreach ($property['property info'] as $key => $property_info) {
- $help[] = $key . ' (' . $property_info['label'] . ')';
- }
- $info['tokens'][$token_type][$name]['description'] .= ' ' . t('The following properties may be appended to the token: @keys',
- array('@keys' => implode(', ', $help))
- );
- }
- }
- }
-
- foreach ($token_types as $token_type => $type) {
- if (!empty($info['tokens'][$token_type]) && !isset($info['types'][$token_type])) {
- if (isset($entity_info[$type])) {
- $info['types'][$token_type] = array(
- 'name' => $entity_info[$type]['label'],
- 'description' => t('Tokens related to the "@name" entities.', array('@name' => $entity_info[$type]['label'])),
- 'needs-data' => $token_type,
- );
- }
- else {
- $info['types'][$token_type] = array(
- 'name' => drupal_strtoupper($token_type),
- 'description' => t('@name tokens.', array('@name' => drupal_strtoupper($token_type))),
- 'needs-data' => $token_type,
- );
- }
- }
- if (!empty($info['tokens'][$token_type]) && !isset($info['types']["list<$token_type>"]) && $token_type != 'site') {
- if (isset($entity_info[$type])) {
- $info['types']["list<$token_type>"] = array(
- 'name' => t('List of @entities', array('@entities' => isset($entity_info[$type]['plural label']) ? $entity_info[$type]['plural label'] : $entity_info[$type]['label'] . 's')),
- 'description' => t('Tokens related to the "@name" entities.', array('@name' => $entity_info[$type]['label'])),
- 'needs-data' => "list<$token_type>",
- );
- }
- else {
- $info['types']["list<$token_type>"] = array(
- 'name' => t('List of @type values', array('@type' => $token_type)),
- 'description' => t('Tokens for lists of @type values.', array('@type' => $token_type)),
- 'needs-data' => "list<$token_type>",
- );
- }
-
- for ($i = 0; $i < 4; $i++) {
- $info['tokens']["list<$token_type>"][$i] = array(
- 'name' => t('@type with delta @delta', array('@delta' => $i, '@type' => $info['types'][$token_type]['name'])),
- 'description' => t('The list item with delta @delta. Delta values start from 0 and are incremented by one per list item.', array('@delta' => $i)),
- 'type' => $token_type,
- );
- }
- }
- }
- }
- function entity_token_tokens($type, $tokens, array $data = array(), array $options = array()) {
- $token_types = entity_token_types_chained();
- $replacements = array();
- if (isset($token_types[$type]) && (!empty($data[$type]) || $type == 'site')) {
- $data += array($type => FALSE);
-
- $info = module_exists('token') ? token_get_info() : token_info();
- foreach ($tokens as $name => $original) {
-
- if (!empty($info['tokens'][$type][$name]['entity-token']) || $type == 'struct') {
- $wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, $token_types[$type], $data[$type], $options) : $wrapper;
- $property_name = str_replace('-', '_', $name);
- try {
- if (isset($wrapper->$property_name)) {
- $replacement = _entity_token_get_token($wrapper->$property_name, $options);
- if (isset($replacement)) {
- $replacements[$original] = $replacement;
- }
- }
- }
- catch (EntityMetadataWrapperException $e) {
-
- }
- }
- }
-
- $info['tokens'] += array($type => array());
- foreach ($info['tokens'][$type] as $name => $token_info) {
- if (!empty($token_info['entity-token']) && isset($token_info['type']) && entity_token_types_chained($token_info['type'])) {
- if ($chained_tokens = token_find_with_prefix($tokens, $name)) {
- $wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, $token_types[$type], $data[$type], $options) : $wrapper;
- $property_name = str_replace('-', '_', $name);
- try {
-
- $value = ($token_info['type'] == 'struct') ? $wrapper->$property_name : $wrapper->$property_name->value();
- $replacements += token_generate($token_info['type'], $chained_tokens, array($token_info['type'] => $value), $options);
- }
- catch (EntityMetadataWrapperException $e) {
-
- }
- }
- }
- }
- }
-
- elseif ($item_token_type = entity_property_list_extract_type($type)) {
- foreach ($tokens as $name => $original) {
-
- if (is_numeric($name)) {
- $wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, "list<$token_types[$item_token_type]>", $data[$type], $options) : $wrapper;
- try {
- $replacement = _entity_token_get_token($wrapper->get($name), $options);
- if (isset($replacement)) {
- $replacements[$original] = $replacement;
- }
- }
- catch (EntityMetadataWrapperException $e) {
-
- }
- }
-
- else {
- $parts = explode(':', $name, 2);
- $delta = $parts[0];
- if (is_numeric($delta) && $chained_tokens = token_find_with_prefix($tokens, $delta)) {
- $wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, "list<$token_types[$item_token_type]>", $data[$type], $options) : $wrapper;
- try {
- $replacements += token_generate($item_token_type, $chained_tokens, array($item_token_type => $wrapper->get($delta)->value()), $options);
- }
- catch (EntityMetadataWrapperException $e) {
-
- }
- }
- }
- }
- }
-
-
- if ($type == 'struct') {
- $wrapper = $data[$type];
- foreach ($wrapper as $name => $property) {
- $token_type = _entity_token_map_to_token_type($property->info());
- if (entity_token_types_chained($token_type) && $chained_tokens = token_find_with_prefix($tokens, $name)) {
- try {
-
- $value = ($token_type == 'struct') ? $property : $property->value();
- $replacements += token_generate($token_type, $chained_tokens, array($token_type => $value), $options);
- }
- catch (EntityMetadataWrapperException $e) {
-
- }
- }
- }
- }
- return $replacements;
- }
- function _entity_token_wrap_data($token_type, $type, $data, $options) {
- if ($type == 'site') {
- $wrapper = entity_metadata_site_wrapper();
- }
- elseif ($type == 'struct') {
-
- $wrapper = $data;
- }
- else {
- $wrapper = entity_metadata_wrapper($type, $data);
- }
- if (isset($options['language']) && $wrapper instanceof EntityStructureWrapper) {
- $wrapper->language($options['language']->language);
- }
- return $wrapper;
- }
- function _entity_token_get_token($wrapper, $options) {
- if (!$wrapper || $wrapper->value() === NULL) {
-
- return NULL;
- }
- if (empty($options['sanitize'])) {
-
- $options['decode'] = TRUE;
- }
- $langcode = isset($options['language']) ? $options['language']->language : NULL;
-
-
- if ($label = $wrapper->label()) {
- return empty($options['sanitize']) ? $label : check_plain($label);
- }
- switch ($wrapper->type()) {
- case 'integer':
- return $wrapper->value();
- case 'decimal':
- return number_format($wrapper->value(), 2);
- case 'date':
- return format_date($wrapper->value(), 'medium', '', NULL, $langcode);
- case 'duration':
- return format_interval($wrapper->value(), 2, $langcode);
- case 'boolean':
- return $wrapper->value() ? t('true') : t('false');
- case 'uri':
- case 'text':
- return $wrapper->value($options);
- }
-
- if ($wrapper instanceof EntityListWrapper) {
- $output = array();
- foreach ($wrapper as $item) {
- $output[] = _entity_token_get_token($item, $options);
- }
- return implode(', ', $output);
- }
-
-
- return (string) $wrapper;
- }
|