0

I purchased a Drupal theme and the the support is...lacking. So I thought I'd try and tackle it myself. I'm relatively new to PHP programming, so please take it easy on me :)

The error I got was: Notice: Object of class Drupal\Core\Field\FieldItemList could not be converted to int in theme_css_alter()

Digging into the error, it came from the first line of code in this if statement:

if(isset($node->nid) && $node->nid == 90){
    //do stuff
 }

I did my research and found that its a PHP error when using the == operator, likely since $node->nid is being fetched as a string, and 90 is an integer, and it can't convert the nid on the fly.

Possible solutions I found while Googling were either making a 'getter' to fetch the nid as an integer (which sounds more complicated than necessary), using the === operator; and I'm guessing on my own that if I convert 90 to a string it would also work?

Now, doing a test run, === does stop the error from showing, but my research shows that === only works when both the value and type are equal, and given I'm comparing a string and an integer, I assume that it would always just be false then.

So...

  1. Am I correct in saying that in PHP 90 (as a string) does not == 90 (as an integer)?
  2. Am I correct in saying that using === instead of == is NOT the correct way to compare a string and integer?
  3. Would $node->nid == (string)90{ be the correct way to compare for this if statement? What is the most correct way to do this comparison?
3
  • Comparing strings with numbers generally does what you expect. The error message indicates that $node->nid is a FieldItemList, not a string. Commented Feb 15, 2017 at 23:19
  • Thanks Barmar. I assumed nid would be a string because thats what it normally is elsewhere and that FieldItemList was just the PHP class in Drupal 8. Is that not correct either? Commented Feb 15, 2017 at 23:22
  • It might have a method to convert to a string, so you can use it where a string is needed, but it's not actually a string. Commented Feb 15, 2017 at 23:27

3 Answers 3

1

When you compare a number with something else, the other thing is first converted to a number, and then these two numbers are compared. So a comparison like "90" == 90 does what you expect -- it's equivalent to 90 == 90.

The problem you're having is that $node->nid is not a string, it's an object of class Drupal\Core\Field\FieldItemList. The class doesn't provide a method to convert this type to a number, so it's not able to perform the comparison.

However, it appears that this type has a method to convert to string. But PHP won't do a double conversion when performing the comparison. So you need to perform the object->string conversion explicitly, and then PHP will convert the string to an integer.

if (isset($node->nid) && (string)$node->nid == 90)
Sign up to request clarification or add additional context in comments.

3 Comments

That sounds right. Thanks. I guess that makes it more of a Drupal thing than a PHP thing. I'm new here, should I change the question and open a new one at drupal.stackexchange.com?
I've just added a solution.
I've marked this as a solution since its technically correct with the questions I've asked. Unfortunately FieldItemList can't be converted to a string either, but now I know why this line is failing I can do some more digging. (FYI, I think the reason for my confusion is changes between Drupal 7 and 8; I think D7 you could call it with the solution you proposed...)
0

A string will not be equal to an integer. You need to check for the type too. That is why we use the triple equal sign ===. Consider the following code:

$integer = 90;
$string = "90";

echo($integer == $string); // prints 1 (true)
echo($integer === $string); // prints 0 (false)

You can test your own variables in this page.

1 Comment

Your code contradicts what you wrote in the answer. The first one prints true, showing that a string can be equal to an integer when you use ==.
0

Barmar is correct on why this if statement is failing; the comparison is working but $node->nid is not a string or an integer. I'm adding this answer as additional reference for the Drupal side of things.

As reference: https://drupal.stackexchange.com/questions/145823/how-do-i-get-the-current-node-id

Drupal 8 has changed the way to access nid values. Ultimately, the reason is that the parameter is upcasted to a full node object when you try to access it, so its neither a string or integer.

The correct way to do the comparison would be to use either $node->nid->value or $node->id(). Alternative ways to fetch the id as a value are in the link.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.