You are here

function _viewfield_get_view_args in Viewfield 6

Same name and namespace in other branches
  1. 6.2 viewfield.module \_viewfield_get_view_args()
  2. 7.3 viewfield.module \_viewfield_get_view_args()
  3. 7.2 viewfield.module \_viewfield_get_view_args()

Perform argument replacement

1 call to _viewfield_get_view_args()
theme_viewfield_formatter_default in theme/viewfield.theme.inc
Return a themed view avoiding viewfield recursion.

File

theme/viewfield.theme.inc, line 51
Theme functions.

Code

function _viewfield_get_view_args($token_enabled, $vargs, $node) {
  $args = array();

  // Prevent token_replace() from running this function a second time
  // before it completes the first time.
  static $tokens = TRUE;
  if ($tokens && !empty($vargs)) {
    $pos = 0;
    while ($pos < strlen($vargs)) {
      $found = FALSE;

      // If string starts with a quote, start after quote and get everything
      // before next quote.
      if (strpos($vargs, '"', $pos) === $pos) {
        if (($quote = strpos($vargs, '"', ++$pos)) !== FALSE) {

          // Skip pairs of quotes.
          while (!(($ql = strspn($vargs, '"', $quote)) & 1)) {
            $quote = strpos($vargs, '"', $quote + $ql);
          }
          $args[] = str_replace('""', '"', substr($vargs, $pos, $quote + $ql - $pos - 1));
          $pos = $quote + $ql + 1;
          $found = TRUE;
        }
      }
      elseif (($comma = strpos($vargs, ',', $pos)) !== FALSE) {

        // Otherwise, get everything before next comma.
        $args[] = substr($vargs, $pos, $comma - $pos);

        // Skip to after comma and repeat
        $pos = $comma + 1;
        $found = TRUE;
      }
      if (!$found) {
        $args[] = substr($vargs, $pos);
        $pos = strlen($vargs);
      }
    }
    if ($token_enabled) {
      $tokens = FALSE;

      // If the view field is being loaded as a "view field" of "view row",
      // instead of a simple "node field", the node object is not fully populated:
      // we need a full node to perform a correct replacement.
      $node = node_load($node->nid);
      foreach ($args as $key => $text) {
        $args[$key] = token_replace($text, 'node', $node);
      }
      $tokens = TRUE;
    }

    // For backwards compatibility, we scan for %nid, etc.
    foreach ($args as $key => $value) {
      $args[$key] = strtr($value, array(
        '%nid' => $node->nid,
        '%author' => isset($node->uid) ? $node->uid : (isset($node_values->uid) ? $node_values->uid : NULL),
        '%viewer' => $GLOBALS['user']->uid,
      ));
    }
  }
  return $args;
}