1 menu.inc _menu_load_objects(&$item, &$map)

Loads objects into the map as defined in the $item['load_functions'].

Parameters

$item: A menu router or menu link item

$map: An array of path arguments; for example, array('node', '5').

Return value

Returns TRUE for success, FALSE if an object cannot be loaded, and NULL: if the $map contains the placeholders (e.g., '%node'), not arguments for the load functions (e.g., '5'). Names of object loading functions are placed in $item['load_functions']. Loaded objects are placed in $map[]; keys are the same as keys in the $item['load_functions'] array. $item['access'] is set to FALSE if an object cannot be loaded.

Related topics

File

core/includes/menu.inc, line 607
API for the Backdrop menu system.

Code

function _menu_load_objects(&$item, &$map) {
  if ($load_functions = $item['load_functions']) {
    // If the path map is the same as the router path, don't bother loading
    // placeholders.
    if (!empty($item['path']) && $item['path'] === implode('/', $map)) {
      return NULL;
    }

    // If someone calls this function twice, then unserialize will fail.
    if (!is_array($load_functions)) {
      $load_functions = unserialize($load_functions);
    }
    $path_map = $map;
    foreach ($load_functions as $index => $function) {
      if ($function) {
        $value = isset($path_map[$index]) ? $path_map[$index] : '';
        if (is_array($function)) {
          // Set up arguments for the load function. These were pulled from
          // 'load arguments' in the hook_menu() entry, but they need
          // some processing. In this case the $function is the key to the
          // load_function array, and the value is the list of arguments.
          $args = current($function);
          $function = key($function);
          $load_functions[$index] = $function;

          // Some arguments are placeholders for dynamic items to process.
          foreach ($args as $i => $arg) {
            if ($arg === '%index') {
              // Pass on argument index to the load function, so multiple
              // occurrences of the same placeholder can be identified.
              $args[$i] = $index;
            }
            if ($arg === '%map') {
              // Pass on menu map by reference. The accepting function must
              // also declare this as a reference if it wants to modify
              // the map.
              $args[$i] = &$map;
            }
            if (is_int($arg)) {
              $args[$i] = isset($path_map[$arg]) ? $path_map[$arg] : '';
            }
          }
          array_unshift($args, $value);
          $return = call_user_func_array($function, $args);
        }
        else {
          $return = (backdrop_substr($value, 0, 1) === '%') ? NULL : $function($value);
        }
        // If callback returned an error or there is no callback, trigger 404.
        if ($return === FALSE) {
          $item['access'] = FALSE;
          $map = FALSE;
          return FALSE;
        }
        $map[$index] = $return;
      }
    }
    $item['load_functions'] = $load_functions;
  }
  return TRUE;
}