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
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • 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 Twitter-Style Dropdowns Using MooTools

    Twitter does some great stuff with JavaScript. What I really appreciate about what they do is that there aren't any epic JS functionalities -- they're all simple touches. One of those simple touches is the "Login" dropdown on their homepage. I've taken...

  • By
    CSS Scoped Styles

    There are plenty of awesome new attributes we've gotten during the HTML5 revolution:  placeholder, download, hidden, and more.  Each of these attributes provides us a different level of control over an element on the page, but there's a new element attribute that allows...

Discussion

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