Jump to content
VeiJari

Basics of hooking

Recommended Posts

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: 

image.png.696daf301396a34832a7d6a24495b693.png

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!

Share this post


Link to post
Share on other sites

Hi @VeiJari

It's usual practice, but not compulsory, to put hooks into /site/ready.php. site/ready.php loads before _init.php.

You're changing the saved page data so you may need to turn off outputting formatting first if saving the entire page, or simply set-and-save the 'ajasta' field, eg (untested):

wire()->addHookAfter('Pages::published', function($event) {
    $page = $event->arguments('page');

    $t = wire()->templates->get($page->template);
    if($t->name == 'artikkeli' && $t->hasField("ajasta") {
        $page->setAndSave('ajasta', 0);
    }   
    $event->return = $page;
});

 

  • Like 3

Share this post


Link to post
Share on other sites

I guess $pages is not "ready" in _init.php, since _init.php is loaded pretty early. When PW is up and running and before the template is executed, it runs site/ready.php. So placing hooks in ready.php is the recommended practice.

28 minutes ago, psy said:

site/ready.php loads before _init.php

sorry, no.

  • Like 1

Share this post


Link to post
Share on other sites

I'd support the /site/ready.php solution as well. It seems to be the better place for hooks that don't end in a module.

But there is another thing.

Your hooks is tied to the published state or the action of publishing it. While testing this you have to unplublish the page first to test your hook.
Had this testing issue a while back, too. 😉

 

  • Like 1

Share this post


Link to post
Share on other sites

Sure, init.php runs before ready.php as well, but thanks for the link, which explains this more precisely:

Quote

/site/init.php
This file is included during ProcessWire's boot initialization, immediately after autoload modules have been loaded and had their init() methods called. Anything you do in here will behave the same as an init() method on a module. When this file is called, the current $page has not yet been determined. This is an excellent place to attach hooks that don't need to know anything about the current page.

 

  • Like 3

Share this post


Link to post
Share on other sites

Good pick-up @wbmnfktr!

/site/init.php is different to /site/templates/_init.php

I guess the custom of using ready.php is that by that time, PW knows about the current page which in this case, is referenced in the hook.

  • Like 2

Share this post


Link to post
Share on other sites
On 4/11/2019 at 3:27 PM, psy said:

Hi @VeiJari

It's usual practice, but not compulsory, to put hooks into /site/ready.php. site/ready.php loads before _init.php.

You're changing the saved page data so you may need to turn off outputting formatting first if saving the entire page, or simply set-and-save the 'ajasta' field, eg (untested):


wire()->addHookAfter('Pages::published', function($event) {
    $page = $event->arguments('page');

    $t = wire()->templates->get($page->template);
    if($t->name == 'artikkeli' && $t->hasField("ajasta") {
        $page->setAndSave('ajasta', 0);
    }   
    $event->return = $page;
});

 

Tested and the code works!

Thank you for this.

Our customer also wants the field to be disabled when the page is published, how do you achieve this? 

Quick browsing of the API, there doesn't seem to be a disable() function for a field.

Share this post


Link to post
Share on other sites
On 4/11/2019 at 4:02 PM, wbmnfktr said:

Be careful here @Autofahrn and @psy.

@VeiJari is talking about init.php and not _init.php.

  1. init.php
  2. ready.php
  3. finished.php

Just to keep this in mind.

https://processwire.com/blog/posts/processwire-2.6.7-core-updates-and-more/

I was actually talking about /site/templates/_init.php, but after reading the difference of /site/init.php and /site/templates/_init.php, I decided to include psys' solution to site/init.php.

Thank you for this!

  • Like 1

Share this post


Link to post
Share on other sites
3 minutes ago, VeiJari said:

Our customer also wants the field to be disabled when the page is published, how do you achieve this? 

Quick browsing of the API, there doesn't seem to be a disable() function for a field.

I have never used it but you could try to hook into the fields visibility via API in some way.

https://processwire.com/docs/fields/dependencies/

 

Share this post


Link to post
Share on other sites
7 minutes ago, VeiJari said:

Our customer also wants the field to be disabled when the page is published, how do you achieve this? 

Quick browsing of the API, there doesn't seem to be a disable() function for a field.

You are probably looking for the "collapsed" field and its constants.

(collapsedNoLocked to be precise)

  • Like 1

Share this post


Link to post
Share on other sites
46 minutes ago, Autofahrn said:

You are probably looking for the "collapsed" field and its constants.

(collapsedNoLocked to be precise)

I think the solution lies somewhere in that topic, but I haven't figured out how to integrate Robin S' solution for my Pages::published hook.

Share this post


Link to post
Share on other sites
$wire->addHookAfter('ProcessPageEdit::buildForm', function($event) {
  $page = $event->object->getPage();
  if($page->template != 'home') return; // example
  if($page->isUnpublished()) return;
  $form = $event->arguments(0);
  $field = $form->getChildByName('title'); // example
  $field->collapsed = Inputfield::collapsedNoLocked;
});

🙂 

  • Like 5

Share this post


Link to post
Share on other sites
58 minutes ago, bernhard said:

$wire->addHookAfter('ProcessPageEdit::buildForm', function($event) {
  $page = $event->object->getPage();
  if($page->template != 'home') return; // example
  if($page->isUnpublished()) return;
  $form = $event->arguments(0);
  $field = $form->getChildByName('title'); // example
  $field->collapsed = Inputfield::collapsedNoLocked;
});

🙂 

Works like a charm! 

Thank you for this!

And also, I want to thank every one for motivating me to write hooks and seeing how practical and easy they're to make! 🙂

  • Like 5

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 jom
      Hi everyone
      It seems that I don't fully understand the wireTempPath() function and I need some help.
      I use wireTempPath() to create a new location in assets/cache/WireTempDir and than copy a pdf from the assets/files/page folder to the new folder. I want the file to be accessible only for a limited time, that's why I use wireTempPath.
      The file seems to be copied to the right location, but gets deleted right afterwards, according to 
      As mentioned in the topic above, 
      $wireTempDir->setRemove(false); prevents the file to be deleted. But I like the file to be automatically deleted after a few days. So, how can I do that?
      My code so far (everything works, but the automatic removal of the tempDir folder):
      //generate and show download link $folder = time(); // timestamp as temporary folder $maxAge = (int) $settings->options_downloadlink_valid_hours * 3600; //tempDir wants maxAge as seconds $options = array( 'maxAge' => $maxAge ); $wireTempDir = wireTempDir($folder, $options); $wireTempDir->setRemove(false); $src_file = $page->ebook_download->filename; // Create a new directory in ProcessWire's cache dir if(wire('files')->mkdir($wireTempDir, $recursive = true)) { if(wire('files')->copy($src_file, $wireTempDir)){ //get subdirs from tempDir: $pos = strpos($wireTempDir, "WireTempDir"); $subdir = substr($wireTempDir, $pos, 100); $out .= "<p><a href='" . wire('pages')->get('template=passthrough')->httpUrl . "?file=" . $subdir . $page->ebook_download->basename . "' target='_blank'>$page->title</a></p>"; } } I appreciate any ideas - thanks!
      Oliver
    • By Macaco
      It's a bilingual site. There are two pages: "Artists" and "Events" each with a "Page Reference" field connecting each other.
      - Artists has a field where one can choose events available.
      - Events has a field where you can either choose artists available or create new ones.
      The problems: 
      - When I create an "Artist" page and select events from the list, it doesn't update the collection of participating artists on the "Event" page.
      - When I create an artist from the "Event" page. The field 'artist page > settings > language' is not "Active" for the second language.  When the artist page is created manually,"Active" is on by default.
      I know this all have to do with hooks, but I'm don't fully understand the logics.
    • By VeiJari
      Hello forum, this is my first security related post, so I'm a bit of a newbie.
      I understand that when I have direct front-input from user I should sanitize the input, but how about when I use a secret key for showing a API for a third-party supplier? Should I sanitize the input->get() key?
      I've tested this issue and I tried ?key=<?php echo $page->field; ?> And without adding any sanitization it comes back: /?key=<?php%20echo%20$page->field;%20?>
      So can I rely on this, or should I still use $sanitizer just in case?
       
      Thanks for the help!
    • By EyeDentify
      I have been experimenting with the new $page->meta() method and find it useful.

      Once i figured out that the data i "save" with it is tied to the page where i called the method from.

      So this is not obvious at least not for me in the documentation:
      https://processwire.com/api/ref/page/meta/
       
      So i just wanted to share that revelation with the community so you don´t get as confused as i was.

      Happy Coding Everyone.
    • By louisstephens
      Going through my long quest to get better with ajax and utilizing the api, I have hit yet another roadblock. I currently have a form with an image field (thanks to flydev for getting that sorted), "title" text input, and a select field set to multiple. In my ajax call, I added in:
      tags = $("#select-tags").val(); form_data.append('tags', tags); $.ajax({ type: 'POST', data: form_data, contentType: false, processData: false, url: '/ajax/upload-preview/', success: function(data) { console.log("Woo"); }, error: function(xhr, ajaxOptions, thrownError) { alert(xhr.responseText); } }); And in the ajax template: 
      $tags = $sanitizer->text($_POST['tags']); $image = $sanitizer->text($_POST['image']); $p = new Page(); $p->template = "preview"; $p->parent = $pages->get("/previews/"); $p->name = $title; $p->title = $title; $p->tags = $tags; $p->save(); If I select a "tag" from the select input and submit, it does indeed add it to the Page Reference field in the backend. However, this does not work with an array being passed to it of multiple options.

      So it does appear that my ajax call is trying to submit multiple options, but I am really just unsure how to get these two added in. I saw in other forums posts of add($page) and even add(array()). Do I need to handle this js array differently or do  I need to foreach through the $tags to add it like:
      foreach($tags as $tag) { $p->tags->add($tag); $p->save(); } I tried this approach, but apparently I am still missing something.
       
      Edit:
      I was doing some tweaking, and I know I can split the js array out like:
      for (i = 0, len = tags.length; i < len; i++) { console.log(tags[i]); } However, I am not sure then how to handle the POST in php if I were to split it out.
×
×
  • Create New...