Jump to content

How can I display an image in the backend?


titanium
 Share

Recommended Posts

I have a template with four or five fields - so far, so normal. One of these fields is used to generate some image with gd outside of PW. The generated image itself is saved in its own field with the help of the API.

I would like to display this image right in the backend, next to the other fields. How can I achieve this? The image field is obviously not suitable, because the image shouldn't be editable. I guess I have to write a module for this and hook somewhere... how to start?

Link to comment
Share on other sites

Little bit of background information would be useful here: you're saying that you've got a field you're using to create an image. What type of field is it, what kind of content does it have, does it store that too or is it cleared after image has been created? Is created image always at same location and does it's name vary?

Depending on what you're doing exactly you might want to a) create new inputfield and/or fieldtype for this purpose or b) create an autoload module that hooks into ProcessPageEdit::execute (or something more appropriate) and if an image is found from pre-specified location renders it somewhere (positioning could be done with JS, if that's necessary.)

Like I said it all depends on what you're trying to achieve and what's the whole context here :)

Link to comment
Share on other sites

Little bit of background information would be useful here: you're saying that you've got a field you're using to create an image. What type of field is it, what kind of content does it have, does it store that too or is it cleared after image has been created? Is created image always at same location and does it's name vary?

Depending on what you're doing exactly you might want to a) create new inputfield and/or fieldtype for this purpose or b) create an autoload module that hooks into ProcessPageEdit::execute (or something more appropriate) and if an image is found from pre-specified location renders it somewhere (positioning could be done with JS, if that's necessary.)

I really appreciate your help, teppo and Nico. I should have been more specific. I've already done some custom backend forms in the past, they where done as a process module like the following example - this renders a table in the backend, if bound to an admin page as a process. I think something like this was meant by Nico:

class ProcessTable extends Process implements Module {

	public static function getModuleInfo() {
		return array(
			'title' => __('Table showcase', __FILE__),
			'version' => 100,
			'summary' => __('Example of a table in backend', __FILE__),
			'author' => 'Yes, it was me! :-)',
			'href' => '',
			'autoload' => false,
			'singular' => true,
			'permanent' => false
		);
	}

	public function init() {
		parent::init();
		ini_set('auto_detect_line_endings', true);
	}

	public function ___execute()
	{
		$this->setFuel('processHeadline', 'Example of a table');
		$table = $this->modules->get('MarkupAdminDataTable');
		$table->headerRow(array('Header col1', 'Header col2'));
		$table->row(array('col1', 'col2'));
		return $table->render();
	}
}

it would be easy to render a picture in such a custom form.

In contrast to this, I would like to show a picture in one of the forms as they get rendered by Processwire by default when a new page is created and a template gets selected. I hope you have an idea of what I'm trying to say - I'm in lack of the correct terminology for these forms  :)

The picture should be rendered below the fields the template already has. The dynamically generated picture is saved to an image field, which I've set to hidden because it's filled by an extra module on its own - this part is already working. The picture can be addressed normally like every other picture, e. g. echo $page-myPic->size(120, 0) etc.

teppo said: "b) create an autoload module that hooks into ProcessPageEdit::execute (or something more appropriate)". That sounds like the route I'm trying to find. Do you know how I can access the form Processwire renders? Maybe I can somehow append my custom output  to it.

Link to comment
Share on other sites

Note: you've mentioned that this should be done when page is added. I'm a bit confused about that, since your field shouldn't have any value at that point yet. If I'm confusing something here and that is actually the case, you'll probably want to take a look at ProcessPageAdd::execute -- pretty much everything else after this should still apply..

Execute method of ProcessPageEdit.module returns rendered edit form. By hooking after it you'll get access to $event->return, which contains said form markup. This way should be able to modify it before it gets sent to browser.

I'm assuming that you're familiar with how hooks are added. If not, wiki has an (unfinished) article about those and HelloWorld.module provides very good examples.

Hope this helps.. and that I understood properly what you're doing there :)

Link to comment
Share on other sites

public function init()
{
	$this->addHookAfter('PageEdit::execute', $this, 'example');
}

public function example($event)
{
	var_dump($event->return); # does not get even executed in backend
}

Hi teppo, hmm... I suppose the method name hooked to is wrong, isn't it? Oh boy, I have the feeling I don't know anything  :mellow:

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