Jump to content

I think this tutroial is not working anymore on Module Development!


sam13579
 Share

Recommended Posts

Hi,

`$this->addHook()` give you the ability to define a custom method that can be available in the class object given as a parameter.

 $this->addHook('Page::summarize',     $this,            'summarize');
                   |       |              |                   |- (name of the method defined in the current object (module, class..)
                   |       |              | 
                   |       |              |- current object where the method is defined (module, class)
                   |       | 
                   |       |- the name is mandatory, 
                   |          you will call the method given as third param from the Page $page object ($page->summarize()).
                   |
				   |- the object where the new method will be added to

 

In the example of the `summarize()` method you are talking about, it expect a method `summarize()` to be defined in an autoload module. But you are not required to write a module just to define a new method to an existing class. You can write it outside a class, for example, in the file `init.php` by writing the following:

$this->addHook('Page::summarize', function ($event) {
 // the $event->object represents the object hooked (Page)
 $page = $event->object;
 // first argument is the optional max length
 $maxlen = $event->arguments(0);
 // if no $maxlen was present, we'll use a default of 200
 if(!$maxlen) $maxlen = 200;
 // use sanitizer truncate method to create a summary
 $summary = $this->sanitizer->truncate($page->body, $maxlen);
 // populate $summary to $event->return, the return value
 $event->return = $summary;
});

// or by defining the function... 

function summarize_2($event) {
  // the $event->object represents the object hooked (Page)
  $page = $event->object;
  // first argument is the optional max length
  $maxlen = $event->arguments(0);
  // if no $maxlen was present, we'll use a default of 200
  if(!$maxlen) $maxlen = 200;
  // use sanitizer truncate method to create a summary
  $summary = wire()->sanitizer->truncate($page->body, $maxlen);
  // populate $summary to $event->return, the return value
  $event->return = $summary;
 };

// ... and giving the name of the function to the hook.
// note the `null` parameter used to tell the hook we are not using it from a class object
$this->addHook('Page::summarize_2', null, 'summarize_2');

You will be using a hook where `after` or `before` doesn't matter, to define a new method `summarize()` to the existing class `Page`.

So in your template, eg. `home.php` or `basic-page.php`, you can then call both methods from the `$page` api variable.

<?php namespace ProcessWire;

// Template home.php

// if the `body` field content is: "The body field of this page summarized in less than 200 characters, or it will be truncated."
// calls of the new defined methods `summarize()` and `summarize_2()` will print the content of the `body` field.

echo $page->summarize();

echo $page->summarize_2();

 

If you want to test it from a module, then is quite simple (note we replace the `null` param by `$this` to tell the hook we are using it from the current object class (themodule): 

class ExampleSummarize extends WireData implements Module {
  public static function getModuleInfo() {
    return [
      'title' => 'Example Summarize module',
      'version' => 1,
      'summary' => 'Add a new method `summarize()` to page object.',
      'autoload' => true,
    ];
  }
	
  function summarize($event) {
  // the $event->object represents the object hooked (Page)
  $page = $event->object;
  // first argument is the optional max length
  $maxlen = $event->arguments(0);
  // if no $maxlen was present, we'll use a default of 200
  if(!$maxlen) $maxlen = 200;
  // use sanitizer truncate method to create a summary
  $summary = wire()->sanitizer->truncate($page->body, $maxlen);
  // populate $summary to $event->return, the return value
  $event->return = $summary;
 };

  public function init() {
    $this->addHook('Page::summarize', $this, 'summarize');
  }
}

If you write this code in a file `ExampleSummarize.module` and from the backend you refresh modules and install this module, the method `summarize()` will be available by calling `$page->summarize()` from where you want.

 

You can also find more informations and lot of details there:

https://processwire.com/docs/modules/hooks/

 

 

 

  • Like 7
  • Thanks 1
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.
×
×
  • Create New...