Fix unrelease regression - fatal when editing relationship type Employer
authoreileen <emcnaughton@wikimedia.org>
Tue, 5 Jun 2018 02:06:29 +0000 (14:06 +1200)
committereileen <emcnaughton@wikimedia.org>
Tue, 5 Jun 2018 02:11:32 +0000 (14:11 +1200)
CRM/Admin/Form/RelationshipType.php
CRM/Core/Form/EntityFormTrait.php

index 387905615ceef991482bc44fe458ccaafd5d32ad..fde8d4c3b3d4b0bba4910d9662ad130e6b80c9bd 100644 (file)
@@ -50,6 +50,8 @@ class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form {
    *  - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']]
    *  - template - use a field specific template to render this field
    *  - required
+   *  - is_freeze (field should be frozen).
+   *
    * @var array
    */
   protected $entityFields = [];
@@ -102,6 +104,8 @@ class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form {
    * Build the form object.
    */
   public function buildQuickForm() {
+    $isReserved = ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $this->_id, 'is_reserved'));
+    $this->entityFields['is_active']['is_freeze'] = $isReserved;
 
     self::buildQuickEntityForm();
     if ($this->_action & CRM_Core_Action::DELETE) {
@@ -116,25 +120,14 @@ class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form {
     );
 
     $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, '__');
-
-    // add select for contact type
-    $this->add('select', 'contact_types_a', ts('Contact Type A') . ' ',
-      array(
-        '' => ts('All Contacts'),
-      ) + $contactTypes
-    );
-    $this->add('select', 'contact_types_b', ts('Contact Type B') . ' ',
-      array(
-        '' => ts('All Contacts'),
-      ) + $contactTypes
-    );
-
-    //only selected field should be allow for edit, CRM-4888
-    if ($this->_id &&
-      CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $this->_id, 'is_reserved')
-    ) {
-      foreach (array('contactTypeA', 'contactTypeB', 'isActive') as $field) {
-        $$field->freeze();
+    foreach (['contact_types_a' => ts('Contact Type A'), 'contact_types_b' => ts('Contact Type B')] as $name => $label) {
+      $element = $this->add('select', $name, $label . ' ',
+        array(
+          '' => ts('All Contacts'),
+        ) + $contactTypes
+      );
+      if ($isReserved) {
+        $element->freeze();
       }
     }
 
index 76b1a2cd69c91dca32923c7e235dc0157ac20c23..564edf6754e5b740a11149d9ccc30c0f6b75ae06 100644 (file)
@@ -161,7 +161,10 @@ trait CRM_Core_Form_EntityFormTrait {
   protected function addEntityFieldsToTemplate() {
     foreach ($this->getEntityFields() as $fieldSpec) {
       if (empty($fieldSpec['not-auto-addable'])) {
-        $this->addField($fieldSpec['name'], [], CRM_Utils_Array::value('required', $fieldSpec));
+        $element = $this->addField($fieldSpec['name'], [], CRM_Utils_Array::value('required', $fieldSpec));
+        if (!empty($fieldSpec['is_freeze'])) {
+          $element->freeze();
+        }
       }
     }
   }