Cleanup redundant caching of custom fields
authorColeman Watts <coleman@civicrm.org>
Mon, 28 Dec 2015 17:43:02 +0000 (12:43 -0500)
committerColeman Watts <coleman@civicrm.org>
Mon, 28 Dec 2015 17:43:02 +0000 (12:43 -0500)
CRM/Core/BAO/CustomField.php
CRM/Core/Form/Renderer.php
CRM/Core/PseudoConstant.php

index f886d41ee1179b807f499a8db9ec617db51006b7..1494bca00586530bd12d1207a199fc7471f59d00 100644 (file)
@@ -57,11 +57,6 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    */
   public static $_importFields = NULL;
 
-  /**
-   * @var array
-   */
-  public static $displayInfoCache = array();
-
   /**
    * Build and retrieve the list of data types and descriptions.
    *
@@ -404,9 +399,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
       return FALSE;
     }
     CRM_Utils_Hook::customFieldOptions($this->id, $options, FALSE);
-    $entity = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->custom_group_id, 'extends');
-    $entity = in_array($entity, array('Individual', 'Household', 'Organization')) ? 'Contact' : $entity;
-    CRM_Utils_Hook::fieldOptions($entity, "custom_{$this->id}", $options, array('context' => $context));
+    CRM_Utils_Hook::fieldOptions($this->getEntity(), "custom_{$this->id}", $options, array('context' => $context));
     return $options;
   }
 
@@ -748,11 +741,11 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    * @param int $fieldID
    *   The custom field ID.
    *
-   * @return CRM_Core_DAO_CustomField
+   * @return CRM_Core_BAO_CustomField
    *   The field object.
    */
   public static function getFieldObject($fieldID) {
-    $field = new CRM_Core_DAO_CustomField();
+    $field = new CRM_Core_BAO_CustomField();
 
     // check if we can get the field values from the system cache
     $cacheKey = "CRM_Core_DAO_CustomField_{$fieldID}";
@@ -827,10 +820,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
     )));
 
     if ($isSelect) {
-      $options = CRM_Utils_Array::value('values', civicrm_api3('contact', 'getoptions', array(
-        'field' => "custom_$fieldId",
-        'context' => $search ? 'search' : 'create',
-      )), array());
+      $options = $field->getOptions($search ? 'search' : 'create');
 
       // Consolidate widget types to simplify the below switch statement
       if ($search || ($widget !== 'AdvMulti-Select' && strpos($widget, 'Select') !== FALSE)) {
@@ -849,8 +839,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
       // Add data for popup link. Normally this is handled by CRM_Core_Form->addSelect
       if ($field->option_group_id && !$search && $widget == 'Select' && CRM_Core_Permission::check('administer CiviCRM')) {
         $selectAttributes += array(
-          'data-api-entity' => 'contact',
-          // FIXME: This works because the getoptions api isn't picky about custom fields, but it's WRONG
+          'data-api-entity' => $field->getEntity(),
           'data-api-field' => 'custom_' . $field->id,
           'data-option-edit-path' => 'civicrm/admin/options/' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $field->option_group_id),
         );
@@ -1026,7 +1015,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
         }
         if ($field->data_type == 'ContactReference') {
           $attributes['class'] = (isset($attributes['class']) ? $attributes['class'] . ' ' : '') . 'crm-form-contact-reference huge';
-          $attributes['data-api-entity'] = 'contact';
+          $attributes['data-api-entity'] = 'Contact';
           $element = $qf->add('text', $elementName, $label, $attributes, $useRequired && !$search);
 
           $urlParams = "context=customfield&id={$field->id}";
@@ -1137,22 +1126,13 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
       throw new Exception('CRM_Core_BAO_CustomField::displayValue requires a field id');
     }
 
-    if (!isset(self::$displayInfoCache[$fieldId])) {
-      if (!is_a($field, 'CRM_Core_BAO_CustomField')) {
-        $field = new CRM_Core_BAO_CustomField();
-        $field->id = $fieldId;
-      }
-      $options = $field->getOptions();
-      self::$displayInfoCache[$fieldId] = array(
-        'id' => $fieldId,
-        'html_type' => $field->html_type,
-        'data_type' => $field->data_type,
-        'date_format' => $field->date_format,
-        'time_format' => $field->time_format,
-        'options' => $options,
-      );
+    if (!is_a($field, 'CRM_Core_BAO_CustomField')) {
+      $field = self::getFieldObject($fieldId);
     }
-    return self::formatDisplayValue($value, self::$displayInfoCache[$fieldId], $contactId);
+
+    $fieldInfo = array('options' => $field->getOptions()) + (array) $field;
+
+    return self::formatDisplayValue($value, $fieldInfo, $contactId);
   }
 
   /**
@@ -2383,6 +2363,16 @@ WHERE cf.id = %1 AND cg.is_multiple = 1";
     return ($field['html_type'] == 'CheckBox' || strpos($field['html_type'], 'Multi') !== FALSE);
   }
 
+  /**
+   * Get api entity for this field
+   *
+   * @return string
+   */
+  public function getEntity() {
+    $entity = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->custom_group_id, 'extends');
+    return in_array($entity, array('Individual', 'Household', 'Organization')) ? 'Contact' : $entity;
+  }
+
   /**
    * Set pseudoconstant properties for field metadata.
    *
index 60e997eeeee39fee424e5077b96421c70375e083..6ea326b8c1eb4168ff179d9a18e2538a0d66f54c 100644 (file)
@@ -256,7 +256,7 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty {
    * @param HTML_QuickForm_element $field
    */
   public function renderFrozenEntityRef(&$el, $field) {
-    $entity = $field->getAttribute('data-api-entity');
+    $entity = strtolower($field->getAttribute('data-api-entity'));
     $vals = json_decode($field->getAttribute('data-entity-value'), TRUE);
     $display = array();
 
index ce2f161829b63e5fa193e5eff66a3a38d96e3e4d..1debce267600ecdda11d746e7f8f180ef0298351 100644 (file)
@@ -591,7 +591,6 @@ class CRM_Core_PseudoConstant {
     }
     if ($name == 'cache') {
       CRM_Core_OptionGroup::flushAll();
-      CRM_Core_BAO_CustomField::$displayInfoCache = array();
     }
   }