Use customfield with ManiphestTaskSearchEngine

Hey all,

i have a Maniphest customfield “Tasktype” which gets it’s values from a configuration option. That works fine. Is there any way to make this customfield a constraint in a ManiphestTaskSearchEngine search or do i have to use the Conduit endpoint for this?

Thanks and take care
Florian

You can’t do it with conduit either; see Can't add custom fields as constraints in maniphest.search conduit endpoint

Hi @jbrownEP and others,

actually, it is possible. Digging deep into the ManiphestTaskSearchEngine and PhabricatorApplicationSearchEngine i found out that the Maniphest Advanced Search UI is using only SavedQueries (calling newSavedQuery() on the ManiphestTaskSearchEngine instead of Queries (calling newQuery() on the ManiphestTaskSearchEngine.

A savedQuery has a method setParameter() which takes a fieldKey and a fieldValue as parameters. Sorrowly the savedQuery then has no method execute or the likes, so when you have setup your savedQuery you then have to build a Query from it. Luckily the ManiphestTaskSearchEngine (and thus, i guess, all other search engines) have a method buildQueryFromSavedQuery() which takes the savedQuery as argument and returns a Query which you then can handle as usualy.

Fiddling with custmfields was then another thing as the fieldKeyfor them is a combination of custom:customFieldIndex. But the fieldIndex can be get with the method getFieldIndex() from an instance of the customfield class.

So, all in all, a rough outline example looks like:

/* Get customfield instance */
$t_field = id(new LiquidgumboManiphestCustomFieldType())
 ->setViewer($this->getViewer());

/* Setup task query engine */
 $tmp_search_engine = id(new ManiphestTaskSearchEngine())
 ->setViewer($this->getViewer());

/* Get new saved query */
$tmp_query = $tmp_search_engine->newSavedQuery();

/* Set parameters projectPHIDs and customfield */
$tmp_query->setParameter(
  'projectPHIDs',
  array('PHID-PROJ-gqz2ajx5lfm7q2kr6jey')
);
$tmp_query->setParameter(
  'custom:'.$t_field->getFieldIndex(),
  array('Task')
);

/* Build query from savedQuery */
$query = $tmp_search_engine->buildQueryFromSavedQuery($tmp_query);

/* Setup query */
$query->setViewer($this->getViewer());

/* Get tasks */
$tasks = $query->execute();

I have no idea if this is valid in the Phabricator world but it works and as it seems, making it work is what you have to do when dealing with a bit more advanced coding in Phabricator :slight_smile: . I sure would like to get an opinion from more experienced Phabricator coders on this.

Take care
Florian

Oh, yeah, you can totally do it in PHP. I was just pointing out that you can’t do it from Conduit unless you want to maintain a fork of Phabricator.