@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<?php
2
3final class PhabricatorStandardSelectCustomFieldDatasource
4 extends PhabricatorTypeaheadDatasource {
5
6 public function getBrowseTitle() {
7 return pht('Browse Values');
8 }
9
10 public function getPlaceholderText() {
11 return pht('Type a field value...');
12 }
13
14 public function getDatasourceApplicationClass() {
15 return null;
16 }
17
18 public function loadResults() {
19 $viewer = $this->getViewer();
20
21 $class = $this->getParameter('object');
22 if (!$class) {
23 throw new Exception(
24 pht('You must set a custom field class!'));
25 }
26 if (!class_exists($class)) {
27 throw new Exception(
28 pht(
29 'Custom field class "%s" does not exist.',
30 $class));
31 }
32
33 $reflection = new ReflectionClass($class);
34 $interface = 'PhabricatorCustomFieldInterface';
35 if (!$reflection->implementsInterface($interface)) {
36 throw new Exception(
37 pht(
38 'Custom field class "%s" does not implement interface "%s".',
39 $class,
40 $interface));
41 }
42
43 $role = $this->getParameter('role');
44 if (!strlen($role)) {
45 throw new Exception(pht('No custom field role specified.'));
46 }
47
48 $object = newv($class, array());
49 $field_list = PhabricatorCustomField::getObjectFields($object, $role);
50
51 $field_key = $this->getParameter('key');
52 if (!strlen($field_key)) {
53 throw new Exception(pht('No custom field key specified.'));
54 }
55
56 $field = null;
57 foreach ($field_list->getFields() as $candidate_field) {
58 if ($candidate_field->getFieldKey() == $field_key) {
59 $field = $candidate_field;
60 break;
61 }
62 }
63
64 if ($field === null) {
65 throw new Exception(
66 pht(
67 'No field with field key "%s" exists for objects of class "%s" with '.
68 'custom field role "%s".',
69 $field_key,
70 $class,
71 $role));
72 }
73
74 if (!($field instanceof PhabricatorStandardCustomFieldSelect)) {
75 $field = $field->getProxy();
76 if (!($field instanceof PhabricatorStandardCustomFieldSelect)) {
77 throw new Exception(
78 pht(
79 'Field "%s" is not a standard select field, nor a proxy of a '.
80 'standard select field.',
81 $field_key));
82 }
83 }
84
85 $options = $field->getOptions();
86
87 $results = array();
88 foreach ($options as $key => $option) {
89 $results[] = id(new PhabricatorTypeaheadResult())
90 ->setName($option)
91 ->setPHID($key);
92 }
93
94 return $this->filterResultsAgainstTokens($results);
95 }
96
97}