Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot inject custom formatter #111

Closed
webdevilopers opened this issue May 22, 2014 · 3 comments
Closed

Cannot inject custom formatter #111

webdevilopers opened this issue May 22, 2014 · 3 comments

Comments

@webdevilopers
Copy link
Contributor

Though a column can use translation setting the setTranslationEnabled() method to true I need to translate my value inside a custom formatter.

This is more of Question than an Issue.

Since this is my DI attempt I tried to inject the viewRenderer into my custom formatter inside my module.config.php:

    'service_manager' => array(
        'factories' => [
            'ContractStateFormatter' => function($sm) {
                $viewRenderer = $sm->get('ViewRenderer');
                $contractStateFormatter = new \Application\Datagrid\Column\Formatter\ContractState();
                $contractStateFormatter->setView($viewRenderer);
                return $contractStateFormatter;
            }
        ]
    ),

Successfully setting the formatter on the column:

        $col = new Column\Select('state_name');
    $col->setLabel('State');
        $col->setWidth(5);
        $contractState = $this->getServiceLocator()->get('ContractStateFormatter');
        $col->setFormatter($contractState);
        #$col->setFormatter(new Formatter\ContractState()); // working fine
        $col->setFilterDefaultValue($state);
        $col->setTranslationEnabled(true);
        $grid->addColumn($col);

As you can see before the DI the formatter was working fine.

Here is the formatter:

<?php
namespace Application\Datagrid\Column\Formatter;

use ZfcDatagrid\Column\Formatter\AbstractFormatter;
use ZfcDatagrid\Column\AbstractColumn;

class ContractState extends AbstractFormatter
{
    protected $validRenderers = array(
        'jqGrid',
        'bootstrapTable'
    );

    protected $view;

    public function setView($view)
    {
        #echo $view->translate('Home');        
        #$this->view = 'test';
        $this->view = $view;
    }

    public function getView()
    {
        return $this->view;
    }

    public function getFormattedValue(AbstractColumn $column)
    {
        $row = $this->getRowData();

$this->view->translate($row['state_name']));
        $html = sprintf('<span class="state_name">%s</span><br>', $row['state_name']); // translation getTranslator()
        // some custom formatting
        return $html;
    }
}

The setting works fine, the translation works fine. But though everything worked fine before I now get the following error:
Could not save the datagrid cache. Does the directory "/home/.../Zend/workspaces/DefaultWorkspace10/PQ2/data/ZfcDatagrid" exists and is writeable?

This actually makes no sense, right? Maybe the problem is caused by some kind of overhead or conflict with the view attribute. But I couldn't find any conflict inside the formatter.

Again, this is my first DI attempt. Please tell me if my approach is wrong.
Thanks

@webdevilopers webdevilopers changed the title Injecting translation / view into formatter Cannot inject custom formatter Jun 3, 2014
@webdevilopers
Copy link
Contributor Author

@ThaDafinser This seems to be a general problem with injection.

Here is another one I tried:

'service_manager' => array(
    'factories' => [
        return new \Application\Datagrid\Column\Formatter\ContractState(
            $sm->get('ZfcRbac\Service\AuthorizationService')
        );
    ]
)

I tried constructor injection and method injection.

@webdevilopers
Copy link
Contributor Author

As described injecting the formatter with ViewRenderer or ZfcRbac\Service\AuthorizationService causes the error:
Could not save the datagrid cache. Does the directory "/home/mborchers/Zend/workspaces/DefaultWorkspace10/PQ2/data/ZfcDatagrid" exists and is writeable?

While injecting the MvcTranslator causes no problem:

'service_manager' => array(
    'factories' => [
        return new \Application\Datagrid\Column\Formatter\ContractState(
            $sm->get('MvcTranslator')
        );
    ]
)

A problem with the Cache maybe @ThaDafinser ?

@ThaDafinser
Copy link
Owner

This issue was moved to zfc-datagrid/zfc-datagrid#13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants