Page MenuHomePhabricator
Paste P2080

CopyPHIDAction.php
ActivePublic

Authored by avivey on Dec 7 2017, 12:47 PM.
<?php
/**
* Dump this in /src/extensions, and get a "Copy PHID to clipboard" action on every page.
*/
final class CopyPHIDAction extends PhabricatorAutoEventListener {
public function register() {
$this->listen(PhabricatorEventType::TYPE_UI_DIDRENDERACTIONS);
}
public function handleEvent(PhutilEvent $event) {
switch ($event->getType()) {
case PhabricatorEventType::TYPE_UI_DIDRENDERACTIONS:
$this->handleActionEvent($event);
break;
}
}
private function handleActionEvent($event) {
$viewer = $event->getUser();
$object = $event->getValue('object');
if (!$object || !$object->getPHID()) {
return;
}
Javelin::initBehavior('phabricator-clipboard-copy');
$actions = $event->getValue('actions');
$actions[] = id(new PhabricatorActionView())
->setHref('#')
->setRenderAsForm(true)
->setName(pht('Copy PHID (%s)', phid_get_type($object->getPHID())))
->setIcon('fa-clipboard')
->addSigil('clipboard-copy')
->setMetadata(array('text' => $object->getPHID()));
$event->setValue('actions', $actions);
}
}