@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Simplify and improve PhabricatorCustomField APIs

Summary:
Ref T1703. Ref T3718. The `PhabricatorCustomFieldList` seems like a pretty good idea. Move more code into it to make it harder to get wrong.

Also the sequencing on old/new values for these transactions was a bit off; fix that up.

Test Plan: Edited standard and custom profile fields.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T1703, T3718

Differential Revision: https://secure.phabricator.com/D6751

+57 -28
+1 -2
src/applications/people/controller/PhabricatorPeopleProfileController.php
··· 97 97 ->setUser($viewer) 98 98 ->setObject($user); 99 99 100 - $fields = PhabricatorCustomField::getObjectFields( 100 + $field_list = PhabricatorCustomField::getObjectFields( 101 101 $user, 102 102 PhabricatorCustomField::ROLE_VIEW); 103 - $field_list = new PhabricatorCustomFieldList($fields); 104 103 $field_list->appendFieldsToPropertyList($user, $viewer, $view); 105 104 106 105 return $view;
+5 -12
src/applications/people/controller/PhabricatorPeopleProfileEditController.php
··· 32 32 33 33 $profile_uri = '/p/'.$user->getUsername().'/'; 34 34 35 - $fields = PhabricatorCustomField::getObjectFields( 35 + $field_list = PhabricatorCustomField::getObjectFields( 36 36 $user, 37 37 PhabricatorCustomField::ROLE_EDIT); 38 - $field_list = new PhabricatorCustomFieldList($fields); 38 + $field_list->readFieldsFromStorage($user); 39 39 40 40 if ($request->isFormPost()) { 41 - $xactions = array(); 42 - foreach ($fields as $field) { 43 - $field->readValueFromRequest($request); 44 - $xactions[] = id(new PhabricatorUserTransaction()) 45 - ->setTransactionType(PhabricatorTransactions::TYPE_CUSTOMFIELD) 46 - ->setMetadataValue('customfield:key', $field->getFieldKey()) 47 - ->setNewValue($field->getNewValueForApplicationTransactions()); 48 - } 41 + $xactions = $field_list->buildFieldTransactionsFromRequest( 42 + new PhabricatorUserTransaction(), 43 + $request); 49 44 50 45 $editor = id(new PhabricatorUserProfileEditor()) 51 46 ->setActor($viewer) ··· 56 51 $editor->applyTransactions($user, $xactions); 57 52 58 53 return id(new AphrontRedirectResponse())->setURI($profile_uri); 59 - } else { 60 - $field_list->readFieldsFromStorage($user); 61 54 } 62 55 63 56 $title = pht('Edit Profile');
+2 -2
src/applications/people/storage/PhabricatorUser.php
··· 850 850 return $this->customFields[$role]; 851 851 } 852 852 853 - public function attachCustomFields($role, array $fields) { 854 - $this->customFields[$role] = $fields; 853 + public function attachCustomFields($role, PhabricatorCustomFieldList $list) { 854 + $this->customFields[$role] = $list; 855 855 return $this; 856 856 } 857 857
+10 -5
src/applications/transactions/editor/PhabricatorApplicationTransactionEditor.php
··· 125 125 } 126 126 return $old_edges; 127 127 case PhabricatorTransactions::TYPE_CUSTOMFIELD: 128 - $field = $this->getCustomFieldForTransaction($object, $xaction); 129 - return $field->getOldValueForApplicationTransactions(); 128 + // NOTE: Custom fields have their old value pre-populated when they are 129 + // built by PhabricatorCustomFieldList. 130 + return $xaction->getOldValue(); 130 131 default: 131 132 return $this->getCustomTransactionOldValue($object, $xaction); 132 133 } ··· 585 586 throw new Exception( 586 587 "You can not apply transactions which already have commentVersions!"); 587 588 } 588 - if ($xaction->getOldValue() !== null) { 589 - throw new Exception( 590 - "You can not apply transactions which already have oldValue!"); 589 + 590 + $custom_field_type = PhabricatorTransactions::TYPE_CUSTOMFIELD; 591 + if ($xaction->getTransactionType() != $custom_field_type) { 592 + if ($xaction->getOldValue() !== null) { 593 + throw new Exception( 594 + "You can not apply transactions which already have oldValue!"); 595 + } 591 596 } 592 597 593 598 $type = $xaction->getTransactionType();
+5 -4
src/infrastructure/customfield/field/PhabricatorCustomField.php
··· 49 49 $role) { 50 50 51 51 try { 52 - $fields = $object->getCustomFields($role); 52 + $field_list = $object->getCustomFields($role); 53 53 } catch (PhabricatorCustomFieldNotAttachedException $ex) { 54 54 $base_class = $object->getCustomFieldBaseClass(); 55 55 ··· 73 73 $field->setObject($object); 74 74 } 75 75 76 - $object->attachCustomFields($role, $fields); 76 + $field_list = new PhabricatorCustomFieldList($fields); 77 + $object->attachCustomFields($role, $field_list); 77 78 } 78 79 79 - return $fields; 80 + return $field_list; 80 81 } 81 82 82 83 ··· 87 88 PhabricatorCustomFieldInterface $object, 88 89 $role, 89 90 $field_key) { 90 - return idx(self::getObjectFields($object, $role), $field_key); 91 + return idx(self::getObjectFields($object, $role)->getFields(), $field_key); 91 92 } 92 93 93 94
+31
src/infrastructure/customfield/field/PhabricatorCustomFieldList.php
··· 16 16 $this->fields = $fields; 17 17 } 18 18 19 + public function getFields() { 20 + return $this->fields; 21 + } 19 22 20 23 /** 21 24 * Read stored values for all fields which support storage. ··· 118 121 } 119 122 } 120 123 } 124 + } 125 + 126 + public function buildFieldTransactionsFromRequest( 127 + PhabricatorApplicationTransaction $template, 128 + AphrontRequest $request) { 129 + 130 + $xactions = array(); 131 + 132 + $role = PhabricatorCustomField::ROLE_APPLICATIONTRANSACTIONS; 133 + foreach ($this->fields as $field) { 134 + if (!$field->shouldEnableForRole($role)) { 135 + continue; 136 + } 137 + 138 + $old_value = $field->getOldValueForApplicationTransactions(); 139 + 140 + $field->readValueFromRequest($request); 141 + 142 + $xaction = id(clone $template) 143 + ->setTransactionType(PhabricatorTransactions::TYPE_CUSTOMFIELD) 144 + ->setMetadataValue('customfield:key', $field->getFieldKey()) 145 + ->setOldValue($old_value) 146 + ->setNewValue($field->getNewValueForApplicationTransactions()); 147 + 148 + $xactions[] = $xaction; 149 + } 150 + 151 + return $xactions; 121 152 } 122 153 123 154 }
+3 -3
src/infrastructure/customfield/interface/PhabricatorCustomFieldInterface.php
··· 5 5 public function getCustomFieldBaseClass(); 6 6 public function getCustomFieldSpecificationForRole($role); 7 7 public function getCustomFields($role); 8 - public function attachCustomFields($role, array $fields); 8 + public function attachCustomFields($role, PhabricatorCustomFieldList $list); 9 9 10 10 } 11 11 ··· 33 33 return $this->customFields[$role]; 34 34 } 35 35 36 - public function attachCustomFields($role, array $fields) { 37 - $this->customFields[$role] = $fields; 36 + public function attachCustomFields($role, PhabricatorCustomFieldList $list) { 37 + $this->customFields[$role] = $list; 38 38 return $this; 39 39 } 40 40