Refactored case-type - client-type relationship

and corresponding part of widget
This commit is contained in:
naomi
2018-06-13 13:31:04 +02:00
parent 74b6f90c94
commit e601fde1ef
4 changed files with 37 additions and 95 deletions

View File

@ -9,34 +9,33 @@ namespace Drupal\opencase;
class EntityTypeRelations {
public static function getAllowedParentBundles($childEntityType, $childBundle) {
$field = $childEntityType == 'oc_case' ? 'actors_involved' : 'oc_case';
$base_field_override = \Drupal\Core\Field\Entity\BaseFieldOverride::load("$childEntityType.$childBundle.$field");
$allowedBundles = array();
public static function getAllowedActorTypesForCaseType($case_type) {
$base_field_override = \Drupal\Core\Field\Entity\BaseFieldOverride::load("oc_case.$case_type.actors_involved");
$allowedActorTypes = array();
if ($base_field_override) {
$targetBundleConfig = $base_field_override->getSettings()['handler_settings']['target_bundles'];
// example of $targetBudleConfig: ['client' => 'client', 'volunteer' => 0]
foreach($targetBundleConfig as $machine_name => $value) {
if ($value) {
$allowedBundles[] = $machine_name;
if ($targetBundleConfig) {
// example of $targetBundleConfig: ['client' => 'client', 'volunteer' => 0]
foreach($targetBundleConfig as $machine_name => $value) {
if ($value) {
$allowedActorTypes[] = $machine_name;
}
}
}
}
return $allowedBundles; // NB. this is an array of machine names only, indexed numerically.
}
}
return $allowedActorTypes; // NB. this is an array of machine names only, indexed numerically.
}
public static function getAllowedChildBundles($parentEntityType, $parentBundle) {
$childEntityType = $parentEntityType == 'oc_case' ? 'oc_activity' : 'oc_case';
// get all the child bundles
$childBundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($childEntityType);
// $childBundles is array where the key is the machine name and the value is array containing label
$allowedChildBundles = array();
foreach(array_keys($childBundles) as $childBundle) {
if (in_array($parentBundle, self::getAllowedParentBundles($childEntityType, $childBundle))) {
$allowedChildBundles[$childBundle] = $childBundles[$childBundle]['label'];
public static function getAllowedCaseTypesForActorType($actor_type) {
$allCaseTypes = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_case');
// $allCaseTypes is array where the key is the machine name and the value is array containing label
$allowedCaseTypes = array();
foreach(array_keys($allCaseTypes) as $caseType) {
if (in_array($actor_type, self::getAllowedActorTypesForCaseType($caseType))) {
$allowedCaseTypes[$caseType] = $allCaseTypes[$caseType]['label'];
}
}
return $allowedChildBundles; // NB. this is an array of labels, indexed by machine name.
return $allowedCaseTypes; // NB. this is an array of labels, indexed by machine name.
}
}