From 75f59466ea63316b5036feaccad25726f8f28c47 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 13 Feb 2014 16:03:13 -0800 Subject: [PATCH] CRM-13929 Decouple entityRef field rendering from CRM_Core_Form --- CRM/Core/Form.php | 47 -------------------------------------- CRM/Core/Form/Renderer.php | 35 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 47 deletions(-) diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 5bf14d29ff..f814dc9be0 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -112,14 +112,6 @@ class CRM_Core_Form extends HTML_QuickForm_Page { */ public $urlPath = array(); - /** - * Stores info about reference fields for preprocessing - * Public so that hooks can access it - * - * @var array - */ - public $entityReferenceFields = array(); - /** * constants for attributes for various form elements * attempt to standardize on the number of variations that we @@ -414,8 +406,6 @@ class CRM_Core_Form extends HTML_QuickForm_Page { // the user can do both the form and set default values with this hook CRM_Utils_Hook::buildForm(get_class($this), $this); - $this->preprocessReferenceFields(); - $this->addRules(); } @@ -1302,7 +1292,6 @@ class CRM_Core_Form extends HTML_QuickForm_Page { } $props['select'] = CRM_Utils_Array::value('select', $props, array()) + $defaults; - $this->entityReferenceFields[] = $name; $this->formatReferenceFieldAttributes($props); return $this->add('text', $name, $label, $props, $required); } @@ -1320,42 +1309,6 @@ class CRM_Core_Form extends HTML_QuickForm_Page { CRM_Utils_Array::remove($props, 'multiple', 'select', 'api', 'entity', 'placeholder', 'create'); } - /** - * Convert IDs to values and format for display - */ - private function preprocessReferenceFields() { - foreach ($this->entityReferenceFields as $name) { - $field = $this->getElement($name); - $val = $field->getValue(); - // Support array values - if (is_array($val)) { - $val = implode(',', $val); - $field->setValue($val); - } - if ($val) { - $data = array(); - $entity = $field->getAttribute('data-api-entity'); - $select = json_decode($field->getAttribute('data-select-params'), TRUE); - // Support serialized values - if (strpos($val, CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE) { - $val = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($val, CRM_Core_DAO::VALUE_SEPARATOR)); - $field->setValue($val); - } - $result = civicrm_api3($entity, 'getlist', array('params' => array('id' => $val))); - if ($field->isFrozen()) { - $field->removeAttribute('class'); - } - if (!empty($result['values'])) { - // Simplify array for single selects - makes client-side code simpler (but feels somehow wrong) - if (empty($select['multiple'])) { - $result['values'] = $result['values'][0]; - } - $field->setAttribute('data-entity-value', json_encode($result['values'])); - } - } - } - } - /** * Convert all date fields within the params to mysql date ready for the * BAO layer. In this case fields are checked against the $_datefields defined for the form diff --git a/CRM/Core/Form/Renderer.php b/CRM/Core/Form/Renderer.php index 02b6e74cc4..52efe27aa5 100644 --- a/CRM/Core/Form/Renderer.php +++ b/CRM/Core/Form/Renderer.php @@ -183,6 +183,9 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty { if (!$class || strpos($class, 'crm-form-') === FALSE) { $class = ($class ? "$class " : '') . 'crm-form-' . $type; } + elseif (strpos($class, 'crm-form-entityref') !== FALSE) { + self::preProcessEntityRef($element); + } if ($required) { $class .= ' required'; @@ -196,6 +199,38 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty { $element->updateAttributes($attributes); } + /** + * Convert IDs to values and format for display + */ + static function preProcessEntityRef($field) { + $val = $field->getValue(); + // Support array values + if (is_array($val)) { + $val = implode(',', $val); + $field->setValue($val); + } + if ($val) { + $entity = $field->getAttribute('data-api-entity'); + $select = json_decode($field->getAttribute('data-select-params'), TRUE); + // Support serialized values + if (strpos($val, CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE) { + $val = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($val, CRM_Core_DAO::VALUE_SEPARATOR)); + $field->setValue($val); + } + $result = civicrm_api3($entity, 'getlist', array('params' => array('id' => $val))); + if ($field->isFrozen()) { + $field->removeAttribute('class'); + } + if (!empty($result['values'])) { + // Simplify array for single selects - makes client-side code simpler (but feels somehow wrong) + if (empty($select['multiple'])) { + $result['values'] = $result['values'][0]; + } + $field->setAttribute('data-entity-value', json_encode($result['values'])); + } + } + } + /** * Render entity references as text. * If user has permission, format as link (or now limited to contacts). -- 2.25.1