/** * @param $masterCategoryKey * @param $masterEntityKey * @param $masterFieldKey * @param $embeddedCategoryKey * @param $embeddedEntityKey * @param $id * @param bool $creation * @return mixed */ private function save($masterCategoryKey, $masterEntityKey, $masterFieldKey, $embeddedCategoryKey, $embeddedEntityKey, $id, $creation = false) { $data = Input::all(); // Find Entity config (from sharp CMS config file) $entity = SharpCmsConfig::findEntity($embeddedCategoryKey, $embeddedEntityKey); try { // First : validation if ($entity->validator) { $validator = App::make($entity->validator); $validator->validate($data, !$creation ? $id : null); } // Data is valid, we are going back to the master form. Let's restore the master form data... $masterInstanceData = sharp_decode_embedded_entity_data($data['masterInstanceData']); // ... add the embedded form data... $embeddedInstanceData = sharp_encode_embedded_entity_data(Input::except(["_method", "_token", "masterInstanceData", "masterInstanceId", "masterEntityLabel"])); if (strpos($masterFieldKey, ".") !== false) { // Embedded instance in part of a list item list($listKey, $itemId, $fieldKey) = explode(".", $masterFieldKey); $masterInstanceData[$listKey][$itemId][$fieldKey] = $embeddedInstanceData; } else { $masterInstanceData[$masterFieldKey] = $embeddedInstanceData; } Session::flash('masterInstanceData', serialize($masterInstanceData)); // ... and redirect back to the master entity form return $this->redirectToMaster($masterCategoryKey, $masterEntityKey, $data['masterInstanceId']); } catch (ValidationException $e) { return Redirect::back()->withInput()->withErrors($e->getErrors()); } }
/** * @return string * @throws \Dvlpp\Sharp\Exceptions\EntityConfigurationNotFoundException */ private function initValue() { // First time we display the embed field. We have to valuate its __embed_data field, // in order to post the current value of the embedded object $embeddedEntityConfig = SharpCmsConfig::findEntity($this->field->entity_category, $this->field->entity); $std = []; foreach ($embeddedEntityConfig->data["form_fields"] as $fieldKey => $configField) { $std[$fieldKey] = $this->initValueFormField($fieldKey, $configField, $this->fieldValue); } $std["__sharp_duplication"] = $this->instance->__sharp_duplication; // Add Id $std[$embeddedEntityConfig->id_attribute] = $this->fieldValue->{$embeddedEntityConfig->id_attribute}; $initialVal = sharp_encode_embedded_entity_data($std); return $initialVal; }