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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By nuel
      Hi
      I'm working with PW for some time now, but – please don't laugh – never used hooks.
      I have 200 – 300 subpages with the template 'artist', the title of each page represents the full name, sometimes John Doe, sometimes John Emmet Brown Doe. I want to auto-populate the field 'artist_last_name' with the word that is most likely the last name, the last word of the page title. If there are more names, it can be changed manually. I need this for easier and quicker alphabetical sorting.
      What I put together is this, in ready.php. Doesn't work. Again, I'm new to hooks..
      <?php     $wire->addHookBefore('Pages::saved', function(HookEvent $event) {     $p = $event->arguments('page');     if($p->template != 'artist') return;     if($p->artist_last_name) return;     $ln = end(explode(' ', $p->title));     $p->set('artist_last_name',$ln);     }); ?> In the best case this would be done once globally without having to open and save every page. But new pages should populate the field on page save.
      Thanks for your help, I think it's easy, I just need a push..
      Nuél
    • By picarica
      So hello i am trying to get a .png file from file field and put it automatically to image field, why png image is in the file field is because i already have a hook that extracts .zip and uploads all content into file field, but i just realizes i cant use size() function on image in file field so i am tryin got reupload it to images field
      i already have something like  this in ready.php
      $word = ".png"; foreach($page->subor_hry as $file) { if(strpos($file, $word) !== false){ $page->images_thumb = $file->url; } } by my logic it should work but it dosnt i get error ProcessPageEdit: Unable to read: /site-hry/assets/files/1027/flash_fishy_screenshot.png
      when i remove url from $file->url i just get ProcessPageEdit: Item added to ProcessWire\Pageimages is not an allowed type
      so what am i doing wrong? is there some other way to do this ?
      also can i have all this in
      $this->addHookAfter('Pages::saveReady', function(HookEvent $event) { whats the correct function to have it apply on all pages ?
    • By rooofl
      Hi! I am trying to create a hook that takes the value of a form file field, and add it to an image gallery of an existing page.
      $forms->addHookBefore('FormBuilderProcessor::processInputDone', function($e) { $form = $e->arguments(0); if($form->name == 'new-inuse') { // related_font is a page selector $font = $form->getChildByName('related_font'); // getting the page name $fontName = $font->attr('value')->name; // finds the page from its name $fontPage = wire('pages')->find("name=$fontName"); // in_use_image is a file upload field $image = $form->getChildByName('in_use_image'); // trying to add the image to the existing image gallery in_use field $fontPage->in_use->add('$image'); // error here $fontPage->save(); } }); This outputs the following error: `Call to a member function add() on null`. Any idea what’s wrong with my code?
    • By picarica
      so what i am trying to do is that i uploded some files into file field, and then i want hook to get MD5sum and other stuff from .xml and put it into text field into it coresponding pages, pages like this are gonna be many, for each one i want it to output it into its fields
    • By Paul Greinke
      Hi there. I wrote a custom module for one of my projects. In fact I maybe want to use my module in other projects too. In order to be variable and customizable  I need to implement some custom hooks into my module. So I can afterwards hook into the my functions in order to modify them to match the needs of the new project.
      I tried simply defining functions with the '__' prefix. But that did not work. I'm imagining something like the following:
      <?php class MyClass { public function ___someFunction() { // Do something } } // ready.php $this->addHookBefore('MyClass::someFunction', function($event) { // some customization }); Is there a way to accomplish that? 
×
×
  • Create New...