You are here

function _patterns_array_context in Patterns 6

Same name and namespace in other branches
  1. 6.2 patterns.module \_patterns_array_context()
  2. 7.2 includes/unused.inc \_patterns_array_context()
  3. 7 includes/unused.inc \_patterns_array_context()

Helper function to create a context array based on the supplied object Supplying a parent object will set the parent for this context

2 calls to _patterns_array_context()
patterns_array_fetch in ./patterns.module
Find parts of an array based on a semi-compatible xpath syntax.
patterns_config_data in ./patterns.module
Take a $data and $config object and adjust $data based on the supplied configuration

File

./patterns.module, line 2704
Enables extremely simple adding/removing features to your site with minimal to no configuration

Code

function _patterns_array_context(&$obj, &$current = null) {

  // If a current context is set, use it's parent and key values
  if (!($trace = $current['trace'])) {
    $trace = array();
  }
  if (!($key = $current['key'])) {
    $key = null;
  }
  if (!($parent =& $current['parent'])) {
    $parent = null;
  }
  $context = array(
    'trace' => $trace,
    'key' => $key,
    'item' => &$obj,
    'parent' => &$parent,
  );
  $refs = array(
    &$context,
  );
  while (!empty($refs)) {
    $ref =& $refs[0];
    $parent =& $ref['item'];
    array_splice($refs, 0, 1);
    if (is_array($parent) && !empty($parent)) {
      $i = 0;
      foreach ($parent as $index => &$child) {

        // TODO possible optimizations can be done here (with the parent trace)
        $ref[$i] = array(
          'trace' => _patterns_array_trace($ref),
          'key' => $index,
          'item' => &$child,
          'parent' => &$ref,
        );
        array_unshift($refs, '');
        $refs[0] =& $ref[$i++];
      }
    }
  }
  return $context;
}