From 16efd7402eac9319bd8e5d7a3e34e645650ff1ca Mon Sep 17 00:00:00 2001 From: Andrew Short Date: Tue, 15 Jan 2013 22:41:25 +1100 Subject: [PATCH] ENHANCEMENT: Add grid field save handler. --- docs/en/reference/grid-field.md | 6 ++++++ forms/gridfield/GridField.php | 9 +++++++++ forms/gridfield/GridFieldComponent.php | 15 +++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/docs/en/reference/grid-field.md b/docs/en/reference/grid-field.md index 79d0083d4..6cfa8e87f 100644 --- a/docs/en/reference/grid-field.md +++ b/docs/en/reference/grid-field.md @@ -303,6 +303,12 @@ This object is used for creating actions buttons, for example a delete button. W a FormAction, the gridfield finds a `GridField_ActionProvider` that listens on that action. `GridFieldDeleteAction` have a pretty basic implementation of how to use a Form action. +## GridField_SaveHandler + +This is used to create a handler that is called when a form containing the grid +field is saved into a record. This is useful for performing actions when saving +the record. + ### GridState Gridstate is a class that is used to contain the current state and actions on the gridfield. It's diff --git a/forms/gridfield/GridField.php b/forms/gridfield/GridField.php index 5f2d835df..25fc90dae 100644 --- a/forms/gridfield/GridField.php +++ b/forms/gridfield/GridField.php @@ -727,6 +727,15 @@ class GridField extends FormField { return parent::handleRequest($request, $model); } + + public function saveInto(DataObjectInterface $record) { + foreach($this->getComponents() as $component) { + if($component instanceof GridField_SaveHandler) { + $component->handleSave($this, $record); + } + } + } + } diff --git a/forms/gridfield/GridFieldComponent.php b/forms/gridfield/GridFieldComponent.php index fb584576d..ac8c891c7 100644 --- a/forms/gridfield/GridFieldComponent.php +++ b/forms/gridfield/GridFieldComponent.php @@ -142,3 +142,18 @@ interface GridField_URLHandler extends GridFieldComponent { */ public function getURLHandlers($gridField); } + +/** + * A component which is used to handle when a grid field is saved into a record. + */ +interface GridField_SaveHandler extends GridFieldComponent { + + /** + * Called when a grid field is saved - i.e. the form is submitted. + * + * @param GridField $field + * @param DataObjectInterface $record + */ + public function handleSave(GridField $grid, DataObjectInterface $record); + +}