Jump to content

Hook add property to argument before method is called


---
 Share

Recommended Posts

Hi,

I have a method (___renderImage) which renders an image tag (<img src) and has a single argument: Pageimage $image.

Now I have a hook called "beforeRenderImage" in which I'd like to add a property to the argument of ___renderImage. So for example, I'd like to add a property "requiredDimension" to the argument before ___renderImage is called.

Is this possible?

Link to comment
Share on other sites

Yes. Put the following code in your /site/ready.php

<?php
$this->addHookBefore('YourClass::renderImage', function ($e) {
  $image = $e->arguments(0);
  $image->requiredDimensions = '100x100';
  $e->arguments(0, $image);
});

or

wire()->addHookBefore('YourClass::renderImage', null, 'beforeRenderImage');

function beforeRenderImage($e) {
  $image = $e->arguments(0);
  $image->requiredDimensions = '100x100';
  $e->arguments(0, $image);
}

 

Read docs about hooks: https://processwire.com/api/hooks/

  • Like 3
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...