Skip to content

Commit

Permalink
Store validated fields data in plugin for use during save. Removes th…
Browse files Browse the repository at this point in the history
…e need for onContentBeforeSave method
  • Loading branch information
Thomas Hunziker committed Jan 18, 2017
1 parent f894085 commit 702b816
Showing 1 changed file with 24 additions and 78 deletions.
102 changes: 24 additions & 78 deletions plugins/system/fields/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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);

Expand All @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 702b816

Please sign in to comment.