简体   繁体   中英

Magento custom admin module wysiwyg integration

I've created an admin module based off of the tutorial here . I'm attempting to change two of my form inputs to wysiwyg editors based off of information found here . However, whenever I load the edit page I get an error Call to a member function setCanLoadTinyMce() on a non-object . $this->getLayout()->getBlock('head') var_dumps to false.

Namespace/Slides/Block/Adminhtml/Slide/Edit.php looks as follows

class Namespace_Slides_Block_Adminhtml_Slide_Edit
    extends Mage_Adminhtml_Block_Widget_Form_Container
{
    protected function _prepareLayout() 
    {
        parent::_prepareLayout();
        if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
            $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
        }
    }

    protected function _construct() 
    {
        //... Construction stuff
    }
}

Namespace/Slides/Block/Adminhtml/Slide/Edit/Form.php

class Cfw_Slides_Block_Adminhtml_Slide_Edit_Form
    extends Mage_Adminhtml_Block_Widget_Form
{

    protected function _prepareForm() 
    {
       //...Do some things first, like create the fieldset..


        //Add the editable fields
        $this->_addFieldsToFieldset($fieldset, array(
            'foreground_image' => array(
                'label' => $this->__('Foreground Image'),
                'input' => 'image',
                'required' => false,
            ),
            'background_image' => array(
                'label' => $this->__('Background Image'),
                'input' => 'editor',
                'required' => true,
                'config' => Mage::getSingleton('cms/wysiwyg_config')->getConfig(),
                'wysiwyg' => true,
            ),
            'description' => array(
                'label' => $this->__('Text Overlay'),
                'input' => 'editor',
                'required' => false,
                'config' => Mage::getSingleton('cms/wysiwyg_config')->getConfig(),
                'wysiwyg' => true,
            )
        )); 
        return $this;
    }


    protected function _addFieldsToFieldset(
        Varien_Data_Form_ElementFieldset $fieldset, $fields)
    {
        $requestData = new Varien_Object($this->getRequest()->getPost('slideData'));
        foreach ($fields as $name => $_data) {
            if ($requestValue = $requestData->getData($name)) {
                $_data['value'] = $requestValue;
            }

            //Wrap all fields with slideData group
            $_data['name'] = "slideData[$name]";

            //Generally, label and title are always the same
            $_data['title'] = $_data['label'];

            //If no new value exists, use the existing slide data.
            if (!array_key_exists('value', $_data)) {
                $_data['value'] = $this->_getSlide()->getData($name);
            }

            //Finally, call vanilla funcctionality to add field.
            $fieldset->addField($name, $_data['input'], $_data);
        }
        return $this;
    }


}

I'm not sure if you need it, but here's my file structure as well

Namespace
-Slides
--Block
---Adminhtml
----Slide
-----Edit
------Form.php
-----Edit.php
-----Grid.php
----Slide.php
--controllers
---Adminhtml
----SlideConroller.php
--etc
---config.xml
--Helper
---Data.php
--Model
---Resource
----Slide
-----Collection.php
----Slide.php
---Slide.php
--sql
---namespace_slides_setup
----install-0.0.1.php

The issue is that Magento cannot find your head block.

Instead of using:

$this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);

Try calling it like this:

Mage::app()->getLayout()->getBlock('head')->setCanLoadTinyMce(true);

If that doesn't work, it's a different issue but the problem is still that Magento can't find the head block.

I imagine you no longer need a solution for this, but I ran into the same issue using the same tutorial that you did.

The 'head' block (and thus setCanLoadTinyMce()) was unavailable in the Edit.php and the Form.php via the _prepareLayout() function.

The 'head' block was available in the controller(SlideController.php in your case) between $this->loadLayout() and $this->renderLayout within the editAction() function.

In SlideController.php change

$this->loadLayout()
        ->_addContent($brandBlock)
        ->renderLayout();

to

$this->loadLayout() ;               
$this->_addContent($brandBlock);                                        
$this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
$this->renderLayout();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM