Magento – set link field href with setValues in admin form

adminadminformadminhtmlblocksmagento-1

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://...'
));

Best Answer

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());
Related Topic