Jump to content

tell me about hooks in ready.php


tarkvsg
 Share

Recommended Posts

Hi!
tell me, pls, how to execute this equality (from ready.php)

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
    if($event->object->hasField == 'FIELDNAME')
    {
        .....
    }
}

- $hasField The Field object associated with this Inputfield (from definition parent class for InputfieldPage)

the left side equation is the object entity, and the right side - the string 
How it is?

  • Like 1
Link to comment
Share on other sites

Hi @tarkvsg, welcome to the PW forums.

2 hours ago, tarkvsg said:

$hasField The Field object associated with this Inputfield (from definition parent class for InputfieldPage)

the left side equation is the object entity, and the right side - the string 
How it is?

It works because the comparison uses the == "equal to" and not the === "identical to" operator.

The PHP manual explains that == means "TRUE if $a is equal to $b after type juggling". In the case of the comparison...

$event->object->hasField == 'FIELDNAME'

...'FIELDNAME' is a string so a comparison is made to the string value of $event->object->hasField. And the string value of a Field object is the field's name. See the __toString() method of the Field class here: 
https://github.com/processwire/processwire/blob/649d2569abc10bac43e98ca98db474dd3d6603ca/wire/core/Field.php#L1097-L1103

 

  • Like 5
Link to comment
Share on other sites

2 hours ago, tarkvsg said:

How i could forget about php type juggling?

Hi, I don't know if this question is ironically made for your self or if is a real one. Anyway just incase, the answer is easy: Simply add a single character.

Always use === instead of == (or !== instead of !=) when comparing so if the types differs PHP will not attempt conversions and will return FALSE. Do not use the == until is really required, knowing what you are doing.

Charts: https://github.com/sentientmachine/php_equality_charts

 

  • Like 1
Link to comment
Share on other sites

5 hours ago, flydev said:

Hi, I don't know if this question is ironically made for your self or if is a real one. Anyway just incase, the answer is easy: Simply add a single character.

Always use === instead of == (or !== instead of !=) when comparing so if the types differs PHP will not attempt conversions and will return FALSE. Do not use the == until is really required, knowing what you are doing.

Charts: https://github.com/sentientmachine/php_equality_charts

 

Ofcause ironically. it was a newbie quastion and i was to guessed someself. Thanks again.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...