2

I must rewrite the Sitemap form in the adminhtml. I must add a new field but I can't display that. I've checked on Google and the results haven't help me.

<?xml version="1.0"?>
<config>
    <modules>
        <Dtlmmage_Sitemap>
            <version>0.7.2</version>
        </Dtlmmage_Sitemap>
    </modules>
    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <sitemap_edit_form>Dtlmmage_Adminhtml_Block_Sitemap_Edit_Form</sitemap_edit_form>
                </rewrite>
            </adminhtml>
        </blocks>
        <models>
            <sitemap>
                <rewrite>
                    <sitemap>Dtlmmage_Sitemap_Model_Sitemap</sitemap>
                </rewrite>
                <resourceModel>sitemap_resource</resourceModel>
            </sitemap>
            <sitemap_resource>
                <rewrite>
                    <catalog_product>Dtlmmage_Sitemap_Model_Resource_Catalog_Product</catalog_product>
                </rewrite>
            </sitemap_resource>
        </models>
    </global>
</config>

<?php
class Dtlmmage_Adminhtml_Block_Sitemap_Edit_Form extends Mage_Adminhtml_Block_Sitemap_Edit_Form
{
    protected function _prepareForm()
    {
        $model = Mage::registry('sitemap_sitemap');
        $form = new Varien_Data_Form();

        $fieldset = $form->addFieldset('add_sitemap_form', array('legend' => Mage::helper('sitemap')->__('Sitemap')));

        $fieldset->addField('sitemap_type', 'select', array(
            'label'    => Mage::helper('sitemap')->__('Type'),
            'name'     => 'sitemap_type',
            'required' => false,
            'value'    => $model->getSitemapCategory(),
            'values'   => array(
                'categories' => 'Catégories, sous-catégories, pages filtrées, pages CMS et guides',
                'products'   => 'Produits',
                'images'     => 'Images',
            ),
        ));
        $this->setForm($form);

        return parent::_prepareForm();


       }
    }

Can you help me to solve the problem?

Thanks

Edit : I've check again on Google and I've found this solution

4 Answers 4

2

you should not have to make a rewrite for just adding a field.

You can use an observer event that is called after the _prepareForm Method is executed, then get the form of the block and add a new fieldset with your own fields.

basically like described here: https://www.jyotiranjan.in/blog/add-new-field-magento-admin-form-event-observer/

2
class Dtlmmage_Adminhtml_Block_Sitemap_Edit_Form extends Mage_Adminhtml_Block_Sitemap_Edit_Form
{
    protected function _prepareForm()
    {
        parent::_prepareForm();
        $model = Mage::registry('sitemap_sitemap');
        $form = $this->getForm();

        $fieldset = $form->getElement('add_sitemap_form');

        $fieldset->addField('sitemap_type', 'select', array(
            'label'    => Mage::helper('sitemap')->__('Type'),
            'name'     => 'sitemap_type',
            'required' => false,
            'value'    => $model->getSitemapType(),
            'values'   => array(
                'categories' => 'Catégories, sous-catégories, pages filtrées, pages CMS et guides',
                'products'   => 'Produits',
                'images'     => 'Images',
            ),
        ));

        $this->setForm($form);

        return $this;
    }

}

It's my solution.


@Abdul, my manager has not approved this solution because I rewrite unnecessarily the form

1
  • why should return $this and not parent::_prepareForm();?
    – madi
    Oct 9, 2016 at 4:05
1

Try this code

<?php

class Dtlmmage_Adminhtml_Block_Sitemap_Edit_Form extends Mage_Adminhtml_Block_Sitemap_Edit_Form
{

    protected function _prepareForm()
    {
        $model = Mage::registry('sitemap_sitemap');

        $form = new Varien_Data_Form(array(
            'id'        => 'edit_form',
            'action'    => $this->getData('action'),
            'method'    => 'post'
        ));

        $fieldset = $form->addFieldset('add_sitemap_form', array('legend' => Mage::helper('sitemap')->__('Sitemap')));

        if ($model->getId()) {
            $fieldset->addField('sitemap_id', 'hidden', array(
                'name' => 'sitemap_id',
            ));
        }

        $fieldset->addField('sitemap_filename', 'text', array(
            'label' => Mage::helper('sitemap')->__('Filename'),
            'name'  => 'sitemap_filename',
            'required' => true,
            'note'  => Mage::helper('adminhtml')->__('example: sitemap.xml'),
            'value' => $model->getSitemapFilename()
        ));

        $fieldset->addField('sitemap_type', 'select', array(
            'label'    => Mage::helper('sitemap')->__('Type'),
            'name'     => 'sitemap_type',
            'required' => false,
            'value'    => $model->getSitemapCategory(),
            'values'   => array(
                'categories' => 'Catégories, sous-catégories, pages filtrées, pages CMS et guides',
                'products'   => 'Produits',
                'images'     => 'Images',
            ),
        ));

        $fieldset->addField('sitemap_path', 'text', array(
            'label' => Mage::helper('sitemap')->__('Path'),
            'name'  => 'sitemap_path',
            'required' => true,
            'note'  => Mage::helper('adminhtml')->__('example: "sitemap/" or "/" for base path (path must be writeable)'),
            'value' => $model->getSitemapPath()
        ));

        if (!Mage::app()->isSingleStoreMode()) {
            $field = $fieldset->addField('store_id', 'select', array(
                'label'    => Mage::helper('sitemap')->__('Store View'),
                'title'    => Mage::helper('sitemap')->__('Store View'),
                'name'     => 'store_id',
                'required' => true,
                'value'    => $model->getStoreId(),
                'values'   => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(),
            ));
            $renderer = $this->getLayout()->createBlock('adminhtml/store_switcher_form_renderer_fieldset_element');
            $field->setRenderer($renderer);
        }
        else {
            $fieldset->addField('store_id', 'hidden', array(
                'name'     => 'store_id',
                'value'    => Mage::app()->getStore(true)->getId()
            ));
            $model->setStoreId(Mage::app()->getStore(true)->getId());
        }

        $fieldset->addField('generate', 'hidden', array(
            'name'     => 'generate',
            'value'    => ''
        ));

        $form->setValues($model->getData());

        $form->setUseContainer(true);

        $this->setForm($form);

        return $this;
    }

}
1

After hours of searching and debugging this function to override, I found out that:

return parent::_prepareForm();

returns the extended class function instead of the function that you created to override the "parent" function.

This basically means that every code that is added or removed is ignored and simply the original function is returned.

To fix this you should use the following code instead.

return $this;

Your Answer

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

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