diff --git a/libraries/joomla/form/fields/checkbox.php b/libraries/joomla/form/fields/checkbox.php index a7f3f04240f01..c2826f15890b5 100644 --- a/libraries/joomla/form/fields/checkbox.php +++ b/libraries/joomla/form/fields/checkbox.php @@ -36,6 +36,14 @@ class JFormFieldCheckbox extends JFormField */ protected $checked = false; + /** + * Checkbox field should always have any value. + * + * @var boolean + * @since __DEPLOY_VERSION__ + */ + public $ensureValue = true; + /** * Method to get certain otherwise inaccessible properties from the form field object. * diff --git a/libraries/joomla/form/fields/checkboxes.php b/libraries/joomla/form/fields/checkboxes.php index 5b3c782bc791e..b5abe655670c2 100644 --- a/libraries/joomla/form/fields/checkboxes.php +++ b/libraries/joomla/form/fields/checkboxes.php @@ -46,13 +46,21 @@ class JFormFieldCheckboxes extends JFormFieldList protected $forceMultiple = true; /** - * The comma seprated list of checked checkboxes value. + * The comma separated list of checked checkboxes value. * * @var mixed * @since 3.2 */ public $checkedOptions; + /** + * Checkboxes field should always have any value. + * + * @var boolean + * @since __DEPLOY_VERSION__ + */ + public $ensureValue = true; + /** * Method to get certain otherwise inaccessible properties from the form field object. * diff --git a/libraries/src/Form/Form.php b/libraries/src/Form/Form.php index 700cf67387595..8abd4588c91b9 100644 --- a/libraries/src/Form/Form.php +++ b/libraries/src/Form/Form.php @@ -186,14 +186,15 @@ protected function bindLevel($group, $data) /** * Method to filter the form data. * - * @param array $data An array of field values to filter. - * @param string $group The dot-separated form group path on which to filter the fields. + * @param array $data An array of field values to filter. + * @param string $group The dot-separated form group path on which to filter the fields. + * @param bool $ensureValues Auto-inject empty values for fields which are supposed to always have values. * * @return mixed Array or false. * * @since 11.1 */ - public function filter($data, $group = null) + public function filter($data, $group = null, $ensureValues = true) { // Make sure there is a valid JForm XML document. if (!($this->xml instanceof \SimpleXMLElement)) @@ -230,6 +231,11 @@ public function filter($data, $group = null) { $output->set($key, $this->filterField($field, $input->get($key, (string) $field['default']))); } + // Auto-inject values. + elseif ($ensureValues && ($fieldInstance = $this->getField($name, $group)) && !empty($fieldInstance->ensureValue)) + { + $output->set($key, null); + } } return $output->toArray(); diff --git a/plugins/system/fields/fields.php b/plugins/system/fields/fields.php index 23afd468bd8e0..b815b78cad483 100644 --- a/plugins/system/fields/fields.php +++ b/plugins/system/fields/fields.php @@ -44,8 +44,8 @@ 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)) + // Check if data is an array and the item has an id and we have custom fields data + if (!is_array($data) || empty($item->id) || empty($data['com_fields'])) { return true; } @@ -78,26 +78,25 @@ 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; - - // 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')))) + // Update field value if we have it. + if (array_key_exists($field->name, $data['com_fields'])) { - $value = json_encode($value); - } + $value = $data['com_fields'][$field->name]; + + // JSON encode value for complex fields + if (is_array($value) && count($value, COUNT_NORMAL) !== count($value, COUNT_RECURSIVE)) + { + $value = json_encode($value); + } - // Setting the value for the field and the item - $model->setFieldValue($field->id, $item->id, $value); + $model->setFieldValue($field->id, $item->id, $value); + } } return true; @@ -126,14 +125,6 @@ public function onUserAfterSave($userData, $isNew, $success, $msg) $user = JFactory::getUser($userData['id']); - $task = JFactory::getApplication()->input->getCmd('task'); - - // Skip fields save when we activate a user, because we will lose the saved data - if (in_array($task, array('activate', 'block', 'unblock'))) - { - return true; - } - // Trigger the events with a real user $this->onContentAfterSave('com_users.user', $user, false, $userData);