class RulesTokenEvaluator

A class implementing a rules input evaluator processing tokens.

Hierarchy

Expanded class hierarchy of RulesTokenEvaluator

Related topics

2 string references to 'RulesTokenEvaluator'
hook_rules_evaluator_info in ./rules.api.php
Declare provided rules input evaluators.
rules_system_evaluator_info in modules/system.rules.inc
Implements hook_rules_evaluator_info() on behalf of the system module.

File

modules/system.eval.inc, line 181

View source
class RulesTokenEvaluator extends RulesDataInputEvaluator {
    
    /**
     * Overrides RulesDataInputEvaluator::prepare().
     */
    public function prepare($text, $var_info) {
        $text = is_array($text) ? implode('', $text) : $text;
        // Skip this evaluator if there are no tokens.
        $this->setting = token_scan($text) ? TRUE : NULL;
    }
    
    /**
     * Evaluate tokens.
     *
     * We replace the tokens on our own as we cannot use token_replace(), because
     * token usually assumes that $data['node'] is a of type node, which doesn't
     * hold in general in our case.
     * So we properly map variable names to variable data types and then run the
     * replacement ourself.
     */
    public function evaluate($text, $options, RulesState $state) {
        $var_info = $state->varInfo();
        $options += array(
            'sanitize' => FALSE,
        );
        $replacements = array();
        $data = array();
        // We also support replacing tokens in a list of textual values.
        $whole_text = is_array($text) ? implode('', $text) : $text;
        foreach (token_scan($whole_text) as $var_name => $tokens) {
            $var_name = str_replace('-', '_', $var_name);
            if (isset($var_info[$var_name]) && ($token_type = _rules_system_token_map_type($var_info[$var_name]['type']))) {
                // We have to key $data with the type token uses for the variable.
                $data = rules_unwrap_data(array(
                    $token_type => $state->get($var_name),
                ), array(
                    $token_type => $var_info[$var_name],
                ));
                $replacements += token_generate($token_type, $tokens, $data, $options);
            }
            else {
                $replacements += token_generate($var_name, $tokens, array(), $options);
            }
            // Remove tokens if no replacement value is found. As token_replace() does
            // if 'clear' is set.
            $replacements += array_fill_keys($tokens, '');
        }
        // Optionally clean the list of replacement values.
        if (!empty($options['callback']) && function_exists($options['callback'])) {
            $function = $options['callback'];
            $function($replacements, $data, $options);
        }
        // Actually apply the replacements.
        $tokens = array_keys($replacements);
        $values = array_values($replacements);
        if (is_array($text)) {
            foreach ($text as $i => $text_item) {
                $text[$i] = str_replace($tokens, $values, $text_item);
            }
            return $text;
        }
        return str_replace($tokens, $values, $text);
    }
    
    /**
     * Create documentation about the available replacement patterns.
     *
     * @param array $var_info
     *   Array with the available variables.
     *
     * @return array
     *   Renderable array with the replacement pattern documentation.
     */
    public static function help($var_info) {
        $render = array(
            '#type' => 'fieldset',
            '#title' => t('Replacement patterns'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
            '#description' => t('Note that token replacements containing chained objects – such as [node:author:uid] – are not listed here, but are still available. The <em>data selection</em> input mode may help you find more complex replacement patterns. See <a href="@url">the online documentation</a> for more information about complex replacement patterns.', array(
                '@url' => rules_external_help('chained-tokens'),
            )),
        );
        $token_info = token_info();
        foreach ($var_info as $name => $info) {
            $token_types[$name] = _rules_system_token_map_type($info['type']);
        }
        foreach ($token_types as $name => $token_type) {
            if (isset($token_info['types'][$token_type])) {
                $render[$name] = array(
                    '#theme' => 'table',
                    '#header' => array(
                        t('Token'),
                        t('Label'),
                        t('Description'),
                    ),
                    '#prefix' => '<h3>' . t('Replacement patterns for %label', array(
                        '%label' => $var_info[$name]['label'],
                    )) . '</h3>',
                );
                foreach ($token_info['tokens'][$token_type] as $token => $info) {
                    $token = '[' . str_replace('_', '-', $name) . ':' . $token . ']';
                    $render[$name]['#rows'][$token] = array(
                        check_plain($token),
                        check_plain($info['name']),
                        check_plain($info['description']),
                    );
                }
            }
        }
        return $render;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
RulesDataInputEvaluator::attachForm public static function Overrides RulesDataProcessor::attachForm(). Overrides RulesDataProcessor::attachForm
RulesDataInputEvaluator::evaluators public static function Returns all input evaluators that can be applied to the parameters type.
RulesDataInputEvaluator::getEvaluatorOptions protected function Generates the evaluator $options.
RulesDataInputEvaluator::getPreparedValue protected function Return $this or skip this processor by returning the next processor. Overrides RulesDataProcessor::getPreparedValue
RulesDataInputEvaluator::prepareSetting public static function Overridden to prepare input evaluator processors. Overrides RulesDataProcessor::prepareSetting
RulesDataInputEvaluator::process public function Overridden to generate evaluator $options and invoke evaluate(). Overrides RulesDataProcessor::process 1
RulesDataInputEvaluator::processors public static function Overrides RulesDataProcessor::processors(). Overrides RulesDataProcessor::processors
RulesDataInputEvaluator::__construct protected function Overridden to invoke prepare(). Overrides RulesDataProcessor::__construct
RulesDataProcessor::$processor protected property Allows chaining processors. If set, the next processor to invoke.
RulesDataProcessor::$setting protected property The processors&#039; setting value.
RulesDataProcessor::access public static function Return whether the current user has permission to use the processor. 1
RulesDataProcessor::dependencies public function Returns an array of modules which we depend on.
RulesDataProcessor::editAccess public function Determines whether the current user has permission to edit this chain of
data processors.
2
RulesDataProcessor::form protected static function Defines the processor form element. 3
RulesDataProcessor::getChainSettings public function Gets the settings array for this and all contained chained processors.
RulesDataProcessor::getSetting public function Gets the settings of this processor.
RulesDataProcessor::unchain protected function
RulesDataProcessor::_item_sort public static function
RulesTokenEvaluator::evaluate public function Evaluate tokens. Overrides RulesDataInputEvaluator::evaluate
RulesTokenEvaluator::help public static function Create documentation about the available replacement patterns. Overrides RulesDataInputEvaluator::help
RulesTokenEvaluator::prepare public function Overrides RulesDataInputEvaluator::prepare(). Overrides RulesDataInputEvaluator::prepare