Jump to content

Config variables [multi dimensional array] references in modules?


darrenc
 Share

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!

Link to comment
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
Link to comment
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"]);
			}
		}

 

Link to comment
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
Link to comment
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.

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