Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

get current record while modifying fields with GridFieldDetailForm


Go to End


2 Posts   1163 Views

Avatar
creativeSynergy

Community Member, 3 Posts

29 September 2015 at 10:35pm

Edited: 29/09/2015 10:38pm

Hi there,

first off all, I'm using SilverStripe 3.2 RC1.

I need to modify the shown fields of a gridfield item if you click on it. To accomplish this I'm modifying the 'GridFieldDetailForm' fields like that

$bundleGridForm = $bundleGridConfig->getComponentByType('GridFieldDetailForm');

$detailFormFields = FieldList::create(
  TabSet::create(
    'Root',
    Tab::create(
      'Haupt-Inhalt',
      CheckboxSetField::create(
        'ManyMany[AvailableVariations]',
        'Verfügbare Variationen',
        $HELP-NEEDED-HERE->VariationItems()->map()->toArray()
      )
    )
  )
);

$bundleGridForm->setFields($detailFormFields);

As you can see, I'd like to only display an CheckboxSetField and as source

$HELP-NEEDED-HERE->VariationItems()->map()->toArray()
I'd like to use a realation (VariationItems) of the currently selected / clicked item.

The Problem is I don't know how to get this Relation, because $this is of course referencing to the class, where my gridfield is on and not to the clicked item.

Perhapse the handleItem() function is what I need because it returns the GridFieldDetailForm_ItemRequest class where I can than call the getRecord() function. But all of this presupposes that I get the request form the gridfield to use the handleItem() function and I absolutely can't figure out how to get this record.

For any help I would be very grateful.

Best Regards

Avatar
creativeSynergy

Community Member, 3 Posts

30 September 2015 at 8:30pm

to answer my own question and for anyone who get also stuck here:

    $bundleGridConfig
      ->removeComponentsByType('GridFieldAddNewButton');
    
    $bundleGridForm = $bundleGridConfig->getComponentByType('GridFieldDetailForm');
    // reset all fields, either you'r just adding new fields below
    $bundleGridForm->setFields(FieldList::create());
    $bundleGridForm->setItemEditFormCallback(function($form) {
      $record = $form->getRecord();

      // get the saved values 
      $availableVariations = $this->Items()->getExtraData('AvailableVariations', $record->ID);
    
      $form->Fields()->push(
        CheckboxSetField::create(
          'VariationList',
          'Verfügbare Variationen',
          $record->VariationItems(),
          // use the saved values as preset
          explode(',', $availableVariations['AvailableVariations'])
        )
      );
    
      // workaround for https://github.com/silverstripe/silverstripe-framework/issues/4067
      $form->Fields()->push(
        HiddenField::create('ManyMany[AvailableVariations]', 'Verfügbare Variationen', $availableVariations['AvailableVariations'])
      );
    });

the problem is that there is a bug, which prevents that CheckboxSetFields get written to the database. As a quick workaround, my CheckboxSetField is just a "pseudo-field" to display the checkboxes and the field to save the selected options to the database is a HiddenField.

All you have to do now, is to parse the selected options as a string and insert this string into the hidden field on any change of the CheckboxSetField.