Search code examples
magentomagento-1.7adminhtml

Edit header text according to data stored on Mage::registry


I have all the necessary files and structures in order to handle CRUD operations on a table I have added on the database. On the Edit.php file I have the getHeaderText() function that modifies the header that will appear on the edit page when either modifying or adding new set of values to the table.

The problem I'm having is that when the following code is evaluated:

if (Mage::registry('allpages_data')  && Mage::registry('allpages_data')->getId())

It always return NULL so always shows New reference text on the header. Checked and when I'm editing it correctly edits the current set of values and when adding new it adds a new set. Tried several solutions and none worked.

2nd EDIT
Here is my allpages_data var_dump:

object(Dts_Allpages_Model_Referencedpages)[412]
  protected '_eventPrefix' => string 'core_abstract' (length=13)
  protected '_eventObject' => string 'object' (length=6)
  protected '_resourceName' => string 'allpages/referencedpages' (length=24)
  protected '_resource' => null
  protected '_resourceCollectionName' => string 'allpages/referencedpages_collection' (length=35)
  protected '_cacheTag' => boolean false
  protected '_dataSaveAllowed' => boolean true
  protected '_isObjectNew' => null
  protected '_data' => 
    array
      empty
  protected '_hasDataChanges' => boolean false
  protected '_origData' => null
  protected '_idFieldName' => string 'referencedpages_id' (length=18)
  protected '_isDeleted' => boolean false
  protected '_oldFieldsMap' => 
    array
      empty
  protected '_syncFieldsMap' => 
    array
      empty

Here is my Edit.php code file, with the getHeaderText function:

<?php
class Dts_Allpages_Block_Adminhtml_Allpagesbackend_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    public function __construct(){
        parent::__construct();

        $id = $this->getRequest()->getParam('id');
        $this->_objectId = 'id';
        $this->_blockGroup = 'allpages';
        $this->_controller = 'adminhtml_allpagesbackend';
        $this->_mode = 'edit';

        $this->_addButton('save_and_continue', array(
                'label' => Mage::helper('adminhtml')->__('Save And Continue Edit'),
                'onclick' => 'saveAndContinueEdit()',
                'class' => 'save',
        ), -100);

        $this->_updateButton('save', 'label', Mage::helper('allpages')->__('Save reference'));

        $this->_formScripts[] = "
                        function toggleEditor() {
                            if (tinyMCE.getInstanceById('form_content') == null) {
                            tinyMCE.execCommand('mceAddControl', false, 'edit_form');
                                } else {
                                    tinyMCE.execCommand('mceRemoveControl', false, 'edit_form');
                                    }
                                }

                        function saveAndContinueEdit(){
                            editForm.submit($('edit_form').action+'back/edit/');
                        }
                ";
    }

    public function getHeaderText()
    {
        if (Mage::registry('allpages_data')  && Mage::registry('allpages_data')->getId()){
            return Mage::helper('allpages')->__('Edit reference');
        } else {
            return Mage::helper('allpages')->__('New reference');
        }
    }

    protected function _prepareLayout()
    {
        if ($this->_blockGroup && $this->_controller && $this->_mode) {
            $this->setChild('form', $this->getLayout()->createBlock($this->_blockGroup . '/' . $this->_controller . '_' . $this->_mode . '_form'));
        }
        return parent::_prepareLayout();
    }
}

And here is my Form.php file code:

<?php
class Dts_Allpages_Block_Adminhtml_Allpagesbackend_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $id = $this->getRequest()->getParam('id');
        $params = array('id' => $this->getRequest()->getParam('id'));

        if (Mage::getSingleton('adminhtml/session')->getdata())
        {
            $data = Mage::getSingleton('adminhtml/session')->getdata();
            Mage::getSingleton('adminhtml/session')->getdata(null);
        }
        elseif (Mage::registry('allpages_data'))
        {
            $data = Mage::registry('allpages_data')->getdata();
        }
        else
        {
            $data = array();
        }

//      $entireregistryArray = Mage::getRegistry();
//      var_dump($entireregistryArray);

        $form = new Varien_Data_Form(
                array(
                        'id' => 'edit_form',
                        'action' => $this->getUrl('*/*/save', $params),
                        'method' => 'post',
                        'enctype' => 'multipart/form-data',
                    )
                );

        $form->setUseContainer(true);
        $this->setForm($form);

        $fieldset = $form->addFieldset('allagesbackend_form', array(
                                'legend' =>Mage::helper('allpages')->__('References pages Information')
                            )
                    );

        $fieldset->addField('current_url', 'text', array(
                        'label'     => Mage::helper('allpages')->__('Url'),
                        'class'     => 'required-entry',
                        'required'  => true,
                        'name'      => 'current_url',
                        'note'      => Mage::helper('allpages')->__('The url of the reference.'),
                    )
            );

        $fieldset->addField('search_engine', 'text', array(
                        'label'     => Mage::helper('allpages')->__('Engine'),
                        'class'     => 'required-entry',
                        'required'  => true,
                        'name'      => 'search_engine',
                    )
            );

        $fieldset->addField('description', 'text', array(
                        'label'     => Mage::helper('allpages')->__('Description'),
                        'class'     => 'required-entry',
                        'required'  => true,
                        'name'      => 'description',
                    )
            );

        $form->setValues($data);

        return parent::_prepareForm();
    }
}

Just in case it helps, I'm using CommerceBug tool from AlanStorm.

1st EDIT
Here is a piece of my ActionController code, where I call the Mage::register('allpages_date') prior to the call to renderLayout() as suggested by @benmarks

public function editAction()
    {
        $id = $this->getRequest()->getParam('referencedpages_id', null);
        $model = Mage::getModel('allpages/referencedpages');

        if ($id) {
            $model->load($id);
        }

        if ($model->getId() || $id == 0) {
            $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
            if ($data) {
                $model->setData($data)->setId($id);
            }
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('allpages')->__('Referencedpages table does not exist'));
            $this->_redirect('*/*/');
        }

        Mage::register('allpages_data', $model);

        $this->loadLayout();
        $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
        $this->renderLayout();
    }

Solution

  • If you aren't setting that registry key in your action controller prior to renderLayout() then it is not being set in a useful way for block rendering. Consider it like any other variable - there's no magic there.