diff --git a/entity_token.info b/entity_token.info index 66fddcb..fd07252 100644 --- a/entity_token.info +++ b/entity_token.info @@ -4,3 +4,8 @@ core = 7.x files[] = entity_token.tokens.inc files[] = entity_token.module dependencies[] = entity + +; Testing dependencies. +files[] = entity_token.test +test_dependencies[] = entity_token_test +test_dependencies[] = token diff --git a/entity_token.install b/entity_token.install new file mode 100644 index 0000000..ffbeab2 --- /dev/null +++ b/entity_token.install @@ -0,0 +1,26 @@ + 'Entity token', + 'description' => 'Configure entity token defaults.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('entity_token_admin_form'), + 'access arguments' => array('administer site configuration'), + ); + + return $items; +} + +/** + * Form callback: entity token config. + */ +function entity_token_admin_form($form, &$form_state) { + $form['entity_token_decimal_round'] = array( + '#type' => 'textfield', + '#title' => t('Default decimal rounding'), + '#description' => t('Specify the number of digits to round decimal properties to. Leave blank to not round at all.'), + '#default_value' => variable_get('entity_token_decimal_round'), + '#element_validate' => array('element_validate_integer'), + ); + + return system_settings_form($form); +} diff --git a/entity_token.test b/entity_token.test new file mode 100644 index 0000000..0b51309 --- /dev/null +++ b/entity_token.test @@ -0,0 +1,80 @@ + 'Token unit tests', + 'description' => 'Test basic, low-level token functions.', + 'group' => 'Token', + ); + } + + public function testDecimalTokenSuffixes() { + variable_set('entity_token_decimal_round', NULL); + $node = $this->drupalCreateNode('test'); + + $tokens = array( + 'pi' => MATH_PI, + 'pi:raw' => MATH_PI, + 'pi:unf' => MATH_PI, + 'pi:round' => round(MATH_PI, 0), + 'pi:integer' => round(MATH_PI, 0), + 'pi:round:1' => round(MATH_PI, 1), + 'pi:round:2' => round(MATH_PI, 2), + 'pi:round:4' => round(MATH_PI, 4), + 'pi:round:8' => round(MATH_PI, 8), + 'pi:round:12' => round(MATH_PI, 12), + ); + $this->assertTokens('node', array(), $tokens); + } + + public function testDecimalTokenConfig() { + variable_set('entity_token_decimal_round', NULL); + $node = $this->drupalCreateNode('test'); + + $tokens = array( + 'pi' => MATH_PI, + ); + $this->assertTokens('node', $node, $tokens); + + variable_set('entity_token_decimal_round', 2); + $tokens = array( + 'pi' => round(MATH_PI, 2), + ); + $this->assertTokens('node', $node, $tokens); + + variable_set('entity_token_decimal_round', 0); + $tokens = array( + 'pi' => round(MATH_PI, 0), + ); + $this->assertTokens('node', $node, $tokens); + + variable_set('entity_token_decimal_round', INF); + $tokens = array( + 'pi' => MATH_PI, + ); + $this->assertTokens('node', $node, $tokens); + } + +} diff --git a/entity_token.tokens.inc b/entity_token.tokens.inc index 77a170d..d61b981 100644 --- a/entity_token.tokens.inc +++ b/entity_token.tokens.inc @@ -13,7 +13,7 @@ */ function entity_token_types() { $return = entity_token_types_chained(); - return $return + drupal_map_assoc(array('text', 'integer', 'decimal', 'duration', 'boolean', 'uri')); + return $return + drupal_map_assoc(array('text', 'integer', 'duration', 'boolean', 'uri')); } /** @@ -43,6 +43,7 @@ function entity_token_types_chained($type = NULL) { // Add 'date' and 'site' tokens. $types['date'] = 'date'; $types['site'] = 'site'; + $types['decimal'] = 'decimal'; // Add a 'struct' type. $types['struct'] = 'struct'; } @@ -113,6 +114,16 @@ function entity_token_token_info_alter(&$info) { array('@keys' => implode(', ', $help)) ); } + if ($property_token_type == 'decimal' && !empty($property['property info'])) { + $info['tokens'][$token_type][$name]['dynamic'] = TRUE; + $help = array('raw', 'unf', 'integer', 'round', 'round:?'); + 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)) + ); + } } } @@ -268,6 +279,38 @@ function entity_token_tokens($type, $tokens, array $data = array(), array $optio } } + // Add support for rounding decimal data. + if ($type == 'decimal') { + $wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, $token_types[$type], $data[$type], $options) : $wrapper; + foreach ($tokens as $key => $original) { + if ($key == 'round' || preg_match('/^round:[0-9]+$/', $key) || $key == 'unf' || $key == 'raw' || $key == 'integer') { + $round = NULL; + switch ($key) { + case 'round': + case 'integer': + $round = 0; + break; + + case strpos($key, 'round:') === 0: + list(, $round) = explode(':', $key); + $round = intval($round); + break; + + case 'unf': + case 'raw': + $round = INF; + break; + } + $rep_options = array_merge($options, ( + $round !== NULL ? + array('decimal_round' => $round) : + array() + )); + $replacements[$original] = _entity_token_get_token($wrapper, $rep_options); + } + } + } + return $replacements; } @@ -317,7 +360,27 @@ function _entity_token_get_token($wrapper, $options) { case 'integer': return $wrapper->value(); case 'decimal': - return number_format($wrapper->value(), 2); + $default_round = variable_get('entity_token_decimal_round', NULL); + $round = ( + array_key_exists('decimal_round', $options) ? + $options['decimal_round'] : + ( + !is_null($default_round) && strlen($default_round) > 0 ? + intval($default_round) : + INF + ) + ); + switch ($round) { + case INF: + return $wrapper->value(); + + case 0: + return number_format($wrapper->value(), 0); + + default: + return number_format($wrapper->value(), $round); + } + case 'date': return format_date($wrapper->value(), 'medium', '', NULL, $langcode); case 'duration': diff --git a/tests/entity_token_test.info b/tests/entity_token_test.info new file mode 100644 index 0000000..1f914e4 --- /dev/null +++ b/tests/entity_token_test.info @@ -0,0 +1,9 @@ +name = Entity Token test module +description = Provides test entity types based upon the CRUD API. +version = VERSION +core = 7.x +files[] = entity_test.module +dependencies[] = entity +dependencies[] = entity_token +dependencies[] = token +hidden = TRUE diff --git a/tests/entity_token_test.module b/tests/entity_token_test.module new file mode 100644 index 0000000..7dc156e --- /dev/null +++ b/tests/entity_token_test.module @@ -0,0 +1,26 @@ + t('Pi'), + 'description' => t('Returns pi for testing purposes.'), + 'type' => 'decimal', + 'getter callback' => 'entity_token_test_property_pi', + ); +} + +/** + * Property getter: returns pi. + */ +function entity_token_test_property_pi() { + return M_PI; +}