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

Using tables from another plugin breaks links #326

Open
mehov opened this issue Nov 29, 2023 · 1 comment
Open

Using tables from another plugin breaks links #326

mehov opened this issue Nov 29, 2023 · 1 comment

Comments

@mehov
Copy link

mehov commented Nov 29, 2023

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:

$associationConfiguration[$type][$assocKey]['plugin'] = pluginSplit($association->getClassName())[0];

Using it in link generation has been introduced in #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
Copy link
Author

mehov commented Nov 29, 2023

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');
        }
    }

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

No branches or pull requests

1 participant