Magento, grid, add a column with a link to a website

I hit my head to add a simple and simple website link on a custom grid column. I used Inchoo's blog to add a custom renderer for the column, and it works. I, however, simply change the visualization and add a tag. But my hopes were broken, not working.

How can I do that? It should be simple, but I just can’t find how to do it. I found a lot of questions / answers here in SO, but about adding links to products, categories, etc., Links to external websites, maybe I'm just using the wrong keywords in the search.

Here is _prepareColumns () from my Grid.php

protected function _prepareColumns() { $blog = Mage::getModel('blogtest/blog'); $this->addColumn('api_blog_url', array( 'header' => $this->__('URL'), 'align' => 'center', 'index' => 'api_blog_url', 'width' => 50, 'type' => 'text', 'renderer' => 'Dts_Blogtest_Block_Adminhtml_Blog_Renderer_MyRender' )); .... 

And here is my override override for this column:

 <?php class Dts_Blogtest_Block_Adminhtml_Blog_Renderer_MyRender extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract { public function render(Varien_Object $row) { $value = $row->getData($this->getColumn()->getIndex()); // return '<a href="http://'.$value.'>'.$value.'</a>'; return '<span style="color:red;">'.$value.'</span>'; } } ?> 
+4
source share
2 answers

You mean the red color style that works correctly, but if you uncomment the line with <a href..> , is that not so?

I think you just missed the quote in the href attribute.

 return '<a href="http://'.$value.'">'.$value.'</a>'; 
+4
source

By the way, I think that you can write more beautiful code if you use the following approach: in the render function, you create a block and send the link data. After that, you will create a block and a template for this block. In the template, you get the channel data and show it at your discretion.

 <?php class Something extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract { public function render(Varien_Object $row) { $column_data = $row->getData( $this->getColumn()->getIndex() ); return $this->getLayout() ->createBlock('something/adminhtml_renderer_link') ->setLink($column_data ) ->_toHtml(); } } ?> <?php class Something extends Mage_Core_Block_Template { protected function _construct() { parent::_construct(); $this->setTemplate('something/link.phtml'); } } ?> <?php $link = $this->getLink(); ?> <?php if ($link): ?> <a href="<?php echo $link ?>" target="_blank"><?php echo $this->__('Click to view!'); ?></a> <?php else: ?> <?php echo $this->__('No link'); ?> <?php endif; ?> 
+3
source

Source: https://habr.com/ru/post/1440257/


All Articles