crud-view icon indicating copy to clipboard operation
crud-view copied to clipboard

`\CrudView\Listener\ViewListener::_associations` receives `$whitelist` as `[0, 1, 2, 3, 4]`

Open mehov opened this issue 1 year ago • 1 comments

Hi,

I'm trying to have CrudView use displayField values when linking to associated entries. Right now it links to them via their IDs.

It isn't really documented, I guess because it should just work by default. For me it doesn't, and the only resource I found is https://github.com/FriendsOfCake/crud-view/issues/280, which at least gave me an idea of where to poke around.

https://github.com/FriendsOfCake/crud-view/blob/212573ae0bd318c8bf7ee1126e93f68bb6ae7f72/src/Listener/ViewListener.php#L558

Doing debug([$keys, $whitelist]); right after that says I have an array of normal model names as $keys (Pages, Posts) and an array of integers as $whitelist (0, 1). The latter has exactly as many elements as the former and starts from 0, which makes it seem someone just used array keys instead of values.

mehov avatar Jul 15 '24 14:07 mehov

Yep, it's here in beforePaginate() callback:

https://github.com/FriendsOfCake/crud-view/blob/212573ae0bd318c8bf7ee1126e93f68bb6ae7f72/src/Listener/ViewListener.php#L67

Changing that to $this->associations = $this->_associations($related); hotfixes the issue for me, meaning associated entries are now linked to using their displayFields.

As I was looking through the file, I've seen other calls to $this->_associations (e.g. in beforeRender()) with whatever value passed there, first handled with array_keys. Not sure what's the logic there, so won't suggest any solution. Will keep the hotfix for now.

mehov avatar Jul 15 '24 14:07 mehov