Jump to content

Process Custom Admin Page Image Problem


Soma
 Share

Recommended Posts

I spotted something strange.

I created a custom admin page and have a Process module to render certain pages in a admin data table.

So far it works, but for the image on those pages I got problems. $p->image->url(); wouldn't work, even though it is a single image inputfield (1) , I have to use $p->image->first()->url to get it working. Is this a bug?

Module code:

<?php
class ProcessBilder extends Process {

protected $selLimit = 'template=bild, sort=-created, limit=50, include=all';
protected $selAll = 'template=bild, sort=-created, include=all';

   public static function getModuleInfo() {
       return array(
           'title' => 'Bilder',
           'summary' => 'Page to manage Galerie Images',
           'version' => 001,
           );
   }

   public function ___execute() {
	$out = '';

	$table = $this->modules->get('MarkupAdminDataTable');
	$table->setSortable(true); 
	$table->setEncodeEntities(false);
	$header = array('Bild','Title','Status','created','usermodified');
	$table->headerRow($header);

	$rows = array();

	$pageArr = $this->pages->get('/bilder/')->find($this->selLimit);

	foreach($pageArr as $p){
		$editUrl = "{$this->config->urls->admin}page/edit/?id={$p->id}";
		$img = $p->image->first()->size(0,100)->url(); // $p->image->url() throws error... 
		$rows[] = "<img src=".$img." />";
		$rows[] = $p->editable() ? "<a href='{$editUrl}'>".$p->get('title|name|id').'</a>' : $p->get('title|name|id');
		$rows[] = $p->status;
		$rows[] = date('Y.m.d H:i:s',$p->created);
		$rows[] = $p->modifiedUser->name;
		$table->row($rows);
		$rows = null;
		}

	$button = $this->modules->get('InputfieldButton');
	$button->value = 'Create New';
	$button->href = "{$this->config->urls->admin}page/add/?parent_id=1001";
	$button->class .= ' align_right';

	$pager = $this->getTotal() > count($pageArr) ? $pageArr->renderPager() : '';

	$out = '<p style="overflow:hidden">' . $button->render() . '</p>' . $pager . $table->render() . $pager . '<p style="overflow:hidden">' . $button->render() . '</p>';
       return $out;
   }

Link to comment
Share on other sites

All image fields are technically multi-image fields. The single-image access (when a field is set to 1 image max) is a type of outputFormatting for convenience on the front-end. All pages on the front-end of your site have outputFormatting turned on by default. When outputFormatting is on, it means the page is in a presentation-mode as opposed to a saveable mode. In presentation (outputFormatting) mode, pages are setup for presentation convenience and safety. The distinction exists so that content can be modified at runtime with custom formatters and such, without worry of that runtime-info ending up saved back into the database. This is why ProcessWire throws an error if you try to save a page with outputFormatting on.

outputFormatting is also what converts an images field with a 1-image max to a single image rather than an array of 1 image. That's because I thought people would find it confusing on the front end to have to use an array when all they are dealing with is 1 image. Whereas, when coding for administration of any image(s) field, it's more convenient to treat them all the same way.

When you are in the admin, pages don't come with outputFormatting on by default. Since it sounds like you are presenting content in the admin kind of like how you would on the front end, you can either turn on outputFormatting for those pages you are presenting content from, or better yet: treat all image fields as multi-image fields. If you do that, just remember that all text-based content is in a raw state. For instance, if you want to output an <img> tag with an alt attribute:

<?php
foreach($page->images as $img) { 
   $alt = htmlentities($img->description, ENT_QUOTES, "UTF-8");
   echo "<img src='{$img->url}' alt='$alt' />
}

When building admin stuff, it's generally actually always better to have outputFormatting off.

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