@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 HeraldTokenizerFieldValue
4 extends HeraldFieldValue {
5
6 private $key;
7 private $datasource;
8 private $valueMap;
9
10 public function setKey($key) {
11 $this->key = $key;
12 return $this;
13 }
14
15 public function getKey() {
16 return $this->key;
17 }
18
19 public function setDatasource(PhabricatorTypeaheadDatasource $datasource) {
20 $this->datasource = $datasource;
21 return $this;
22 }
23
24 public function getDatasource() {
25 return $this->datasource;
26 }
27
28 public function setValueMap(array $value_map) {
29 $this->valueMap = $value_map;
30 return $this;
31 }
32
33 public function getValueMap() {
34 return $this->valueMap;
35 }
36
37 public function getFieldValueKey() {
38 if ($this->getKey() === null) {
39 throw new PhutilInvalidStateException('setKey');
40 }
41 return 'tokenizer.'.$this->getKey();
42 }
43
44 public function getControlType() {
45 return self::CONTROL_TOKENIZER;
46 }
47
48 protected function getControlTemplate() {
49 if ($this->getDatasource() === null) {
50 throw new PhutilInvalidStateException('setDatasource');
51 }
52
53 $datasource = $this->getDatasource();
54 $datasource->setViewer($this->getViewer());
55
56 return array(
57 'tokenizer' => array(
58 'datasourceURI' => $datasource->getDatasourceURI(),
59 'browseURI' => $datasource->getBrowseURI(),
60 'placeholder' => $datasource->getPlaceholderText(),
61 'limit' => $datasource->getLimit(),
62 ),
63 );
64 }
65
66 public function renderFieldValue($value) {
67 return $this->renderValueAsList($value, $for_transcript = false);
68 }
69
70 public function renderEditorValue($value) {
71 $viewer = $this->getViewer();
72 $value = (array)$value;
73
74 $datasource = $this->getDatasource()
75 ->setViewer($viewer);
76
77 return $datasource->getWireTokens($value);
78 }
79
80 public function renderTranscriptValue($value) {
81 return $this->renderValueAsList($value, $for_transcript = true);
82 }
83
84 private function renderValueAsList($value, $for_transcript) {
85 $viewer = $this->getViewer();
86 $value = (array)$value;
87
88 if (!$value) {
89 return phutil_tag('em', array(), pht('None'));
90 }
91
92 if ($this->valueMap !== null) {
93 foreach ($value as $k => $v) {
94 $value[$k] = idx($this->valueMap, $v, $v);
95 }
96
97 return implode(', ', $value);
98 }
99
100 $list = $viewer->renderHandleList($value);
101
102 if (!$for_transcript) {
103 $list->setAsInline(true);
104 }
105
106 return $list;
107 }
108
109}