csy_dot_io
csy_dot_io

Reputation: 1199

Value of pseudo inline-grid-field field - Silverstripe 3 & GridFieldExtensions

I use SilverStripe GridFieldExtesions for Inline Editing. I create the fields for that dynamically, so they don't exist as database fields and are always empty after saving / opening the object.

$i = 1;
$inline_fields = array();

do {
    $field = 'Content' . $i;
    $title = 'Spalte ' . $i;

    $inline_fields[$field] = array(
       'title' => $title,
       'field' => 'TextField'
    );

    $i++;
} while( $i <= $this->Columns );

Is there a way to define a value of an inline field like on other fields TextField::create('Foo', 'Foo', 'Bar') ?

Edit:

So I'll try to explain everything a little bit more clear. I've got the Objects 'Table' and 'Row'.

On 'Table' you can define how many columns a 'Row' should have. According to the number of columns on 'Table', InlineFields for 'Row' a generated like shown above.

But 'Row' hast only one field called 'Content', because I don't know how many columns the user would like to have. Because of that, I store everything put into 'Content1, Content2, Content3, ...' as imploded array in 'Content'.

So far so good, but I need to display back the users content in the 'Content1, Content2, Content3, ...' fields. If those fields wouldn't be inline-fields, I would do this like that TextField::create(Name, Title, Value)

So I need to find a way to display pre filled content into an inline field.

Upvotes: 2

Views: 149

Answers (1)

3dgoo
3dgoo

Reputation: 15794

In the setDisplayFields function you can specify a callback function that will return the field you want to use.

To use this you would modify your $inline_fields array to call this callback:

$inline_fields[$field] = array(
    'title' => $title,
    'callback' => 'CustomPage::customContentField'
);

In your class create the callback function that will return the field to be used:

static function customContentField($record, $col, $grid) {
    $value = '123';
    $field = TextField::create($col, $col, $value);
    return $field;
}

This allows you to specify the $value. However, this will not function how you want. Setting the $value in a TextField sets the default value. If you are loading an existing row the $value is not used.

So what happens with this code is when you create a new row it will use the $value specified, but after saving the page the $value will not be used.

Upvotes: 2

Related Questions