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
    How to Create a RetroPie on Raspberry Pi – Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Incredible Demos

Discussion

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