Jump to content
Chris

select field type who uses images for preview changes

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? 

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

thank you verry much guys! i will try this stuff - should be added to the modules section of the pw page, if someone is looking for something like this to =)

Share this post


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

Share this post


Link to post
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");

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Pip
      Hi, Everyone! 
      I'm currently working on a page reference field and set it for multiple pages (AsmSelect) for the input. Is there a way for me to add an image field (aka Avatar) and the title of page in the radio button? 
      I used the field name enclosed in the { }. Didn't work. It appeared a text instead. 
      Thanks in advance and hope to hear from you soon!
    • By EyeDentify
      Hello dear PW gurus.

      I have stumbled over a strange error that i all of sudden got when trying to upload an image to a images field on a page.
      There where images allready stored in the field that i wanted to keep, but during the upload the error apear and after that all images are gone from the field and i can´t upload any, i just get the error every time.
      I am running ProcessWire 3.0.153 dev.
      Update:
      After looking in the assets folder i find the folder for the page and the image files seems to be there including the ones i tried to upload when the error occured.
      But they don´t show up in the images field in the page editor.
       
      The error reported:
       
      SQLSTATE[01000]: Warning: 1265 Data truncated for column 'ratio' at row 1 And here is a screenshot of the event:

      The TracyDebugger Error reporting:

      I hope you fine folks could point me in a direction.
      But it seems our old pal set_time_limit() is back.

      Regards, EyeDentify
    • By rushy
      Hi all
      Continuing my first project where I am creating and manipulating stuff from the frontend. Till now I've always added things like images from the backend, but in this project I need to add and move images from a frontend control. This is a photo album where images are stored in albums, each album being a page containing a Pageimages array in the usual way. So what I want to know is how do I move an image from one album (page) to another in the frontend? I just need some guidance on how to approach this.  I suppose I need to do a copy and delete - but how do I copy an image or images from one page to another? What function should I use to create a new image on an existing page? I include a code snippet from the server side of my delete image request and it works fine. I'd like to implement something similar for a move and upload new image request. 
      Many thanks for any help. Paul 
      <?php namespace ProcessWire; // sanitize inputs as 1-line text $action = $sanitizer->text($input->post('action')); $instr = $sanitizer->text($input->post('input')); $sel = $input->post('selected'); // Expect JSON for image selected image list $selected = json_decode($sel); $nosel = count($selected); $response = array(); // for building JSON response switch($action) { // delete selected images case 'delete': $out = "<p>Deleted $nosel image(s)</p>"; foreach($selected as $item){ $album = $pages->get($item->album); $album->of(false); $out .= "<p>Image {$item->file} from album {$album->title}</p>"; $album->images->delete($item->file); $album->of(true); } $out .= saveUpdatedAlbums($pages, $selected); // add the response message for the delete $response['message'] = $out; break; ...... // save any album that had an image deleted function saveUpdatedAlbums($pages, $selected) { $cur = ''; $out = ''; foreach($selected as $item){ $album = $pages->get($item->album); if($album->id != $cur) { $album->of(false); $album->save('images'); $album->of(true); $cur = $album->id; $out .= "<p>Updated album {$pages->get($cur)->title}</p>"; } } return $out; }  
       
       
    • By DV-JF
      Hey all, hope you're feeling well these days!
      Short question: Under "Setup > Templates" it's possible to sort the templates by last modified. Is this possible for fields view to?
      Long explanation:
      Currently I am in the process of programming a reusable template and often have to copy several fields & templates from different PW installations into my "master version".
      Therefore it would be good to see already in the field list when the last modification has taken place.
      Stay healthy & don't forget to wash you hands - many greets!
×
×
  • Create New...