From 702b816ed8bdd790ff76f93fb38fc384b51d1418 Mon Sep 17 00:00:00 2001 From: Thomas Hunziker Date: Wed, 18 Jan 2017 15:53:46 +0100 Subject: [PATCH] Store validated fields data in plugin for use during save. Removes the need for onContentBeforeSave method --- plugins/system/fields/fields.php | 102 ++++++++----------------------- 1 file changed, 24 insertions(+), 78 deletions(-) diff --git a/plugins/system/fields/fields.php b/plugins/system/fields/fields.php index 3c95f8f1c05e6..1919392b88440 100644 --- a/plugins/system/fields/fields.php +++ b/plugins/system/fields/fields.php @@ -29,75 +29,30 @@ class PlgSystemFields extends JPlugin protected $autoloadLanguage = true; /** - * The save event. + * Validated fields data is stored here. * - * @param string $context The context - * @param stdClass $item The item - * @param boolean $isNew Is new + * @var array + * @since __DEPLOY_VERSION__ + */ + private $validFieldData = array(); + + /** + * The onAfterDataValidation event. * - * @return boolean + * @param JForm $form The form + * @param array $data The validated data * - * @since 3.7.0 + * @return void + * + * @since __DEPLOY_VERSION__ */ - public function onContentBeforeSave($context, $item, $isNew) + public function onAfterDataValidation($form, $data) { - - $parts = FieldsHelper::extract($context); - - if (!$parts) + if (!empty($data['params'])) { - return true; + // Store the validated data in the plugin for use after the save was successful + $this->validFieldData = $data['params']; } - - $context = $parts[0] . '.' . $parts[1]; - - // Loading the fields - $fieldsObjects = FieldsHelper::getFields($context, $item); - - if (!$fieldsObjects) - { - return true; - } - - $params = new Registry; - - // Load the item params from the request - $data = JFactory::getApplication()->input->post->get('jform', array(), 'array'); - - if (key_exists('params', $data)) - { - $params->loadArray($data['params']); - } - - // Load the params from the item itself - if (isset($item->params)) - { - $params->loadString($item->params); - } - - $params = $params->toArray(); - - // Create the new internal fields field - $fields = array(); - - foreach ($fieldsObjects as $field) - { - // Set the param on the fields variable - $fields[$field->alias] = key_exists($field->alias, $params) ? $params[$field->alias] : array(); - - // Remove it from the params array - unset($params[$field->alias]); - } - - $item->_fields = $fields; - - // Update the cleaned up params - if (isset($item->params)) - { - $item->params = json_encode($params); - } - - return true; } /** @@ -113,28 +68,20 @@ public function onContentBeforeSave($context, $item, $isNew) */ public function onContentAfterSave($context, $item, $isNew) { - $parts = FieldsHelper::extract($context); - - if (!$parts) + if (!$this->validFieldData) { return true; } - $context = $parts[0] . '.' . $parts[1]; - - // Return if the item has no valid state - $fields = null; - - if (isset($item->_fields)) - { - $fields = $item->_fields; - } + $parts = FieldsHelper::extract($context); - if (!$fields) + if (!$parts) { return true; } + $context = $parts[0] . '.' . $parts[1]; + // Loading the fields $fieldsObjects = FieldsHelper::getFields($context, $item); @@ -149,7 +96,7 @@ public function onContentAfterSave($context, $item, $isNew) foreach ($fieldsObjects as $field) { // Only save the fields with the alias from the data - if (!key_exists($field->alias, $fields)) + if (!key_exists($field->alias, $this->validFieldData)) { continue; } @@ -167,7 +114,7 @@ public function onContentAfterSave($context, $item, $isNew) } // Setting the value for the field and the item - $model->setFieldValue($field->id, $context, $id, $fields[$field->alias]); + $model->setFieldValue($field->id, $context, $id, $this->validFieldData[$field->alias]); } return true; @@ -198,7 +145,6 @@ public function onUserAfterSave($userData, $isNew, $success, $msg) $user->params = (string) $user->getParameters(); // Trigger the events with a real user - $this->onContentBeforeSave('com_users.user', $user, false); $this->onContentAfterSave('com_users.user', $user, false); // Save the user with the modified params