0

Whenever I render a link type in admin form field, I often ends up loading the model to render the href with $helper->getUrl(...).

Is there a way I can render the link contents, including href and label, with $form->setValues() like other fields?

Example:

I don't want this:

$form->addField('link_to_somewhere', 'link', array(
'label' => 'Click me',
'href'  => 'http://...',
));

I want something like this:

$form->addField('link_to_somewhere', 'link', array(
  'label' => 'Click me',
));

$form->setValues(array(
  'link_to_somewhere' => 'http://...'
));
3

You can natively with the Varien_Data_Form_Element_Link

$fieldset->addField('link', 'link', array(
            'name'      => 'link name',
            'href'      => 'http://google.com',
            'value'     => 'my link to google',
            'label'     => Mage::helper('core')->__('Link label'),
            'title'     => Mage::helper('core')->__('Link Title'),
    ));

You can check all the Element type available by browsing the magentoroot/lib/Varien/Data/Form/Element directory

NOTE : the 'value' is overwrite if you call setValue() on the form. Double check that you don't have a line like this below your field declaration

 $form->setValues($model->getData());
2
  • Unfortunately this is the opposite of what I wanted, edited my answer to make it clear. – Vicary Mar 17 '15 at 9:34
  • Erf. ok ! You can't use the link element from Varien but you can create your own. I'll make a post a bit later. – Pilou Mar 17 '15 at 9:57
3

Ok ! After the comment and the edit, I've understand your need.

The problem you have is that the Varien_Data_Form_Element_Link use the value as the link label and not as the href.

So all you have to do is... create your own form element.

First, create your own class extending Varien_Data_Form_Element_Link

class Namespace_Module_Lib_Varien_Data_Form_Element_Customlink extends Varien_Data_Form_Element_Link
{
    /**
     * Generates element html
     *
     * @return string
     */
    public function getElementHtml()
    {
        $html = $this->getBeforeElementHtml();
        $html .= '<a id="'.$this->getHtmlId().'" '.$this->serialize($this->getHtmlAttributes()).' href="' . $this->getEscapedValue()  . '">'. $this->getLinkLabel() . "</a>\n";
        $html .= $this->getAfterElementHtml();
        return $html;
    }

    /**
     * Prepare array of anchor attributes
     *
     * @return array
     */
    public function getHtmlAttributes()
    {
        return array_diff(parent::getHtmlAttributes(), array('href'));
    }
}

Then, declare your custom type in your form

$fieldset->addType('custom_link','Namespace_Module_Lib_Varien_Data_Form_Element_Customlink');

Finally, use it :)

$fieldset->addField('link', 'custom_link', array(
            'link_label' => 'Click me !',
            'label'      => Mage::helper('core')->__('Link label'),
            'title'      => Mage::helper('core')->__('Link Title'),
    ));

[...]

$form->addValues(array(
  'link' => 'http://...'
));

EDIT I've test it and used addValues() instead of setValues() to avoid removing the values already sets.

Any case, it works !

1
  • This makes a lot sense already. I don't have time to give this a test yet, I'll accept when one of us prove it working. – Vicary Mar 17 '15 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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