@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.

Move token rendering into Datasources

Summary:
Ref T4100. I want to reduce the amount of code duplication that function datasources currently need to wrap some parameter datasource.

For example, `ProjectMembersDatasource` should really just be a little bit of logic on top of `ProjectsDatasource`, which should do most of the heavy lifting.

Moving rendering into datasources brings us a step closer to being able to do this.

Test Plan:
- Rendered normal, function, and invalid tokens.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T4100

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

+58 -25
+56
src/applications/typeahead/datasource/PhabricatorTypeaheadDatasource.php
··· 199 199 ->setTokenType(PhabricatorTypeaheadTokenView::TYPE_INVALID); 200 200 } 201 201 202 + public function renderTokens(array $values) { 203 + $phids = array(); 204 + $setup = array(); 205 + $tokens = array(); 206 + 207 + foreach ($values as $key => $value) { 208 + if (!self::isFunctionToken($value)) { 209 + $phids[$key] = $value; 210 + } else { 211 + $function = $this->parseFunction($value); 212 + if ($function) { 213 + $setup[$function['name']][$key] = $function; 214 + } else { 215 + $name = pht('Invalid Function: %s', $value); 216 + $tokens[$key] = $this->newInvalidToken($name) 217 + ->setKey($value); 218 + } 219 + } 220 + } 221 + 222 + if ($phids) { 223 + $handles = $this->getViewer()->loadHandles($phids); 224 + foreach ($phids as $key => $phid) { 225 + $handle = $handles[$phid]; 226 + $tokens[$key] = PhabricatorTypeaheadTokenView::newFromHandle($handle); 227 + } 228 + } 229 + 230 + if ($setup) { 231 + foreach ($setup as $function_name => $argv_list) { 232 + // Render the function tokens. 233 + $function_tokens = $this->renderFunctionTokens( 234 + $function_name, 235 + ipull($argv_list, 'argv')); 236 + 237 + // Rekey the function tokens using the original array keys. 238 + $function_tokens = array_combine( 239 + array_keys($argv_list), 240 + $function_tokens); 241 + 242 + // For any functions which were invalid, set their value to the 243 + // original input value before it was parsed. 244 + foreach ($function_tokens as $key => $token) { 245 + $type = $token->getTokenType(); 246 + if ($type == PhabricatorTypeaheadTokenView::TYPE_INVALID) { 247 + $token->setKey($values[$key]); 248 + } 249 + } 250 + 251 + $tokens += $function_tokens; 252 + } 253 + } 254 + 255 + return array_select_keys($tokens, array_keys($values)); 256 + } 257 + 202 258 /* -( Token Functions )---------------------------------------------------- */ 203 259 204 260
+2 -25
src/view/form/control/AphrontFormTokenizerControl.php
··· 62 62 $placeholder = $datasource->getPlaceholderText(); 63 63 } 64 64 65 - $tokens = array(); 66 65 $values = nonempty($this->getValue(), array()); 67 - foreach ($values as $value) { 68 - if (isset($handles[$value])) { 69 - $token = PhabricatorTypeaheadTokenView::newFromHandle($handles[$value]); 70 - } else { 71 - $token = null; 66 + $tokens = $datasource->renderTokens($values); 72 67 73 - $function = $datasource->parseFunction($value); 74 - if ($function) { 75 - $token_list = $datasource->renderFunctionTokens( 76 - $function['name'], 77 - array($function['argv'])); 78 - $token = head($token_list); 79 - } 80 - 81 - if (!$token) { 82 - $name = pht('Invalid Function: %s', $value); 83 - $token = $datasource->newInvalidToken($name); 84 - } 85 - 86 - $type = $token->getTokenType(); 87 - if ($type == PhabricatorTypeaheadTokenView::TYPE_INVALID) { 88 - $token->setKey($value); 89 - } 90 - } 68 + foreach ($tokens as $token) { 91 69 $token->setInputName($this->getName()); 92 - $tokens[] = $token; 93 70 } 94 71 95 72 $template = new AphrontTokenizerTemplateView();