4

I want my booleans to give a more friendlier response then true/false. I have seen that I can do Boolan.Nice but that will give me yes/no.

I have used:

 class AboutusGallery extends DataObject{
 private static $db = [
    'Description' => 'Varchar',
    'ShowOnHomePage'=> 'Boolean',
];

private static $owns = [
    'Picture'
];
private static $has_one = [
    'Picture' => Image::class,
    'HomePage' => HomePage::class,
];

private static $summary_fields = [
    'Thumbnail' => 'Immagine',
    'Description' => 'Descrizione',
    /* THIS WORKS*/
    'ShowOnHomePageITA' => 'In primo piano',
];

public function ShowOnHomePageITA(){
   return $this->ShowOnHomePage ? 'Si' : 'No';
}

ShowOnHomePageITA works and does what I am looking for. But when I do the same in my model admin it brakes.

This is what I am doing:

class PageLink extends DataObject {
private static $db = [
    'SortOrder' => 'Int',   
    'Description' => 'Varchar',
    'PageLink' => 'Varchar',
    'ShowOnHeader'=> 'Boolean',
];
private static $has_one = [
    'HomePage' => HomePage::class,
];

private static $summary_fields = [
    'Description',
    /* this does NOT work*/
    'MostraNelHeaderITA' => 'Header',
];

public function MostraNelHeaderITA(){
   return $this->ShowOnHeader ? 'Si' : 'No';
}

I have also tried to do some casting or even change the Boolean into varchar without any luck.

As I try to dev/build or flush the project i get the following:

(!) Fatal error: Call to a member function scaffoldSearchField() on string in D:\Programmazione\WebSites\Pediatra\vendor\silverstripe\framework\src\ORM\DataObject.php on line 2227

So I wonder whether am I doing something wrong or it cannot be done on model admin. Since it works everywhere else except on model admins.

2 Answers2

1

I think your problem is that part of your summary fields array has only a value and the other has a key and a value. Try changing this:

-     'Descrizione',
+     'Description' => 'Descrizione',
scrowler
  • 24,273
  • 9
  • 60
  • 92
0

I think you need to cast the returned value as a DBField object, as opposed to a plain string. So...

public function MostraNelHeaderITA(){
  $stringValue = $this->ShowOnHeader ? 'Si' : 'No';
  return DBField::create_field('Text', $stringValue);
}
noizy
  • 152
  • 2
  • 12