Jump to content
tarkvsg

tell me about hooks in ready.php

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By VeiJari
      Hello forum! 
      I started to write my first hook for Processwire but I'm pretty confused how you should write these. My idea is to hook after publishing in init.php (called before templates)  for a certain template. 
       
      Here's the code: 

      Trying to reset the checkbox (ajasta) and then saving it so it shows unchecked in the admin page when publishing "article" page. 
      But the code isn't doing anything. Not even dumping anything
      What seems to be the problem? And have you made a similar hook for this usage or am I doing it totally wrong? 😄
      Thanks for the support in advance!
    • By celfred
      Hello,
      I am getting nuts trying to understand hooks and I hope someone in the community will be able to help. This is deiving me crazy ! I have tested tens of possibilities to eventually reduce my code to this :
        bd('outside');   $wire->addHookAfter('Page::render', function($event) {     bd('inside');   }); And if someone could tell me why my bd('inside'); never triggers... I would be infinitely grateful !
      EDIT : Forgot to say : this piece of code is in my _init.php included in my template (but I've also tried in my site/ready.php for no better results...)
    • By NorbertH
      Is there a hook to do something right after cloning a page ?
      I want the page to be saved right after cloning it either from the button in the tree or from a lister, because saving the page triggers several calculations that are not triggered by just cloning the page.
       
      Thanks in advance !
    • By horst
      Hey,
      I want to hook into the 404 handling of PW at the most earliest stage. Pleas can anybody point me to the hook or file that suits this best?
    • By Sanyaissues
      Hi, I have a page with a repeater called teams with two fields: team (a pageReference with a list of teams) and people (a pageReference with a list of people who likes this team).
      I'm trying to populate the people field in the repeater according to the value of the team field in the same repeater. So far. I'm able to get the pageArray that I want to assign to the people field, but i don't know how to "save" the value for each instance of the repeater.
      I hope somebody can give me a light. Thanks in advance.
       
      $wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) { if($event->object->hasField == 'people') { $repeaterField = $event->arguments('page')->teams; foreach ($repeaterField as $t) { // Is this the way to loop the instances of the repeater? $team = $t->team->id; $t->people = $event->pages->find("template=user, team={$team}"); var_dump($t->people); // This returns the values that i want to assign for each repeate instance $event->return = $t->people; // I hope this assign a custom pageArray for each repeater, but it assigns the same for all the instances of the repeater } } });  
×
×
  • Create New...