0

I want to compare a token value with php in display suite and then change it if necessary or just print it however running

<?php 
$tokenText = token_replace("[node:field-product:field-vent-type]");
print "<input type='text' name='test' value='$tokenText'></input>";
if($tokenText == "Floor Vent"){
echo "Flooring Vent";
}else{
echo $tokenText;
}
?> 

prints "Floor Vent"(without quotes) in a textbox then prints it again right next to the textbox(I used the textbox to make sure there weren't any extra spaces or hidden html that I missed when I looked at it in source). It should have printed "Flooring Vent" next to the textbox because on that page the value of the token is equal to "Floor Vent" on another page where the token is "Cold Air Return" it prints correctly as it can go to the else like it's supposed to

Am I missing something their IDENTICAL "Floor Vent" & "Floor Vent" so why is the if statement not returning true?

edit: I also tried running it through krumo() whitch says it's 36 chars yey counting it it's only 10

edit 2: I've also tried running

echo htmlSpecialChars($tokenText, ENT_COMPAT | ENT_HTML401 | ENT_NOQUOTES);

against it and am still seeing the string I am comparing it to

edit 3: ran a Var_dump() on $tokenText and found 36 chars then tried a shorter token and still got 36 chars but no spaces or other hidden chars that I can find

0

Try using strcmp:

// debug text
var_dump($tokenText);
var_dump("Floor Vent");
die();

if(strcmp($tokenText, "Floor Vent")==0) {
    echo "Flooring Vent";
}else{
    echo $tokenText;
}
  • thanks but it's not working either – mcgyvrfan121 Feb 20 '14 at 14:18
  • I've edited my answer - use var_dump to see what the difference is between your 2 strings... – Felix Eve Feb 20 '14 at 14:23
  • my token "[node:field-product:field-vent-type]" is 36 chars and I had the use tokens toggle on and somehow it was converting to print but not in the php – mcgyvrfan121 Feb 20 '14 at 14:37
  • So would strip_tags() or trim() help? – Felix Eve Feb 20 '14 at 14:44
  • Thanks for your help but i found another way around the problem by loading the product and doing everything in php – mcgyvrfan121 Feb 20 '14 at 15:03
0

I found that I could load the product entity and get the variables out of it that I need with:

<?php 
$product = commerce_product_load($entity->field_product['und'][0]['product_id']);
//echo krumo($product);
var_dump($product->field_vent_type['und'][0]['value']);
?>

and that removes my need of tokens at least where they need to be changed

  • Glad you got it sorted :) – Felix Eve Feb 20 '14 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.