Improve api.getoptions to return nested groups/tags
[civicrm-core.git] / CRM / Core / BAO / EntityTag.php
index 9cb2ec4f77ba09e64e111506dc6bd57032630d0c..e6b2d4bebbb7e96e0c93055ef6be79f8d0f392ab 100644 (file)
@@ -378,4 +378,28 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag {
     $tags['status'] = TRUE;
     return $tags;
   }
+
+  /**
+   * Get options for a given field.
+   * @see CRM_Core_DAO::buildOptions
+   *
+   * @param String $fieldName
+   * @param String $context : @see CRM_Core_DAO::buildOptionsContext
+   * @param Array $props : whatever is known about this dao object
+   *
+   * @return Array|bool
+   */
+  public static function buildOptions($fieldName, $context = NULL, $props = array()) {
+    $params = array();
+
+    $options = CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
+
+    // Output tag list as nested hierarchy
+    // TODO: This will only work when api.entity is "entity_tag". What about others?
+    if (($fieldName == 'tag' || $fieldName == 'tag_id') && ($context == 'search' || $context == 'create')) {
+      $options = CRM_Core_BAO_Tag::getTags('civicrm_contact', CRM_Core_DAO::$_nullArray, CRM_Utils_Array::value('parent_id', $params), '- ');
+    }
+
+    return $options;
+  }
 }