Retrieve Custom Field Values for Phabricator Objects

By  on  

Phabricator is an incredibly well-coded PHP application but lacks a bit on the documentation side.  Then again, what open source project isn't?  Over the past months I've been slowly integrating Phabricator, a newish review tool, with Bugzilla, a much older tool.  The most recent step saw me needing to add a custom field (DifferentialCommitMessageCustomField class extension) to revisions:

Phabricator Bug ID

Creating the DifferentialCommitMessageCustomField extension was fairly simple but, due to the structure of DifferentialRevision and other Phabricator classes, retrieving that field value isn't as easy as one would hope.  After trying a variety of methods and combing through the classes chain source code, it was Aviv Eyal that provided a nice utility function for retrieving custom field data for a given Phabricator object type:

function get_custom_field_value($object, $key_field) {
  $field = PhabricatorCustomField::getObjectField(
    $object,
    PhabricatorCustomField::ROLE_DEFAULT,
    $key_field
  );

  id(new PhabricatorCustomFieldStorageQuery())
  ->addField($field)
  ->execute();

  $value = $field->getValueForStorage();

  return $value;
}

In my case, the $object was a DifferentialRevision and the $key_field is differential:bugzilla-bug-id, defined within my DifferentialCommitMessageCustomField extension.  If you are adding a CustomField of any kind to any Phabrication application, you can use this same method.

When documentation is lacking and inheritance levels a dozen levels deep, it's great that community members can jump in to help each other -- it speaks volumes for the project.

Recent Features

  • By
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

Incredible Demos

  • By
    Create an Animated Sliding Button Using MooTools

    Buttons (or links) are usually the elements on our sites that we want to draw a lot of attention to. Unfortunately many times they end up looking the most boring. You don't have to let that happen though! I recently found a...

  • By
    Duplicate the jQuery Homepage Tooltips Using Dojo

    The jQuery homepage has a pretty suave tooltip-like effect as seen below: Here's how to accomplish this same effect using Dojo. The XHTML The above HTML was taken directly from the jQuery homepage -- no changes. The CSS The above CSS has been slightly modified to match the CSS rules already...

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!