-
Notifications
You must be signed in to change notification settings - Fork 40
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
Best way to customize relation display fields #58
Comments
I think a good way is using the element formatter:
Then the |
Derp, I took the time to read the full issue description, you are already using that. That said, I'm not sold on complicating the formatting options by using an array DSL. I'm open to see what others think about it. |
@deizel Can't your particular case be handled just by changing display field of |
@ADmad Unfortunately, since I need the public function index()
{
$this->Crud->on('relatedModel', function(Event $event) {
if ($event->subject->name === 'Facilities') {
$event->subject->query = $this->Accounts->Facilities->find('list', [
'valueField' => 'name'
]);
}
}); (Side note: Also I failed to mention earlier, when the default display field was Ultimately, this leads me back to 'formatter' being the only direct solution, with 'callback' being less convoluted than 'element', and 'relation' not being configurable enough. |
@lorenzo For reference, this was the approach with a reusable entity (after cleaning it up): public function index()
{
$action = $this->Crud->action();
$action->config([
'scaffold.fields' => [
'facility_id' => [
'formatter' => 'element',
'element' => 'index/relation',
'assocKey' => 'facility',
'displayField' => 'short_name',
'controller' => 'Facilities',
],
<?php
echo $this->Html->link(
$context->$options['assocKey']->$options['displayField'],
['controller' => $options['controller'], 'action' => 'view', $value]
); Whatever we decide, it might be worth documenting the suggested approach to save others time. 😉 |
Well then imo yours is pretty specific use case and using a formatter element or callback is the way to go. You can help documenting it by providing a patch for the docs. |
Just a quick update to say I haven't forgot about this and plan to get around to it at some point. My solution currently looks like this for now, after a few iterations: public function index()
{
$action = $this->Crud->action();
$action->config([
'scaffold.fields' => [
'facility_id' => [
'formatter' => 'element',
'element' => 'index/relation',
'displayField' => 'short_name',
'controller' => 'Facilities',
],
'account_status_id' => [
'formatter' => 'element',
'element' => 'index/relation',
'link' => false,
],
<?php
use Cake\Utility\Hash;
$link = Hash::get($options, 'link', true);
$displayField = Hash::get($options, 'displayField', 'name');
$assocKey = Hash::get($options, 'assocKey', str_replace('_id', '', $field));
$assoc = $context->$assocKey;
if ($assoc):
$name = $assoc->$displayField;
if ($link):
echo $this->Html->link($name, ['controller' => $options['controller'], 'action' => 'view', $value]);
else:
echo $name;
endif;
endif; If I remember correctly, this now handles the following cases:
I personally think the helper should be able to support all of this, but I'm still open to suggestions. |
Recently had a requirement to display a shorter name for a related field in the index table (from the default
name
field to one calledshort_name
).This is the most elegant solution I could come up with:
Other things I tried included:
beforePaginate
and back again inrelatedModel
, which wasn't ideal either.)Is this a common enough use case where it should be configurable like like
title
andlabel
?The text was updated successfully, but these errors were encountered: