Jump to content

Image field: check if first image is under a certain width


OpenBayou
 Share

Recommended Posts

I need to know if it's possible to add requirements to the image field. Specifically, if the image field has two images and if the first image is under 200px wide. If those requirements aren't met, the post doesn't publish.

 

Thanks for the help.

Link to comment
Share on other sites

12 hours ago, OpenBayou said:

Is this for the template or for the field? Can't seem to find where to go.

Depends on what you mean by...

22 hours ago, OpenBayou said:

..... the post doesn't publish.

What do you mean by 'doesn't publish'? Are you talking about the backend, in the PW admin? If yes, then the code has to go in a Hook, either in an autoload module or somewhere like admin.php

 

Link to comment
Share on other sites

i can show a simple module to do this, the only thing delaying me right now is figuring out how to prevent the publish - I guess if you hook into saveReady, then you can check to see if the page status is published (since the request would have that set) and then if the image requirements are not met, you would set the status to unpublished, overriding the request; haven't tested this yet but will do so soon..

 

ok here is a try - note that once you install this module, you can keep adding stuff to it by adding your hooks to the init and then your functions below.

Spoiler

 


class SiteUtilities extends WireData implements Module {

	/**
	 * getModuleInfo is a module required by all modules to tell ProcessWire about them
	 *
	 * @return array
	 *
	 */
	public static function getModuleInfo() {

		return array(
			'title' => 'Basic Site Utilities',
			'version' => 1,
			'summary' => 'Basic site utilities for admin pages.',
			'singular' => true,
			'autoload' => true,
			'icon' => 'cogs',
			);
	}

	/**
	 * Initialize the module
	 *
	 * ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
	 * when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
	 *
	 */
	public function init() {
		$this->pages->addHookAfter('saveReady', $this, 'checkImages');
	}

	/**
	 * check the image
	 */
	public function checkImages($event) {

		$page = $event->arguments[0];
		if($page->template != 'some-template') return;
        // note it will probably be also necessary to check if the page is new here
        // additional testing and troubleshooting should be performed before using this on a live site
		if($page->isChanged('status') && $page->imagefield->first()->width < 200) {
			$page->addStatus(Page::statusUnpublished);
			$this->error('First image is less than 200px width: page not published.');
		}

	} // end function

} // end class

 

 

  • Like 3
Link to comment
Share on other sites

Thanks for the help but this is beyond my skills set. I don't know how to do a module and when I tried to one, I got an error code saying the module is empty. This may be something I could mess around with later on.

Thanks again.

Link to comment
Share on other sites

A way you could do this without any code:

1. Create one image field for the first image and one image field for the second/subsequent image(s).

2. Set "Min width for uploaded images" to 200 for the first image field. Incidentally, your original post says that the first image should be under 200 pixels wide, but that doesn't make sense so I assume you mean over 200 pixels wide.

3. Make both image fields "required".

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