Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/customfield/field/PhabricatorCustomField.php
Show First 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | try { | ||||
$spec = $object->getCustomFieldSpecificationForRole($role); | $spec = $object->getCustomFieldSpecificationForRole($role); | ||||
if (!is_array($spec)) { | if (!is_array($spec)) { | ||||
$obj_class = get_class($object); | $obj_class = get_class($object); | ||||
throw new Exception( | throw new Exception( | ||||
"Expected an array from getCustomFieldSpecificationForRole() for ". | "Expected an array from getCustomFieldSpecificationForRole() for ". | ||||
"object of class '{$obj_class}'."); | "object of class '{$obj_class}'."); | ||||
} | } | ||||
$fields = PhabricatorCustomField::buildFieldList($base_class, $spec); | $fields = PhabricatorCustomField::buildFieldList( | ||||
$base_class, | |||||
$spec, | |||||
$object); | |||||
foreach ($fields as $key => $field) { | foreach ($fields as $key => $field) { | ||||
if (!$field->shouldEnableForRole($role)) { | if (!$field->shouldEnableForRole($role)) { | ||||
unset($fields[$key]); | unset($fields[$key]); | ||||
} | } | ||||
} | } | ||||
foreach ($fields as $field) { | foreach ($fields as $field) { | ||||
Show All 20 Lines | public static function getObjectField( | ||||
return idx($fields, $field_key); | return idx($fields, $field_key); | ||||
} | } | ||||
/** | /** | ||||
* @task apps | * @task apps | ||||
*/ | */ | ||||
public static function buildFieldList($base_class, array $spec) { | public static function buildFieldList($base_class, array $spec, $object) { | ||||
$field_objects = id(new PhutilSymbolLoader()) | $field_objects = id(new PhutilSymbolLoader()) | ||||
->setAncestorClass($base_class) | ->setAncestorClass($base_class) | ||||
->loadObjects(); | ->loadObjects(); | ||||
$fields = array(); | $fields = array(); | ||||
$from_map = array(); | $from_map = array(); | ||||
foreach ($field_objects as $field_object) { | foreach ($field_objects as $field_object) { | ||||
$current_class = get_class($field_object); | $current_class = get_class($field_object); | ||||
foreach ($field_object->createFields() as $field) { | foreach ($field_object->createFields($object) as $field) { | ||||
$key = $field->getFieldKey(); | $key = $field->getFieldKey(); | ||||
if (isset($fields[$key])) { | if (isset($fields[$key])) { | ||||
$original_class = $from_map[$key]; | $original_class = $from_map[$key]; | ||||
throw new Exception( | throw new Exception( | ||||
"Both '{$original_class}' and '{$current_class}' define a custom ". | "Both '{$original_class}' and '{$current_class}' define a custom ". | ||||
"field with field key '{$key}'. Field keys must be unique."); | "field with field key '{$key}'. Field keys must be unique."); | ||||
} | } | ||||
$from_map[$key] = $current_class; | $from_map[$key] = $current_class; | ||||
▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | /* -( Core Properties and Field Identity )--------------------------------- */ | ||||
/** | /** | ||||
* Most field implementations are unique, in that one class corresponds to | * Most field implementations are unique, in that one class corresponds to | ||||
* one field. However, some field implementations are general and a single | * one field. However, some field implementations are general and a single | ||||
* implementation may drive several fields. | * implementation may drive several fields. | ||||
* | * | ||||
* For general implementations, the general field implementation can return | * For general implementations, the general field implementation can return | ||||
* multiple field instances here. | * multiple field instances here. | ||||
* | * | ||||
* @param object The object to create fields for. | |||||
* @return list<PhabricatorCustomField> List of fields. | * @return list<PhabricatorCustomField> List of fields. | ||||
* @task core | * @task core | ||||
*/ | */ | ||||
public function createFields() { | public function createFields($object) { | ||||
return array($this); | return array($this); | ||||
} | } | ||||
/** | /** | ||||
* You can return `false` here if the field should not be enabled for any | * You can return `false` here if the field should not be enabled for any | ||||
* role. For example, it might depend on something (like an application or | * role. For example, it might depend on something (like an application or | ||||
* library) which isn't installed, or might have some global configuration | * library) which isn't installed, or might have some global configuration | ||||
Show All 19 Lines | /* -( Core Properties and Field Identity )--------------------------------- */ | ||||
* Normally, you do not need to override this method. Instead, override the | * Normally, you do not need to override this method. Instead, override the | ||||
* methods specific to roles you want to enable. For example, implement | * methods specific to roles you want to enable. For example, implement | ||||
* @{method:shouldUseStorage()} to activate the `'storage'` role. | * @{method:shouldUseStorage()} to activate the `'storage'` role. | ||||
* | * | ||||
* @return bool True to enable the field for the given role. | * @return bool True to enable the field for the given role. | ||||
* @task core | * @task core | ||||
*/ | */ | ||||
public function shouldEnableForRole($role) { | public function shouldEnableForRole($role) { | ||||
if ($this->proxy) { | |||||
return $this->proxy->shouldEnableForRole($role); | // NOTE: All of these calls proxy individually, so we don't need to | ||||
} | // proxy this call as a whole. | ||||
switch ($role) { | switch ($role) { | ||||
case self::ROLE_APPLICATIONTRANSACTIONS: | case self::ROLE_APPLICATIONTRANSACTIONS: | ||||
return $this->shouldAppearInApplicationTransactions(); | return $this->shouldAppearInApplicationTransactions(); | ||||
case self::ROLE_APPLICATIONSEARCH: | case self::ROLE_APPLICATIONSEARCH: | ||||
return $this->shouldAppearInApplicationSearch(); | return $this->shouldAppearInApplicationSearch(); | ||||
case self::ROLE_STORAGE: | case self::ROLE_STORAGE: | ||||
return $this->shouldUseStorage(); | return $this->shouldUseStorage(); | ||||
▲ Show 20 Lines • Show All 962 Lines • Show Last 20 Lines |