monollonom Posted January 8, 2021 Posted January 8, 2021 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.
adrian Posted January 8, 2021 Posted January 8, 2021 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. 1 1
monollonom Posted January 9, 2021 Author Posted January 9, 2021 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 ! 2
adrian Posted January 9, 2021 Posted January 9, 2021 @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.
teppo Posted January 9, 2021 Posted January 9, 2021 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 ? 1
monollonom Posted January 9, 2021 Author Posted January 9, 2021 Ok I see, thanks @teppo for the informations. In my case it's no big issue to go this way (limiting to ProcessPageEdit) but I don't mind changing back to @adrian's condition in case I come across other "Page editors" like you said.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now