After updating the Token Module from 6.x-1.16 to 6.x-1.17, my dblog prints the following error when displaying a view:

preg_match_all() expects parameter 2 to be string, array given in /etc/drupal6/all/modules/token/token.module on line 701.

And, in the view of question some fields are empty. Thus my views are now broken! Worked fine in the 6.x-1.16 version.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

storytellerjeff’s picture

Subscribe - I'm getting the same error on my site.

Dave Reid’s picture

Priority: Critical » Major

I have added some debugging code to 6.x-1.x-dev release that will help trace what function is responsible for calling this incorrectly. Please download the 6.x-1.x-dev release in about 12 hours to help me debug this further.

roball’s picture

Title: preg_match_all() expects parameter 2 to be string, array given in token.module on line 701. » The XXX function called token replacement with an array rather than a string for $text in token.module on line 263
Version: 6.x-1.17 » 6.x-1.x-dev

Thanks for adding the debug code. With 6.x-1.x-dev rolled out at 2011-Nov-03, 00:31 UTC the error message mentioned above changed to

The _viewfield_get_view_args() function called token replacement with an array rather than a string for $text in /etc/drupal6/all/modules/token/token.module on line 263.

Thus the error in my CCK nodes and views is caused by the Viewfield module 6.x-1.2, within the "theme/viewfield.theme.inc" file. Its problematic function currently is:

/**
 * Perform argument replacement
 */
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_values = node_load($node->nid);
      $args = token_replace($args, 'node', $node_values);
      $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;
}

Can you please say how that function must be changed in order to be compatible with your significant change in Token 6.x-1.17?

Dave Reid’s picture

Title: The XXX function called token replacement with an array rather than a string for $text in token.module on line 263 » The _viewfield_get_view_args() function called token replacement with an array rather than a string for $text
Project: Token » Viewfield

Transferring to the Viewfield issue queue.

storytellerjeff’s picture

@Dave Reid - I'm not sure that this is specifically a Viewfield issue. Here's the error message I'm now getting:

The _custom_breadcrumbs_get_breadcrumb() function called token replacement with an array rather than a string for $text in /home/catdocto/public_html/sites/all/modules/token/token.module on line 263.

Any thoughts?

keithm’s picture

Title: The _viewfield_get_view_args() function called token replacement with an array rather than a string for $text » Update Viewfield token replacement for API change
Priority: Major » Normal
Status: Active » Needs review
FileSize
815 bytes

It looks like token_replace() stopped accepting an array for its first argument starting in token-6.x-1.15. Viewfield needs to be updated accordingly.

Dave Reid’s picture

Hrm, thanks for pointing that out. So yeah, the API did actually support an array as of 6.x-1.15 but we've slowly moved away from that. This is still a good change for module authors to make as it never should have been supported like that, but I'm probably going to file a new issue in token to now throw the notice when this happens now.

@storytellerjeff: Yes, multiple modules did this. See #1327960: Invalid use of token_replace API - causes preg_match_all errors in token module for the custom_breadcrumbs issue.

storytellerjeff’s picture

@Dave Reid - Thanks! I appreciate that you're on top of this so quickly.

roball’s picture

Version: 6.x-1.x-dev » 6.x-1.2
Status: Needs review » Reviewed & tested by the community

Thanks for the patch at #6, keithm! Have applied it on Viewfield 6.x-1.2 and it solved the problem. No longer "The _viewfield_get_view_args() function called token replacement with an array rather than a string for $text in /etc/drupal6/all/modules/token/token.module on line 263." warnings by Token Module 6.x-1.18.

keithm’s picture

Version: 6.x-1.2 » 6.x-1.x-dev
Status: Reviewed & tested by the community » Fixed
allnet000’s picture

I am not using the ViewField module, but still getting a similar error message. Is there a different patch I need to apply?

user notice: The custom_pagers_get_list_from_view() function called token replacement with an array rather than a string for $text in sites/all/modules/token/token.module on line 263.

roball’s picture

@allnet000: as you can see in your error message, that's a problem of the custom_pagers module: #1327982: Invalid use of token_replace API - causes preg_match_all errors in token module.

allnet000’s picture

thank you.

Dave Reid’s picture

A quick update on this from the token maintainer (me): I'm officially backtracking on not supporting $text being an array. I looked back and as of 6.x-1.15 the token_replace() functions officially had it documented that $text could either be a string or an array. This reference to it being an array was removed when I updated the documentation for the functions which I had assumed the array reference was a mistake (which was my fault). At this point in 6.x-1.x-dev (and eventually in the future 6.x-1.19 release) of Token there are no more warnings thrown if $text is an array and token replacement should just work as before. I apologize to the end users of Token who encountered those frustrating notices and the module developers who had these bugs reported against their modules. This experience was at least valuable to know which modules were using array token replacement as they would have encountered this problem when porting modules to Drupal 7. Also, in the official API documentation, $text is a string. The parameter being an array is only supported for backwards-compatibility.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

kayuna’s picture

Uhm.. I'm very new to all this, do I paste that string into the existing php thing, or make a new?

keithm’s picture

@kayuna: This issue was fixed for viewfield, though there might be other modules that still have the problem. As Dave Reid says in #14, upgrading Token to the latest development version 6.x-1.x-dev will fix all warnings regardless. If you need other support, please open a new issue since this issue is currently closed.

yktdan’s picture

dev version works, 6.18 does not, still. If the proper fix is to install a new viewfield (and others without the api change), then viewfield needs a new release, not just yet another dev. Most of us like to run supported releases, not dev versions. So how about a token 6.19 that is fixed.

nicktr’s picture

Dev version works for me too. Any news on a 6.19 release?

roball’s picture

This issue is one of the Viewfield module and already closed.

nicktr, you are asking for a new release of the Token module. I have posted this request in their own issue queue here: #1772136: Release of Token 6.x-1.19.