Changeset View
Changeset View
Standalone View
Standalone View
src/view/form/control/AphrontFormTokenizerControl.php
Show All 29 Lines | public function setPlaceholder($placeholder) { | ||||
$this->placeholder = $placeholder; | $this->placeholder = $placeholder; | ||||
return $this; | return $this; | ||||
} | } | ||||
protected function renderInput() { | protected function renderInput() { | ||||
$name = $this->getName(); | $name = $this->getName(); | ||||
$values = nonempty($this->getValue(), array()); | $values = nonempty($this->getValue(), array()); | ||||
assert_instances_of($values, 'PhabricatorObjectHandle'); | // Values may either be handles (which are now legacy/deprecated) or | ||||
// strings. Load handles for any PHIDs. | |||||
$load = array(); | |||||
$handles = array(); | |||||
$select = array(); | |||||
foreach ($values as $value) { | |||||
if ($value instanceof PhabricatorObjectHandle) { | |||||
$handles[$value->getPHID()] = $value; | |||||
$select[] = $value->getPHID(); | |||||
} else { | |||||
$load[] = $value; | |||||
$select[] = $value; | |||||
} | |||||
} | |||||
// TODO: Once this code path simplifies, move this prefetch to setValue() | |||||
// so we can bulk load across multiple controls. | |||||
if ($load) { | |||||
$viewer = $this->getUser(); | |||||
if (!$viewer) { | |||||
// TODO: Clean this up when handles go away. | |||||
throw new Exception( | |||||
pht('Call setUser() before rendering tokenizer string values.')); | |||||
} | |||||
$loaded_handles = $viewer->loadHandles($load); | |||||
$handles = $handles + iterator_to_array($loaded_handles); | |||||
} | |||||
// Reorder the list into input order. | |||||
$handles = array_select_keys($handles, $select); | |||||
if ($this->getID()) { | if ($this->getID()) { | ||||
$id = $this->getID(); | $id = $this->getID(); | ||||
} else { | } else { | ||||
$id = celerity_generate_unique_node_id(); | $id = celerity_generate_unique_node_id(); | ||||
} | } | ||||
$placeholder = null; | $placeholder = null; | ||||
if (!strlen($this->placeholder)) { | if (!strlen($this->placeholder)) { | ||||
if ($this->datasource) { | if ($this->datasource) { | ||||
$placeholder = $this->datasource->getPlaceholderText(); | $placeholder = $this->datasource->getPlaceholderText(); | ||||
} | } | ||||
} else { | } else { | ||||
$placeholder = $this->placeholder; | $placeholder = $this->placeholder; | ||||
} | } | ||||
$template = new AphrontTokenizerTemplateView(); | $template = new AphrontTokenizerTemplateView(); | ||||
$template->setName($name); | $template->setName($name); | ||||
$template->setID($id); | $template->setID($id); | ||||
$template->setValue($values); | $template->setValue($handles); | ||||
$username = null; | $username = null; | ||||
if ($this->user) { | if ($this->user) { | ||||
$username = $this->user->getUsername(); | $username = $this->user->getUsername(); | ||||
} | } | ||||
$datasource_uri = null; | $datasource_uri = null; | ||||
if ($this->datasource) { | if ($this->datasource) { | ||||
$datasource_uri = $this->datasource->getDatasourceURI(); | $datasource_uri = $this->datasource->getDatasourceURI(); | ||||
} | } | ||||
if (!$this->disableBehavior) { | if (!$this->disableBehavior) { | ||||
Javelin::initBehavior('aphront-basic-tokenizer', array( | Javelin::initBehavior('aphront-basic-tokenizer', array( | ||||
'id' => $id, | 'id' => $id, | ||||
'src' => $datasource_uri, | 'src' => $datasource_uri, | ||||
'value' => mpull($values, 'getFullName', 'getPHID'), | 'value' => mpull($handles, 'getFullName', 'getPHID'), | ||||
'icons' => mpull($values, 'getIcon', 'getPHID'), | 'icons' => mpull($handles, 'getIcon', 'getPHID'), | ||||
'limit' => $this->limit, | 'limit' => $this->limit, | ||||
'username' => $username, | 'username' => $username, | ||||
'placeholder' => $placeholder, | 'placeholder' => $placeholder, | ||||
)); | )); | ||||
} | } | ||||
return $template->render(); | return $template->render(); | ||||
} | } | ||||
} | } |