Skip to content

Commit

Permalink
[com_fields] Normalise the request com_fields data (#19884)
Browse files Browse the repository at this point in the history
* Normalise the request com_fields data

* CS

* PHP 5.3 compat

* Fields in com_fields array (#9)

Fields should be set in com_fields array and not direcly in $data

* Spelling

* Also normalise request data on front-end user profile save (#10)

* Also normalise request data on front-end user profile save

* correct context and option

* Handle 0 properly in empty check

* Simplify

* allowing value 0 to be saved (#11)

when setting a value of 0 in a text field the function empty will return true > setting the value to null

* correct needsUpdate when strlen (or count) = 1 which incorrectly equa… (#12)

* correct needsUpdate when strlen (or count) = 1 which incorrectly equaled to 'true'

* Update field.php

* Update field.php
  • Loading branch information
laoneo authored and Ruud68 committed Mar 26, 2018
1 parent 23aa088 commit 1dc231b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
5 changes: 3 additions & 2 deletions administrator/components/com_fields/models/field.php
Expand Up @@ -589,8 +589,9 @@ public function setFieldValue($fieldId, $itemId, $value)
}
elseif (count($value) == 1 && count((array) $oldValue) == 1)
{
// Only a single row value update can be done
$needsUpdate = true;
// Only a single row value update can be done when not empty
$needsUpdate = is_array($value[0]) ? count($value[0]) : strlen($value[0]);
$needsDelete = !$needsUpdate;
}
else
{
Expand Down
8 changes: 8 additions & 0 deletions components/com_users/controllers/profile.php
Expand Up @@ -113,6 +113,14 @@ public function save()
return false;
}

// Send an object which can be modified through the plugin event
$objData = (object) $requestData;
$app->triggerEvent(
'onContentNormaliseRequestData',
array('com_users.user', $objData, $form)
);
$requestData = (array) $objData;

// Validate the posted data.
$data = $model->validate($form, $requestData);

Expand Down
8 changes: 8 additions & 0 deletions libraries/src/MVC/Controller/FormController.php
Expand Up @@ -694,6 +694,14 @@ public function save($key = null, $urlVar = null)
return false;
}

// Send an object which can be modified through the plugin event
$objData = (object) $data;
$app->triggerEvent(
'onContentNormaliseRequestData',
array($this->option . '.' . $this->context, $objData, $form)
);
$data = (array) $objData;

// Test whether the data is valid.
$validData = $model->validate($form, $data);

Expand Down
56 changes: 50 additions & 6 deletions plugins/system/fields/fields.php
Expand Up @@ -9,6 +9,7 @@

defined('_JEXEC') or die;

use Joomla\CMS\Form\Form;
use Joomla\Registry\Registry;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Multilanguage;
Expand All @@ -30,6 +31,38 @@ class PlgSystemFields extends JPlugin
*/
protected $autoloadLanguage = true;

/**
* Normalizes the request data.
*
* @param string $context The context
* @param object $data The object
* @param Form $form The form
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function onContentNormaliseRequestData($context, $data, Form $form)
{
if (!FieldsHelper::extract($context, $data))
{
return true;
}

// Loop over all fields
foreach ($form->getGroup('com_fields') as $field)
{
// Make sure the data object has an entry
if (isset($data->com_fields[$field->fieldname]))
{
continue;
}

// Set a default value for the field
$data->com_fields[$field->fieldname] = false;
}
}

/**
* The save event.
*
Expand All @@ -45,7 +78,7 @@ class PlgSystemFields extends JPlugin
public function onContentAfterSave($context, $item, $isNew, $data = array())
{
// Check if data is an array and the item has an id
if (!is_array($data) || empty($item->id))
if (!is_array($data) || empty($item->id) || empty($data['com_fields']))
{
return true;
}
Expand Down Expand Up @@ -78,17 +111,28 @@ public function onContentAfterSave($context, $item, $isNew, $data = array())
return true;
}

// Get the fields data
$fieldsData = !empty($data['com_fields']) ? $data['com_fields'] : array();

// Loading the model
$model = JModelLegacy::getInstance('Field', 'FieldsModel', array('ignore_request' => true));

// Loop over the fields
foreach ($fields as $field)
{
// Determine the value if it is available from the data
$value = key_exists($field->name, $fieldsData) ? $fieldsData[$field->name] : null;
// Determine the value if it is (un)available from the data
if (key_exists($field->name, $data['com_fields']))
{
$value = $data['com_fields'][$field->name] === false ? null : $data['com_fields'][$field->name];
}
// Field not available on form, use stored value
else
{
$value = $field->rawvalue;
}

// If no value set (empty) remove value from database
if (is_array($value) ? !count($value) : !strlen($value))
{
$value = null;
}

// JSON encode value for complex fields
if (is_array($value) && (count($value, COUNT_NORMAL) !== count($value, COUNT_RECURSIVE) || !count(array_filter(array_keys($value), 'is_numeric'))))
Expand Down

0 comments on commit 1dc231b

Please sign in to comment.