Jump to content

select field type who uses images for preview changes


Chris
 Share

Recommended Posts

short story:

think of a radio button mechanic in the background, but ui-wise you present images with captions as "buttons".

for instance i whant to be able to let admins change some visual themes of a single page, with different background and formating for each, i could place screenshots thumbs with a short caption. so the admins are able to se a preview image and know better what this setting does.

therefore it would be needed to define images for an input field, during the field creation. maybe someone likes the idea and is able to do it? 

Link to comment
Share on other sites

Or this:

class InputfieldSelectThumbnail extends InputfieldSelect {

	/**
	 * Return information about this module
	 *
	 */
	public static function getModuleInfo() {
		return array(
			'title' => __('InputfieldSelectThumbnail', __FILE__), // Module Title
			'summary' => __('Select a page from a list that displays a thumbnail of the first image found in the page', __FILE__), // Module Summary
			'version' => 101,
			'autoload' => true
			);
	}

	public function init()
	{
		$this->addHookBefore("InputfieldPage::getConfigInputfields", $this, "addOwnClassToSelect");
	}

	public function addOwnClassToSelect($event)
	{
		$obj = $event->object;
		$classes = $obj->get("inputfieldClasses");
		$classes[] = "InputfieldSelectThumbnail";
		$obj->set("inputfieldClasses", $classes);
		$this->log->message("addOwnClassToSelect: InputfieldSelectThumbnail");
	}

	protected function renderOptions($options, $allowBlank = true) {

		$out = '';
		reset($options); 
		$key = key($options); 
		$hasBlankOption = empty($key); 
		if($allowBlank && !$this->required && !$this->attr('multiple') && !$hasBlankOption) $out .= "<option value=''> </option>"; 

		foreach($options as $value => $label) {

			$selected = $this->isOptionSelected($value) ? " selected='selected'" : '';
			$attrs = $this->getOptionAttributesString($value) . " " . $this->getThumbnailAttributes($value);
			$out .= "\n\t<option$selected $attrs value='" . htmlspecialchars($value, ENT_QUOTES, "UTF-8") . "'>" . $this->entityEncode($label) . "</option>";
		}

		return $out; 
	}

	protected function getThumbnailAttributes($pgid)
	{
		$this->log->message("getThumbnailAttributes({$pgid})");
		$pg = $this->pages->get($pgid);
		if( $pg instanceof NullPage ) return "";

		$fields = $pg->template->fields;
		$imgfield = null;
		foreach($fields as $fld) {
			if( $fld->getInputfield($pg, $fld) instanceof InputfieldImage ) {
				$imgfield = $fld;
				break;
			}
		}
		if( $imgfield == null ) return "";
		
		$img = $pg->get($imgfield->name);
		if( $img == null ) return "";
		
		if( is_array($img) ) {
			if( count($img) == 0 ) return "";
			$img = $img[0];
		} else if( $img instanceof WireArray ) {
			if( $img->count() == 0 ) return "";
			$img = $img->first();
		}
		
		$thumb = $img->size($this->maxThumbWidth, $this->maxThumbHeight);
		$width = $this->maxThumbWidth + 4;
		$height = $this->maxThumbHeight + 4;
		$attrs = "style='padding-left: {$width}px; height: {$height}px; background-image: url({$thumb->url}); background-position: 2px 2px; background-repeat: no-repeat;'";
		return $attrs;
	}

	public function ___getConfigInputfields() {

		$inputfields = parent::___getConfigInputfields();

		$fieldset = $this->modules->get('InputfieldFieldset');
		$fieldset->label = $this->_('Thumbnail options'); 
		$fieldset->attr('name', 'thumboptions'); 
		$fieldset->icon = 'image';

		$field = $this->modules->get('InputfieldInteger');
		$field->label = $this->_('Max. width');
		$field->attr('name', 'maxThumbWidth');
		$field->attr('value', $this->maxThumbWidth ?: 80);
		$field->columnWidth = 50;
		$fieldset->add($field);

		$field = $this->modules->get('InputfieldInteger');
		$field->label = $this->_('Max. height');
		$field->attr('name', 'maxThumbHeight');
		$field->attr('value', $this->maxThumbHeight ?: 80);
		$field->columnWidth = 50;
		$fieldset->add($field);
		
		$inputfields->add($fieldset);
		return $inputfields;
	}
}

If picked as the input field type for a page field, it adds a thumbnail from the first image found in each selectable page to the dropdown (by resizing that image and setting it as the option's background image through css).

  • Like 3
Link to comment
Share on other sites

Here you go. Images need to go into /site/templates/ImageSelect/[path/] and must be named [optionValue].jpg. For usage with FieldtypePage you need to add the module in the settings of InputfieldPage. For the newer options fieldtype it's available right away.

Edit: My version might be more suited to the usecase of presenting layout options to the admins, rather than having a full blown thumbnail display.

Archiv.zip

  • Like 3
  • Thanks 2
Link to comment
Share on other sites

Here you go. Images need to go into /site/templates/ImageSelect/[path/] and must be named [optionValue].jpg. For usage with FieldtypePage you need to add the module in the settings of InputfieldPage. For the newer options fieldtype it's available right away.

Edit: My version might be more suited to the usecase of presenting layout options to the admins, rather than having a full blown thumbnail display.

LostKobrakai, this solutuin fitts my needs well, thank you verry much!

i have some little issues. Images are requested like this 

<img class="image_picker_image" src="/site/templates/ImageSelect/3.jpg">

which will not work for me for two reasons

1. my page is in a subfolder currently e.g. www.awesome.com/nice/site/.... which is not reflected by the url

2. when one liked to use several versions of the field, it could be a little akward, becouse all images are in one directory level, so i would end up keeping track of which id'S are already used by other fields. i suggest to put images in a subfolder with the name of the field, e.g. site/templates/ImageSelect/coolFieldName/1.jpg and so on.

Link to comment
Share on other sites

You can already define custom subfolders in the field settings (at least you should be) and here's the patch to have subfolders supported:

--- /InputfieldImageSelect/InputfieldImageSelect.module
+++ /InputfieldImageSelect/InputfieldImageSelect.module
@@ -13,7 +13,7 @@
 			if(empty($this->optionAttributes[$value])){
 				$this->optionAttributes[$value] = array();
 			}
-			$this->optionAttributes[$value]["data-img-src"] = "/site/templates/ImageSelect/$path$value.jpg";
+			$this->optionAttributes[$value]["data-img-src"] = $this->config->urls->templates . "ImageSelect/$path$value.jpg";
 		}
 
 		if($this->showLabels) $this->addClass("InputfieldImageSelectShowLabels");

Link to comment
Share on other sites

  • 3 years later...
On 1/9/2016 at 3:50 AM, LostKobrakai said:

Here you go. Images need to go into /site/templates/ImageSelect/[path/] and must be named [optionValue].jpg. For usage with FieldtypePage you need to add the module in the settings of InputfieldPage. For the newer options fieldtype it's available right away.

Edit: My version might be more suited to the usecase of presenting layout options to the admins, rather than having a full blown thumbnail display.

Thanks @LostKobrakai, this is great, exactly what I needed!

Just posting a note anyone else using in modern times, I had problems with this inside a matrix repeater with dynamic content. The version of image-picker from 2016 has this line. Straight jquery click doesn't handle event delegation, so for dynamic content you need to use on( "click", ...).  The latest version of image-picker fixes this and swapping out the files in the download linked by LostKobrakai above works fine.

https://github.com/rvera/image-picker/releases

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

×
×
  • Create New...