0

Not sure what is going wrong here but I am sure it is simple.

The token [node:field_report_store_name] ("Text field" token) doesn't handle special characters which [node:field-report-store-name] ("Field 'field_report_store_name' token) takes care of. This token, however, works alone without PHP, but when I try to use it in a PHP pattern, I get an Unexpected Error upon updating the automatic node title.

The below works without error, but special characters such as apostrophes display as "'":

$storename = '[node:field_report_store_name]';
$city = '[node:field-report-zip:locality]';
$state = '[node:field-report-zip:administrative-area]';

if(empty($storename)) {
  return $city.", ".$state;
} else {
  return $storename." in ".$city.", ".$state;
}

The below is the exact same code but with the token that handles the special characters, and produces an Unexpected Error:

$storename = '[node:field-report-store-name]';
$city = '[node:field-report-zip:locality]';
$state = '[node:field-report-zip:administrative-area]';

if(empty($storename)) {
  return $city.", ".$state;
} else {
  return $storename." in ".$city.", ".$state;
}

The settings are set correctly:

Automatic Entity Label Settings

The error as appears in the dblog is:

"ParseError: syntax error, unexpected 's' (T_STRING) in auto_entitylabel_eval() (line 2 of /sites/all/modules/auto_entitylabel/auto_entitylabel.module(447) : eval()'d code)."

Here is the version of the auto_entitylabel.module file: https://git.drupalcode.org/project/auto_entitylabel/-/blob/7.x-1.4/auto_entitylabel.module

The function it hangs up on is:

/**
 * Evaluates php code and passes $entity and $language to it.
 */
function auto_entitylabel_eval($code, $entity, $language = LANGUAGE_NONE) {
  ob_start();
  // @codingStandardsIgnoreLine
  print eval('?>' . $code);
  $output = ob_get_contents();
  ob_end_clean();
  return $output;
}

What am I doing wrong here?

WebMW
  • 514
  • 2
  • 13
  • 26
  • You should mention what is the error message exactly, and what is the context of the code (is it in a page callback or in any hook ?). Also what do you mean by _"This token, however, works alone without PHP"_ ? – EricLavault Mar 16 '21 at 14:00
  • @EricLavault my apologies, the error is "The website encountered an unexpected error. Please try again later." This is code placed within the "Auto Label" tab of a particular content type. The token causing the error works by itself if I do not use PHP. – WebMW Mar 16 '21 at 14:07
  • This is the error message displayed for users, I meant there should be an error logged somewhere (check php logs, also httpd & mysql). – EricLavault Mar 16 '21 at 14:14
  • @EricLavault gotcha, found the error: "ParseError: syntax error, unexpected 's' (T_STRING) in auto_entitylabel_eval() (line 2 of /sites/all/modules/auto_entitylabel/auto_entitylabel.module(447) : eval()'d code)." – WebMW Mar 16 '21 at 14:20
  • I'm going through the list of issues regarding T_STRING and having trouble knowing which is my cause (https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them/18092277#18092277) – WebMW Mar 16 '21 at 14:29
  • Ok, please edit your question so that it emphasize on this error message, that is the real issue. Also, you should make it clear that this code is evaluated by the module `auto_entitylabel` (eval() makes it more complicated, and this is probalby related). – EricLavault Mar 16 '21 at 14:38
  • I made the adjustments you recommended, thank you. – WebMW Mar 16 '21 at 14:43
  • I also added in the function it is hanging up on. – WebMW Mar 16 '21 at 14:53

1 Answers1

1

this does not answer the question but tries to explain why this is happending

The problem comes from the fact that the ' could be an escaped quote ', that is intended to prevent breaking a string when the module evaluates the code.

For example, if the token evaluates to that's a single quote, then some portion of the code may evaluate to

$token = 'that's a single quote';

which will break on the 's', hence the unexpected 's' (T_STRING)

EricLavault
  • 12,130
  • 3
  • 23
  • 45
  • That makes excellent sense. Is there anyway I can get around it or can this be considered a bug in the module code? – WebMW Mar 16 '21 at 14:58
  • I don't know, I would start by using exclusively single quotes in the code and see if it changes something (I've already seen weird beahaviors when mixing both single and double quotes in evaluated code, also in PHP double quotes are interpreted to expand some special character sequences to their meanings, so if not intended you should avoid using them in general because it can only complicates this kind of parsing issue). – EricLavault Mar 16 '21 at 15:14
  • Also, I don't know if the module requires to include the `` but I would try what happens with/without. – EricLavault Mar 16 '21 at 15:20
  • I tried your recommendation of changing the code to exclusively single quotes but no luck. I also tried voiding the PHP tags despite the direction in the settings and my title becomes "$storename = 'Casey's General Store';$city = 'Fargo';$state = 'North Dakota';if(empty($storename)) { return $city.', '.$state;} else { return $storename.' in '.$city.', '.$state;}" ha, but as you can see, the apostrophe works without PHP tags. – WebMW Mar 16 '21 at 15:24
  • This looks like it may have been fixed 4 years ago and pushed to dev, but never the stable release... https://www.drupal.org/project/auto_entitylabel/issues/2702587 I will try the dev and get back... thank you again for all of your help! – WebMW Mar 16 '21 at 15:34
  • Just to update, switching to dev did not do the trick. – WebMW Mar 16 '21 at 16:10