}
}
+ /**
+ * Get the defaults for the entity.
+ */
+ protected function getEntityDefaults() {
+ $defaults = [];
+ if ($this->_action != CRM_Core_Action::DELETE &&
+ $this->getEntityId()
+ ) {
+ $params = ['id' => $this->getEntityId()];
+ $baoName = $this->_BAOName;
+ $baoName::retrieve($params, $defaults);
+ }
+ foreach ($this->entityFields as $fieldSpec) {
+ $value = CRM_Utils_Request::retrieveValue($fieldSpec['name'], $this->getValidationTypeForField($fieldSpec['name']));
+ if ($value !== FALSE) {
+ $defaults[$fieldSpec['name']] = $value;
+ }
+ }
+ return $defaults;
+ }
+
+ /**
+ * Get the validation rule to apply to a function.
+ *
+ * Alphanumeric is designed to always be safe & for now we just return
+ * that but in future we can use tighter rules for types like int, bool etc.
+ *
+ * @param string $fieldName
+ *
+ * @return string|int|bool
+ */
+ protected function getValidationTypeForField($fieldName) {
+ switch ($this->metadata[$fieldName]['type']) {
+ case CRM_Utils_Type::T_BOOLEAN:
+ return 'Boolean';
+
+ default:
+ return 'Alphanumeric';
+ }
+ }
+
/**
* Set translated fields.
*
* @return array
*/
public function setDefaultValues() {
- $defaults = array();
-
- if ($this->getEntityId()) {
- $params = array('id' => $this->getEntityId());
- $baoName = $this->_BAOName;
- $baoName::retrieve($params, $defaults);
- }
+ $defaults = $this->getEntityDefaults();
if ($this->_action & CRM_Core_Action::ADD) {
$defaults['is_active'] = 1;