Use description from schema if available when using entityForm
authorMatthew Wire (MJW Consulting) <mjw@mjwconsult.co.uk>
Fri, 19 Oct 2018 14:36:44 +0000 (15:36 +0100)
committerMatthew Wire (MJW Consulting) <mjw@mjwconsult.co.uk>
Sun, 21 Oct 2018 09:17:38 +0000 (10:17 +0100)
CRM/Admin/Form/RelationshipType.php
CRM/Core/Form.php
CRM/Core/Form/EntityFormTrait.php

index fde8d4c3b3d4b0bba4910d9662ad130e6b80c9bd..3ef04472ab75547004ba0e911996ad70b8487628 100644 (file)
@@ -44,6 +44,7 @@ class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form {
    * Fields may have keys
    *  - name (required to show in tpl from the array)
    *  - description (optional, will appear below the field)
+   *     Auto-added by setEntityFieldsMetadata unless specified here (use description => '' to hide)
    *  - not-auto-addable - this class will not attempt to add the field using addField.
    *    (this will be automatically set if the field does not have html in it's metadata
    *    or is not a core field on the form's entity).
@@ -70,11 +71,16 @@ class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form {
         'name' => 'label_b_a',
         'description' => ts("Label for the relationship from Contact B to Contact A. EXAMPLE: Contact B is 'Child of' Contact A. You may leave this blank for relationships where the name is the same in both directions (e.g. Spouse).")
       ],
-      'description' => ['name' => 'description'],
+      'description' => [
+        'name' => 'description',
+        'description' => ''
+      ],
       'contact_types_a' => ['name' => 'contact_types_a', 'not-auto-addable' => TRUE],
       'contact_types_b' => ['name' => 'contact_types_b', 'not-auto-addable' => TRUE],
       'is_active' => ['name' => 'is_active'],
     ];
+
+    self::setEntityFieldsMetadata();
   }
 
   /**
index 613bcf227deeb3854f3d1edf4f214b2eec23a4fe..9755ec7334b6ea2cc3f74dd00ba6e92c274dfbd9 100644 (file)
@@ -1292,7 +1292,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    *
    * Return string
    */
-  private function getApiAction() {
+  protected function getApiAction() {
     $action = $this->getAction();
     if ($action & (CRM_Core_Action::UPDATE + CRM_Core_Action::ADD)) {
       return 'create';
index 13f70574316fec26e0c370c1bdb17baf8c1a8e3c..30e81b7b3d698b197f0a9bf096ca3568587dde6b 100644 (file)
  */
 
 trait CRM_Core_Form_EntityFormTrait {
+
   /**
    * Get entity fields for the entity to be added to the form.
    *
-   * @var array
+   * @return array
    */
   public function getEntityFields() {
     return $this->entityFields;
@@ -51,7 +52,7 @@ trait CRM_Core_Form_EntityFormTrait {
   /**
    * Get entity fields for the entity to be added to the form.
    *
-   * @var array
+   * @return string
    */
   public function getDeleteMessage() {
     return $this->deleteMessage;
@@ -221,4 +222,22 @@ trait CRM_Core_Form_EntityFormTrait {
     return ($this->_action & CRM_Core_Action::DELETE);
   }
 
+  protected function setEntityFieldsMetadata() {
+    foreach ($this->entityFields as $field => &$props) {
+      if (!empty($props['not-auto-addable'])) {
+        // We can't load this field using metadata
+        continue;
+      }
+      // Resolve action.
+      if (empty($props['action'])) {
+        $props['action'] = $this->getApiAction();
+      }
+      $fieldSpec = civicrm_api3($this->getDefaultEntity(), 'getfield', $props);
+      $fieldSpec = $fieldSpec['values'];
+      if (!isset($props['description']) && isset($fieldSpec['description'])) {
+        $props['description'] = $fieldSpec['description'];
+      }
+    }
+  }
+
 }