function rules_wrap_data

Wraps the given data.

Parameters

$data: If available, the actual data, else NULL.

$info: An array of info about this data.

bool $force: Usually data is only wrapped if really needed. If set to TRUE, wrapping the data is forced, so primitive data types are also wrapped.

Return value

EntityMetadataWrapper An EntityMetadataWrapper or the unwrapped data.

See also

hook_rules_data_info()

7 calls to rules_wrap_data()
RulesData::matchingDataSelector in includes/rules.state.inc
Returns data for the given info and the to-be-configured parameter.
RulesIntegrationTestCase::testTaxonomyIntegration in tests/rules.test
Tests integration for the taxonomy module.
RulesPlugin::applyDataSelector in includes/rules.core.inc
Applies the given data selector.
RulesPlugin::getArgument in includes/rules.core.inc
Returns the argument for the parameter $name described with $info.
RulesState::addVariable in includes/rules.state.inc
Adds the given variable to the given execution state.

... See full list

File

./rules.module, line 569

Code

function &rules_wrap_data($data, $info, $force = FALSE) {
    // If the data is already wrapped, use the existing wrapper.
    if ($data instanceof EntityMetadataWrapper) {
        return $data;
    }
    $cache = rules_get_cache();
    // Define the keys to be passed through to the metadata wrapper.
    $wrapper_keys = array_flip(array(
        'property info',
        'property defaults',
    ));
    if (isset($cache['data_info'][$info['type']])) {
        $info += array_intersect_key($cache['data_info'][$info['type']], $wrapper_keys);
    }
    // If a list is given, also add in the info of the item type.
    $list_item_type = entity_property_list_extract_type($info['type']);
    if ($list_item_type && isset($cache['data_info'][$list_item_type])) {
        $info += array_intersect_key($cache['data_info'][$list_item_type], $wrapper_keys);
    }
    // By default we do not wrap the data, except for completely unknown types.
    if (!empty($cache['data_info'][$info['type']]['wrap']) || $list_item_type || $force || empty($cache['data_info'][$info['type']])) {
        unset($info['handler']);
        // Allow data types to define custom wrapper classes.
        if (!empty($cache['data_info'][$info['type']]['wrapper class'])) {
            $class = $cache['data_info'][$info['type']]['wrapper class'];
            $wrapper = new $class($info['type'], $data, $info);
        }
        else {
            $wrapper = entity_metadata_wrapper($info['type'], $data, $info);
        }
        return $wrapper;
    }
    return $data;
}