Jump to content

[SOLVED] "InputfieldFile::fileAdded" hook not fired when using $field->add()


monollonom
 Share

Recommended Posts

Hi,

I'm trying to generate variations (for responsive images) when uploading from both the admin and the API, using @Soma hook. However when adding a file using $imageField->add("url"), it doesn't seem to trigger the hook.

Am I doing something wrong here ? Should I use another hook ?

Thanks.

Link to comment
Share on other sites

For images added via the API, you need Pagefile::install

This block from my CustomUploadNames module might be helpful in seeing how I handle admin vs API uploaded images.

https://github.com/adrianbj/CustomUploadNames/blob/9c896717459d1674c8ca37731c8a6f79730436a7/ProcessCustomUploadNames.module.php#L92-L103

and you'll also see differences in the customRenameUploads() method below.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Thanks for the pointer @adrian ! Here is what I ended up with :

<?php
class ImageCreateVariations extends WireData implements Module {

	public static function getModuleInfo() {
		return array(
			"title" => "ImageCreateVariations",
			"version" => 100,
			"summary" => "",
			"href" => "",
			"singular" => true,
			"autoload" => true
		);
	}

	public function init() {
		if ($this->wire('page')->process == "ProcessPageEdit") {
			$this->addHookAfter("InputfieldFile::fileAdded", $this, "sizeImage");
		} else {
			$this->addHookAfter("Pagefile::install", $this, "sizeImage");
		}
	}

	public function sizeImage($event) {
		$inputfield = $event->object;
		if ($event->method == "install") {
			$inputfield = $inputfield->pagefiles->getField();
		}
		if ($inputfield->name != "gallery" && $inputfield->name != "image") return;
		if ($event->method == "install") {
			$image = $event->object;
		} else {
			$image = $event->arguments(0);
		}
		$sizes = [300, 600, 1200, 1800];
		foreach ($sizes as $size) {
			$retina = $size * 2;
			if ($retina >= $image->width) {
				$image->width(floor($image->width / 2));
				break;
			}
			$image->width($size);
			$image->width($retina);
		}
	}
}

I simplified your condition in line 94, but maybe it's the wrong thing to do ?

Anyway it now works as expected so thanks again !

  • Like 2
Link to comment
Share on other sites

@monollonom - I think your simplified condition is fine. That was the first PW module I ever built and I am pretty sure I copied that condition from somewhere on the forum and probably didn't fully understand it at the time. And while I have updated the module considerably since I created it, that is one thing I hadn't really noticed as being unnecessary. I'll probably clean it up in the next version.

Link to comment
Share on other sites

2 hours ago, monollonom said:

I simplified your condition in line 94, but maybe it's the wrong thing to do ?

This depends whether you want to support ProcessProfile and potentially other "Page editors" in addition to ProcessPageEdit: if you check the process name specifically, this hook won't be attached in those cases. If it should be attached for other Process modules as well, you do indeed want to compare against the class_implements() array or alternatively check for "instanceof WirePageEditor".

Admittedly I'm not particularly familiar with your code or Adrian's module, so may have missed something obvious ?

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