1

I have created a module to upload PDFs, its a basic CRUD module.

My question is, how can I add a link to the Edit view so that the user can preview the media that is attached to the row being edited?

2
  • You mean a link to the uploaded PDF file?
    – Marius
    Commented Mar 8, 2013 at 20:33
  • Correct, I would like to be able to preview the PDF from the edit view Commented Mar 8, 2013 at 20:34

1 Answer 1

0

Here is how I do it. I create a custom renderer for my file upload fields. It kind of looks like this (assuming your extension is called Easylife_Batch and your entity Pdf' - I saw your other question, I think your extension is called Batch but I don't know the namespace so I added mine): filename: app/code/local/Easylife/Batch/Block/Adminhtml/Pdf/Helper/File.php

<?php
class Easylife_Batch_Block_Adminhtml_Pdf_Helper_File extends Varien_Data_Form_Element_Abstract{
    public function __construct($data){
        parent::__construct($data);
        $this->setType('file');
    }
    public function getElementHtml(){
        $html = '';
        $this->addClass('input-file');
        $html.= parent::getElementHtml();
        if ($this->getValue()) {
            $url = $this->_getUrl();
            if( !preg_match("/^http\:\/\/|https\:\/\//", $url) ) {
                $url = Mage::helper('batch')->getFileBaseUrl() . $url;
            }
            $html .= '<br /><a href="'.$url.'">'.$this->_getUrl().'</a> ';
        }
        if (!$this->getRequired()){
            $html.= $this->_getDeleteCheckbox();
        }
        return $html;
    }
    protected function _getDeleteCheckbox(){
        $html = '';
        if ($this->getValue()) {
            $label = Mage::helper('batch')->__('Delete File');
            $html .= '<span class="delete-image">';
            $html .= '<input type="checkbox" name="'.parent::getName().'[delete]" value="1" class="checkbox" id="'.$this->getHtmlId().'_delete"'.($this->getDisabled() ? ' disabled="disabled"': '').'/>';
            $html .= '<label for="'.$this->getHtmlId().'_delete"'.($this->getDisabled() ? ' class="disabled"' : '').'> '.$label.'</label>';
            $html .= $this->_getHiddenInput();
            $html .= '</span>';
        }       
        return $html;
    }
    protected function _getHiddenInput(){
        return '<input type="hidden" name="'.parent::getName().'[value]" value="'.$this->getValue().'" />';
    }
    protected function _getUrl(){
        return $this->getValue();
    }
    public function getName(){
        return $this->getData('name');
    }
}

This renderer should show a link below the input with the name of the file and a link to it.

You also need to implement the method Mage::helper('batch')->getFileBaseUrl() called above in the getElementHtml method. This should return the base url of your uploads (folder that contains the files).

Now all you have to do is to tell your form to use your renderer for file inputs. To do this add in the block Edit/Tab/Form.php inside the _prepareForm method (if you followed Magento standards the block and method should be there) line :

$fieldset->addType('file', Mage::getConfig()->getBlockClassName('batch/adminhtml_pdf_helper_file')); 
//or directly $fieldset->addType('file', 'Easylife_Batch_Block_Adminhtml_Pdf_Helper_File'); but the first method is cleaner.

Add it above the first $fieldset->addField(...).

Let me know if this works for you. Don't forget to change the namespace.

Small advice. In the future use this Module Creator for CRUD modules. It has support for file and image fields. Still has a few small bugs but it saves you a lot of time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.