ZfTable icon indicating copy to clipboard operation
ZfTable copied to clipboard

Zend Url Helper in callable

Open hattingen opened this issue 11 years ago • 2 comments

I am using the callable decorator like this:

    $this->getHeader('id')->getCell()->addDecorator('callable', array(
        'callable' => function($context, $record){
            return '<a href="/admin/edit/something/'.$record->getUuid().'">'.$record->getId().'</a>';
        }
    ));

Is it possible to use the view helper and routes here?

hattingen avatar Apr 24 '15 11:04 hattingen

inject from service manager is necessary here. as rough example.

class Link extends AbstractTable
{

    /**
     * @var ServiceLocatorInterface
     */
    protected $viewHelperManager;

    protected $config = array(
        'name' => 'Link decorator',
        'showPagination' => true,
        'showQuickSearch' => false,
        'showItemPerPage' => true,
    );

    /**
     * @var array Definition of headers
     */
    protected $headers = array(
        'id' => array('title' => 'Id', 'width' => '50') ,
        //...
    );

    /**
     * @param ServiceLocatorInterface $viewHelperManager
     */
    public function setViewHelperManager(ServiceLocatorInterface $viewHelperManager)
    {
        $this->viewHelperManager = $viewHelperManager;
    }

    /**
     * @return ServiceLocatorInterface
     */
    public function getViewHelperManager()
    {
        return $this->viewHelperManager;
    }

    /**
     * @param $name
     * @param $args
     *
     * @return mixed
     */
    public function __call($name, $args)
    {
        return call_user_func_array($this->getViewHelperManager()->get($name), $args);
    }
    public function init()
    {
        $this->getHeader('id')->getCell()->addDecorator('callable', array(
            'callable' => function($context, $record){
                return '<a href="' . $this->url('your route name', [$record->getUuid()]) ' . '">'.$record->getId().'</a>';
            }
        ));
    }
}

$tblGrid = new Link(/*...*/);
$vhm = $di->get('viewHelperManager');
$tblGrid->setViewHelperManager($vhm);

olekhy avatar Apr 24 '15 12:04 olekhy

Thx. Have it working now. My tries were similar, but I missed the last part.

hattingen avatar Apr 24 '15 12:04 hattingen