Tuesday, May 21, 2013

Prestashop adding image field

To add in admin as a list field:
override/controllers/admin/Admin[Name]Controller.php
in __construct():

$this->fieldImageSettings = array(
  'name' => 'image', //form name
  'dir' => 'name'//img/name
);
$this->imageType = "jpg";


in __construct() or ajaxProcessDetails(): //depending on extended controller

$this->fields_list['image'] = array(
  'title' => $this->l('Image'),
  'width' => 70,
  "image" => $this->fieldImageSettings["dir"]
);

if helper is generated manually:
//$this->setHelperDisplay($helper); // this is full helper
$helper->imageType = $this->imageType;

in __construct() or renderForm or renderForm[Name] //depending on extended controller
$this->fields_form["input"][] = array(
  'type' => 'file',
  'label' => $this->l('Image:'),
  'name' => 'image',
  'required' => false,
  'display_image' => true
);

In override/classes/modelname.php
in __construct():
$this->image_dir = "name"; //points to same path as fieldImageSettings["dir"] above

if you generate custom thumb based on alias name, makesure to clear all cache files in tmp with this additional function in Model Class:

public function deleteImage($force_delete = false) {
    $res = parent::deleteImage($force_delete);
 
    if ($res) {
      //clear cached file
      $files = array(
        $table.'_mini_'.$this->id.'.'.$type,
        $table.'_thumb_'.$this->id.'.'.$type
    );
    foreach($files as $file) {
      if (file_exists(_PS_TMP_IMG_DIR_.$file)) {
        unlink(_PS_TMP_IMG_DIR_.$file);
      }
    }
   
    }
  return $res;
}




No comments: