Jump to content
darrenc

Config variables [multi dimensional array] references in modules?

Recommended Posts

Somatonic made a module to make image variations, of a custom size, upon uploading them. https://gist.github.com/somatonic/5685631 

It works great but it has values hardcoded into the code.

        $image->size(120,120);
        $image->size(1000,0);

I thought that it might make sense to use the config.php to create a multidimensional array for my thumbnail sizes, that way I could reference them from the module as well as my template. 

// in config.php
$config->imageSizes["foo"] = ["width" => 12, "height" => 34];
$config->imageSizes["bar"] = ["width" => 56, "height" => 78];

But back in my module, when I try to loop through $config->imageSizes or $this->config->imageSizes I seem to get absolutely nothing. I'm also not the best programmer so I'm having a hard time figuring out how I can debug something like this that only fires when an image gets uploaded - I tried wire("log")->save("debug", $myOutput) which could be not the best move.

Any help appreciated - the goal is to have one reference set of sizes and then be able to refer to it in the module as well as my templates. Thanks!

Share this post


Link to post
Share on other sites

It seems you must define the multidimensional array in $config in a single statement:

$config->imageSizes = [
    "foo" => ["width" => 12, "height" => 34],
    "bar" => ["width" => 56, "height" => 78],
];

Someone else may know the reason for this.

  • Like 2

Share this post


Link to post
Share on other sites

Thanks Robin, I'm sort of confused why that mattered but you're right that I now have a proper multidimensional array...

However now, upon upload, the image sizes get created but the upload thumbnail continues to hang in the "working" state http://imgur.com/a/ZkWco

I can't save the page and obviously a reload dumps the images. Any thoughts? My code:

		// return empty array if not set
		$sizes = isset($this->config->imageSizes) ? $this->config->imageSizes : [];
		// loop through the sizes and create the images
		if (count($sizes)) {
			foreach ($sizes as $size) {
				$image->size($size["width"], $size["height"]);
			}
		}

 

Share this post


Link to post
Share on other sites

This is working for me (PW 3.0.40)...

/site/config.php

$config->imageSizes = [
	"foo" => ["width" => 200, "height" => 200],
	"bar" => ["width" => 400, "height" => 0],
];

/site/ready.php

$this->addHookAfter('InputfieldFile::fileAdded', function($event) {
	$inputfield = $event->object;
	if($inputfield->name != 'images') return; // needs to match the name of your images field
	// alternatively, to use for all image fields...
	// if($inputfield->class != 'FieldtypeImage') return;
	$image = $event->argumentsByName("pagefile");
	$sizes = isset($this->config->imageSizes) ? $this->config->imageSizes : [];
	if(count($sizes)) {
		foreach($sizes as $size) {
			$image->size($size["width"], $size["height"]);
		}
	}
});

You won't see the variations immediately inside PageEdit, but you can see them after you save the page.

  • Like 3

Share this post


Link to post
Share on other sites

Thanks so much Robin, that definitely does work for me as well. I wish I understood the "why" of this a bit better (the array weirdness, and why ready.php worked differently from the module when not hard-coded), but I guess I'll plow ahead and return to this later.

Thanks again for your help.

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.

×
×
  • Create New...