You are here

public static function RulesData::addMetadataAssertions in Rules 7.2

Adds asserted metadata to the variable info.

In case there are already assertions for a variable, the assertions are merged such that both apply.

See also

RulesData::applyMetadataAssertions()

3 calls to RulesData::addMetadataAssertions()
Rule::stateVariables in includes/rules.plugins.inc
Returns available state variables for an element.
RulesConditionContainer::stateVariables in includes/rules.core.inc
Overridden to exclude variable assertions of negated conditions.
RulesContainerPlugin::stateVariables in includes/rules.core.inc
Returns available state variables for an element.

File

includes/rules.state.inc, line 516
Contains the state and data related stuff.

Class

RulesData
A class holding static methods related to data.

Code

public static function addMetadataAssertions($var_info, $assertions) {
  foreach ($assertions as $selector => $assertion) {

    // Convert the selector back to underscores, such it matches the varname.
    $selector = str_replace('-', '_', $selector);
    $parts = explode(':', $selector);
    if (isset($var_info[$parts[0]])) {

      // Apply the selector to determine the right target array. We build an
      // array like
      // $var_info['rules assertion']['property1']['property2']['#info'] = ..
      $target =& $var_info[$parts[0]]['rules assertion'];
      foreach (array_slice($parts, 1) as $part) {
        $target =& $target[$part];
      }

      // In case the assertion is directly for a variable, we have to modify
      // the variable info directly. In case the asserted property is nested
      // the info-has to be altered by RulesData::applyMetadataAssertions()
      // before the child-wrapper is created.
      if (count($parts) == 1) {

        // Support asserting a type in case of generic entity references only.
        $var_type =& $var_info[$parts[0]]['type'];
        if (isset($assertion['type']) && ($var_type == 'entity' || $var_type == 'list<entity>')) {
          $var_type = $assertion['type'];
          unset($assertion['type']);
        }

        // Add any single bundle directly to the variable info, so the
        // variable fits as argument for parameters requiring the bundle.
        if (isset($assertion['bundle']) && count($bundles = (array) $assertion['bundle']) == 1) {
          $var_info[$parts[0]]['bundle'] = reset($bundles);
        }
      }

      // Add the assertions, but merge them with any previously added
      // assertions if necessary.
      $target['#info'] = isset($target['#info']) ? rules_update_array($target['#info'], $assertion) : $assertion;

      // Add in a callback that the entity metadata wrapper pick up for
      // altering the property info, such that we can add in the assertions.
      $var_info[$parts[0]] += array(
        'property info alter' => array(
          'RulesData',
          'applyMetadataAssertions',
        ),
      );

      // In case there is a VARNAME_unchanged variable as it is used in update
      // hooks, assume the assertions are valid for the unchanged variable
      // too.
      if (isset($var_info[$parts[0] . '_unchanged'])) {
        $name = $parts[0] . '_unchanged';
        $var_info[$name]['rules assertion'] = $var_info[$parts[0]]['rules assertion'];
        $var_info[$name]['property info alter'] = array(
          'RulesData',
          'applyMetadataAssertions',
        );
        if (isset($var_info[$parts[0]]['bundle']) && !isset($var_info[$name]['bundle'])) {
          $var_info[$name]['bundle'] = $var_info[$parts[0]]['bundle'];
        }
      }
    }
  }
  return $var_info;
}