Jump to content

Tinymce field in a repeater insert image plugin open repeater forPage instead of current repeater item page


Recommended Posts

Posted

I have a Tinymce textarea field reside in a repeater. I want to insert an image to the editor and I click the insert image button. It open a modal show images from the current repeater item page. Of course there is no images inside the page. I need to click the "change" link and then find the page I have the image,

Is it possible to make the Tinymce insert image plugin to show repeater forPage?

Gideon 

  • 3 weeks later...
Posted (edited)

The ___execute() method in ProcessPageEditImageSelect core module is hookable, but I don't know how to change the form page_id via hook.

Edited by hellomoto
Posted

This works to replace the ___execute method but hooking from its own module would be a much better & drier approach but I don't know how:


	public function ___execute() {

		if($this->config->demo) throw new WireException("Sorry, image editing functions are disabled in demo mode"); 
		
		if(!$this->page) {
			$error = "No page provided";
			$this->error($error);
			return "<p>$error</p>";
		}

		$sanitizer = $this->wire()->sanitizer;
		$modules = $this->wire()->modules;
		$input = $this->wire()->input;
		$images = $this->getImages($this->page, $this->page->fields);

		// locate any image fields
		$imageFields = $this->getImageFields($this->page);
		if (!wireCount($imageFields)) {
			$this->page = wire('pages')->get(@$this->data['defaultPage']) ?: $this->page;

			$images = $this->getImages($this->page, $this->page->fields);

			// locate image fields from default
			$imageFields = $this->getImageFields($this->page);
		}
		if(wireCount($imageFields)) {
			$imageFieldNames = implode(',', array_keys($imageFields)); 
			/** @var InputfieldButton $btn */
			$btn = $modules->get('InputfieldButton');
			$uploadOnlyMode = "$this->page" === "$this->editorPage" ? 1 : 2;
			$btn->href = "../edit/?modal=1&id={$this->page->id}&fields=$imageFieldNames&uploadOnlyMode=$uploadOnlyMode";
			$btn->value = $this->_('Upload Image');
			$btn->addClass('upload pw-modal-button pw-modal-button-visible'); 
			$btn->icon = 'upload';
			$changes = $input->get('changes'); 
			if($changes) {
				foreach(explode(',', $changes) as $name) {
					$name = $sanitizer->fieldName($name);
					$field = $this->wire()->fields->get($name);
					if(!$field) continue;
					$out .= "<script>refreshPageEditField('$name');</script>";
				}
			}
		} else $btn = null;

		if($this->input->get('file')) return $this->executeEdit();
	
		// initialization of variables was here

		$out = '';

		if(wireCount($images)) {
			$winwidth = (int) $input->get('winwidth');
			$in = $modules->get('InputfieldImage'); /** @var InputfieldImage $in */
			$in->set('adminThumbs', true);
			$lastFieldLabel = '';
			$numImageFields = 0;

			foreach($images as $image) {
				/** @var PageImage $image */
				$fieldLabels = array();
				$parentFields = $image->get('_parentFields');
				if(!is_array($parentFields)) $parentFields = array();
				foreach($parentFields as $parentField) {
					$fieldLabels[] = $parentField->getLabel();
				}
				$fieldLabels[] = $image->field->getLabel();
				$fieldLabel = implode(' > ', $fieldLabels);
				if($fieldLabel != $lastFieldLabel) {
					$numImageFields++;
					$out .= "\n\t<li class='select_images_field_label detail'>" . $sanitizer->entities($fieldLabel) . "</li>";
				}
				$lastFieldLabel = $fieldLabel;
				if($this->noThumbs) {
					$width = $image->width();
					$alt = $sanitizer->entities1($image->description); 
					if($width > $this->maxImageWidth) $width = $this->maxImageWidth;
					$img = "<img src='$image->URL' width='$width' alt=\"$alt\" />";
				} else {
					$image->set('_requireHeight', true); // recognized by InputfieldImage
					$info = $in->getAdminThumb($image); 
					$img = $info['markup'];
				}
				$out .= 
					"\n\t<li><a href='./edit?file={$image->page->id},{$image->basename}" . 
					"&amp;modal=1&amp;id={$this->page->id}&amp;winwidth=$winwidth'>$img</a></li>";
			}

			$class = $this->noThumbs ? "" : "thumbs";
			if($numImageFields > 1) $class = trim("$class multifield"); 
			$out = "\n<ul id='select_images' class='$class'>$out\n</ul>";

		}

		/** @var InputfieldForm $form */
		$form = $modules->get("InputfieldForm");
		$form->action = "./"; 
		$form->method = "get";

		/** @var InputfieldPageListSelect $field */
		$field = $modules->get("InputfieldPageListSelect"); 
		$field->label = $this->_("Images on Page:") . ' ' . $this->page->get("title") . " (" . $this->page->path . ")"; // Headline for page selection, precedes current page title/url
		$field->description = $this->_("If you would like to select images from another page, select the page below."); // Instruction on how to select another page
		$field->attr('id+name', 'page_id'); 
		$field->value = $this->page->id;
		$field->parent_id = 0; 
		$field->collapsed = wireCount($images) ? Inputfield::collapsedYes : Inputfield::collapsedNo;
		$field->required = true; 
		$form->append($field);

		// getImageFields was here

		$out = $form->render() . $out;
		if($btn) $out .= $btn->render();
		
		return "<div id='ProcessPageEditImageSelect'>" . $out . "\n</div>";
	}

You can replace `@$this->data['defaultPage']` or add it to the config:

        /** @var InputfieldPageListSelect $f */
        $f = $modules->get('InputfieldPageListSelect');
		$f->attr('name', 'defaultPage');
		$f->attr('value', @$data['defaultPage']);
		$f->label = $this->_('Default Page if no image fields');
		$f->value = @$data['defaultPage'];
		$inputfields->add($f);

Copy the entire module directory into your site modules directory. 

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.
×
×
  • Create New...