Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F58422
ExampleCustomCategoryField.php
All Users
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
epriestley
Sep 16 2013, 10:25 PM
2013-09-16 22:25:24 (UTC+0)
Size
4 KB
Referenced Files
None
Subscribers
None
ExampleCustomCategoryField.php
View Options
<?php
final
class
ExampleCustomCategoryField
extends
ManiphestCustomField
{
private
$value
;
public
function
getFieldKey
()
{
return
'example:category'
;
}
public
function
getFieldName
()
{
return
pht
(
'Category'
);
}
private
function
getCategories
()
{
return
array
(
'fruit'
=>
array
(
'name'
=>
'Fruit'
,
'subcategories'
=>
array
(
'apple'
=>
'Apple'
,
'banana'
=>
'Banana'
,
),
),
'vegetables'
=>
array
(
'name'
=>
'Vegetables'
,
'subcategories'
=>
array
(
'carrots'
=>
'Carrot'
,
'celery'
=>
'Celery'
,
),
),
'other'
=>
array
(
'name'
=>
'Other'
,
'subcategories'
=>
array
(
'misc'
=>
'Miscellaneous'
,
'etc'
=>
'Et Cetera'
,
),
),
);
}
public
function
shouldAppearInEditView
()
{
return
true
;
}
public
function
shouldAppearInApplicationSearch
()
{
return
true
;
}
public
function
buildFieldIndexes
()
{
$indexes
=
array
();
// Create two indexes for this field: the subcategory, and the category
// the subcategory is a member of. This allows you to search for either
// "Vegetables" or "Carrots" to find things in the "Carrots" category.
$value
=
$this
->
value
;
if
(
$value
)
{
$indexes
[]
=
$this
->
newStringIndex
(
$value
);
foreach
(
$this
->
getCategories
()
as
$key
=>
$categories
)
{
foreach
(
$categories
[
'subcategories'
]
as
$subkey
=>
$subname
)
{
if
(
$subkey
==
$value
)
{
$indexes
[]
=
$this
->
newStringIndex
(
$key
);
break
2
;
}
}
}
}
return
$indexes
;
}
public
function
readApplicationSearchValueFromRequest
(
PhabricatorApplicationSearchEngine
$engine
,
AphrontRequest
$request
)
{
return
$request
->
getArr
(
$this
->
getFieldKey
());
}
public
function
applyApplicationSearchConstraintToQuery
(
PhabricatorApplicationSearchEngine
$engine
,
PhabricatorCursorPagedPolicyAwareQuery
$query
,
$value
)
{
if
(
$value
)
{
$query
->
withApplicationSearchContainsConstraint
(
$this
->
newStringIndex
(
null
),
$value
);
}
}
public
function
appendToApplicationSearchForm
(
PhabricatorApplicationSearchEngine
$engine
,
AphrontFormView
$form
,
$value
,
array
$handles
)
{
if
(!
is_array
(
$value
))
{
$value
=
array
();
}
$value
=
array_fuse
(
$value
);
$control
=
id
(
new
AphrontFormCheckboxControl
())
->
setLabel
(
$this
->
getFieldName
())
->
setName
(
$this
->
getFieldKey
());
foreach
(
$this
->
getCategories
()
as
$key
=>
$category
)
{
$control
->
addCheckbox
(
$this
->
getFieldKey
().
'[]'
,
$key
,
phutil_tag
(
'strong'
,
array
(),
$category
[
'name'
]),
isset
(
$value
[
$key
]));
foreach
(
$category
[
'subcategories'
]
as
$subkey
=>
$subname
)
{
$control
->
addCheckbox
(
$this
->
getFieldKey
().
'[]'
,
$subkey
,
"
\x
C2
\x
A0
\x
C2
\x
A0"
.
$subname
,
isset
(
$value
[
$subkey
]));
}
}
$form
->
appendChild
(
$control
);
}
public
function
renderEditControl
()
{
$options
=
array
();
foreach
(
$this
->
getCategories
()
as
$category
)
{
$options
[
$category
[
'name'
]]
=
$category
[
'subcategories'
];
}
return
id
(
new
AphrontFormSelectControl
())
->
setName
(
$this
->
getFieldKey
())
->
setLabel
(
$this
->
getFieldName
())
->
setValue
(
$this
->
value
)
->
setOptions
(
$options
);
}
public
function
shouldUseStorage
()
{
return
true
;
}
public
function
setValueFromStorage
(
$value
)
{
$this
->
value
=
$value
;
return
$this
;
}
public
function
getValueForStorage
()
{
return
$this
->
value
;
}
public
function
readValueFromRequest
(
AphrontRequest
$request
)
{
$this
->
value
=
$request
->
getStr
(
$this
->
getFieldKey
());
return
$this
;
}
public
function
shouldAppearInPropertyView
()
{
return
true
;
}
public
function
renderTransactionDescription
(
ManiphestTransaction
$transaction
,
$target
)
{
$name
=
$this
->
getCategoryName
(
$transaction
->
getNewValue
());
return
'categorized this task as "'
.
$name
.
'"'
;
}
public
function
renderPropertyViewValue
()
{
return
$this
->
getCategoryName
(
$this
->
value
);
}
private
function
getCategoryName
(
$value
)
{
foreach
(
$this
->
getCategories
()
as
$category
)
{
foreach
(
$category
[
'subcategories'
]
as
$key
=>
$name
)
{
if
(
$key
==
$value
)
{
return
$name
;
}
}
}
return
null
;
}
}
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
amazon-s3
Storage Format
Raw Data
Storage Handle
phabricator/eh/un/h5moxl5ry74ki5qx
Default Alt Text
ExampleCustomCategoryField.php (4 KB)
Attached To
Mode
P926 ExampleCustomCategoryField.php
Attached
Detach File
Event Timeline
Log In to Comment