FriendsOfCake / crud-view

CakePHP: Automated admin backend based on your CRUD configuration
https://crud-view.readthedocs.io/en/latest/
MIT License
49 stars 41 forks source link

Using tables from another plugin breaks links #326

Open mehov opened 9 months ago

mehov commented 9 months ago
public function initialize(): void
{
    parent::initialize();

    $this->loadComponent('Flash');

    $this->loadComponent('Crud.Crud', [
        'actions' => [
            'Crud.Index',
            'Crud.View',
            'Crud.Add',
            'Crud.Edit',
            'Crud.Delete',
            'Crud.Lookup',
        ],
        'listeners' => [
            // New listeners that need to be added:
            'CrudView.View',
            'Crud.Redirect',
            'Crud.RelatedModels',
        ]
    ]);
    $this->Crud->useModel('MyOtherPluginWithTables.'.$this->Crud->model()->getAlias());
}

Results in:

Missing Route Cake\Routing\Exception\MissingRouteException Error A route matching array ( 'plugin' => 'MyOtherPluginWithTables', 'controller' => 'Customers', 'action' => 'view', 0 => 1, 'prefix' => 'Backend', '_ext' => NULL, ) could not be found.

The plugin alias is being automatically picked up from the table name in dot notation here: https://github.com/FriendsOfCake/crud-view/blob/28f1bcdc43d52135f816499d5222cdea08dc9359/src/Listener/ViewListener.php#L578

Using it in link generation has been introduced in FriendsOfCake/crud-view#138

I looked at the code and there doesn't seem to be an obvious way to tell the system that while I use the table from another plugin, the links shouldn't refer to it.

I'm hoping there's a way to somehow pass some flag through in an event callback of some kind?

mehov commented 9 months ago

Dirty workaround

    /**
     * Before render callback.
     *
     * @param \Cake\Event\Event $event The beforeRender event.
     * @return void
     */
    public function beforeRender(\Cake\Event\EventInterface $event)
    {
        // prevent linking to the table plugin: start
        $viewBuilderVars = $this->viewBuilder()->getVars();
        if (isset($viewBuilderVars['associations'])) {
            foreach ($viewBuilderVars['associations'] as &$association) {
                foreach ($association as &$associationTable) {
                    $associationTable['plugin'] = null;
                }
            }
        }
        $this->set('associations', $viewBuilderVars['associations']);
        // prevent linking to the table plugin: end
        if ($this->viewBuilder()->getClassName() === null) {
            $this->viewBuilder()->setClassName('CrudView\View\CrudView');
        }
    }