Crud: model_function returns shortened string

Created on 23 Mar 2018  路  10Comments  路  Source: Laravel-Backpack/CRUD

Bug report

Since 3.5 the "model_function" (at setcolumnsdetails) returns the string shortened. 3.4 works fine

What I did:

I use the funtion similar to the official example (https://laravel-backpack.readme.io/v3.4/docs/crud-columns-types)

         $this->crud->setColumnDetails('timemodel_id', [ 'label' => "time",
                                                          'type' => "model_function",
                                                          'name' => 'timemodel_id',
                                                          'function_name' => 'getSlugWithLinkTimemodel',
                                                        ]);
    public function getSlugWithLinkTimemodel() {
                    $id = $this->timemodel->id;
                    $name = $this->timemodel->name;
                    return '<a href="'.url('/admin/timemodel/'.$id.'/timemodeldetail').'">'.$name.'</a>';
                }

What I expected to happen:

i recieve the string
<a href="http://192.168.0.203/admin/timemodel/1/timemodeldetail">Name</a>

What happened:

i recieve the string
<a href="http://192.168.0.203/admin/workergroup/1/[...]
(shortened to 50 Chars + [...])

What I've already tried to fix it:

Backpack, Laravel, PHP, DB version:

Backpack 3.5, Laravel 5.5

triage

Most helpful comment

No Problem! I love backpack and you make a great job!

All 10 comments

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps _a lot_ in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication mediums:

  • Bug Reports, Feature Requests - Github Issues (here);
  • Quick help (_How do I do X_) - Gitter Chatroom;
  • Long questions (_I have done X and Y and it won't do Z wtf_) - Stackoverflow, using the backpack-for-laravel tag;

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome _awesome_ community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

--
Justin Case
The Backpack Robot

okay, i found that i have to add

'limit'=> 100,

and it seems that datatables ignores html tags.

Hi @schnettker ,

Something like'limit'=> 1000, should do it for you. DataTable doesn't ignore HTML tags - that should still work. But it's likely that every character in the HTML counts to the limit, so I recommend you place a high limit for model_function columns. 1000+

Cheers!

PS. Closed this as I'm pretty sure that's what it was. Let me know if I'm wrong and we'll reopen this.

Hi,

i raised the limit to 1000 (like your example) and Datatables still ignore HTML

like:
image

(The Screenshot is from my interface and should be only a link)

After spending many hours i found the problem,

Look at the old model_function.blade.php

{{-- custom return value --}}
<td>
    <?php
        echo $entry->{$column['function_name']}();
    ?>
</td>

the "echo" command won't be escaped

the new blade

 {{-- custom return value --}}
 @php
    $value = $entry->{$column['function_name']}();
 @endphp

 <span>
    {{ (array_key_exists('prefix', $column) ? $column['prefix'] : '').str_limit($value, array_key_exists('limit', $column) ? $column['limit'] : 50, "[...]").(array_key_exists('suffix', $column) ? $column['suffix'] : '') }}
 </span>

Value is now escaped by blade

the solution for me is

{!! (array_key_exists('prefix', $column) ? $column['prefix'] : '').str_limit($value, array_key_exists('limit', $column) ? $column['limit'] : 50, "[...]").(array_key_exists('suffix', $column) ? $column['suffix'] : '') !!}

Look here

https://github.com/Laravel-Backpack/CRUD/pull/1308

You're totally right - I don't know when this change happened, probably when adding the suffix and prefix. I'm sorry you've spent a lot of time on this @schnettker - thanks a lot for providing a solution, you're definitely helping a lot of other devs. Kudos to you!

No Problem! I love backpack and you make a great job!

I think this should be added to the doc.

'limit'=> 1000

This is very important if you want to display a html link returned by a model_function.

IE:
```php
$this->crud->addColumn([
'name' => 'filepath_link', //replace with your field
'label' => 'Attachment',
'type' => "model_function",
'function_name' => 'getDownloadableLink', // the method in your Model
'limit'=> 1000, // very important
]);

Added! Thanks @jrbecart !

Was this page helpful?
0 / 5 - 0 ratings