diff --git a/README.md b/README.md index 25aab8e..da507a2 100644 --- a/README.md +++ b/README.md @@ -333,7 +333,49 @@ This new method you create would then become available in the progress indicator template. -### 7. Finishing it up +### 7. Loading values from other steps + +There are several use cases were you want to pre-populate a value based +based on the submission value of another step. There are two methods supporting this: + +* `getValueFromOtherStep()` load any submitted value from another step from the session +* `copyValueFromOtherStep()` saves you the repeated work of adding the same lines of code again and again. + +Here is an example of how to populate the email address from step 1 in step2 : + + :::php + copyValueFromOtherStep($fields, 'Step1', 'Email'); + + return $fields; + } + } + + +### 8. Finishing it up Now that we've got a structure set up to collect form data along each step, and progress through successfully, we need to customise what happens at the end of @@ -398,7 +440,7 @@ Here is an example of what we could do here: } -#### 8. Organisation data model +#### 9. Organisation data model The class Organisation is mentioned in the above example but doesn't exist at the moment (unlike the existing Member() class which looks after the member diff --git a/code/model/MultiFormStep.php b/code/model/MultiFormStep.php index 0558ebf..49ebe6d 100644 --- a/code/model/MultiFormStep.php +++ b/code/model/MultiFormStep.php @@ -79,6 +79,13 @@ class MultiFormStep extends DataObject { */ protected $extraClasses = array(); + /** + * Temporary cache to increase the performance for repeated look ups. + * + * @var array $cache + */ + protected $step_data_cache = array(); + /** * Form fields to be rendered with this step. * (Form object is created in {@link MultiForm}. @@ -410,4 +417,49 @@ class MultiFormStep extends DataObject { public function getExtraClasses() { return join(' ', array_keys($this->extraClasses)); } + + /** + * Returns the submitted value, if any, of any steps. + * + * @param string $fromStep (classname) + * @param string $key + * + * @return mixed + */ + public function getValueFromOtherStep($fromStep, $key) { + // load the steps in the cache, if this one doesn't exist + if (!array_key_exists('steps_' . $fromStep, $this->step_data_cache)) { + $steps = MultiFormStep::get()->filter('SessionID', $this->form->session->ID); + + if($steps) { + foreach($steps as $step) { + $this->step_data_cache['steps_' . $step->ClassName] = $step->loadData(); + } + } + } + + // check both as PHP isn't recursive + if(isset($this->step_data_cache['steps_' . $fromStep])) { + if(isset($this->step_data_cache['steps_' . $fromStep][$key])) { + return $this->step_data_cache['steps_' . $fromStep][$key]; + } + } + + return null; + } + + /** + * allows to get a value from another step copied over + * + * @param FieldList $fields + * @param string $formStep + * @param string $fieldName + * @param string $fieldNameTarget (optional) + */ + public function copyValueFromOtherStep(FieldList $fields, $formStep, $fieldName, $fieldNameTarget = null) { + // if a target field isn't defined use the same fieldname + if (!$fieldNameTarget) $fieldNameTarget = $fieldName; + + $fields->fieldByName($fieldNameTarget)->setValue($this->getValueFromOtherStep($formStep, $fieldName)); + } }