function RulesState::applyDataSelector

Returns an entity metadata wrapper as specified in the selector.

Parameters

string $selector: The selector string, e.g. "node:author:mail".

string $langcode: (optional) The language code used to get the argument value if the argument value should be translated. Defaults to LANGUAGE_NONE.

Return value

EntityMetadataWrapper The wrapper for the given selector.

Throws

RulesEvaluationException Throws a RulesEvaluationException in case the selector cannot be applied.

File

includes/rules.state.inc, line 299

Class

RulesState
The rules evaluation state.

Code

public function applyDataSelector($selector, $langcode = LANGUAGE_NONE) {
    $parts = explode(':', str_replace('-', '_', $selector), 2);
    $wrapper = $this->get($parts[0]);
    if (count($parts) == 1) {
        return $wrapper;
    }
    elseif (!$wrapper instanceof EntityMetadataWrapper) {
        throw new RulesEvaluationException('Unable to apply data selector %selector. The specified variable is not wrapped correctly.', array(
            '%selector' => $selector,
        ));
    }
    try {
        foreach (explode(':', $parts[1]) as $name) {
            if ($wrapper instanceof EntityListWrapper || $wrapper instanceof EntityStructureWrapper) {
                // Make sure we are using the right language. Wrappers might be cached
                // and have previous langcodes set, so always set the right language.
                if ($wrapper instanceof EntityStructureWrapper) {
                    $wrapper->language($langcode);
                }
                $wrapper = $wrapper->get($name);
            }
            else {
                throw new RulesEvaluationException('Unable to apply data selector %selector. The specified variable is not a list or a structure: %wrapper.', array(
                    '%selector' => $selector,
                    '%wrapper' => $wrapper,
                ));
            }
        }
    } catch (EntityMetadataWrapperException $e) {
        // In case of an exception, re-throw it.
        throw new RulesEvaluationException('Unable to apply data selector %selector: %error', array(
            '%selector' => $selector,
            '%error' => $e->getMessage(),
        ));
    }
    return $wrapper;
}