finished entity type relations

This commit is contained in:
naomi 2018-06-13 17:51:14 +02:00
parent fd1a0d5156
commit d46142ee2b
3 changed files with 30 additions and 37 deletions

View File

@ -13,29 +13,13 @@ class EntityTypeRelations {
$base_field_override = \Drupal\Core\Field\Entity\BaseFieldOverride::load("oc_case.$case_type.actors_involved"); $base_field_override = \Drupal\Core\Field\Entity\BaseFieldOverride::load("oc_case.$case_type.actors_involved");
$allowedActorTypes = array(); $allowedActorTypes = array();
if ($base_field_override) { if ($base_field_override) {
$targetBundleConfig = $base_field_override->getSettings()['handler_settings']['target_bundles']; $allowedActorTypes = $base_field_override->getSettings()['handler_settings']['target_bundles'];
if ($targetBundleConfig) {
// example of $targetBundleConfig: ['client' => 'client', 'volunteer' => 0]
foreach($targetBundleConfig as $machine_name => $value) {
if ($value) {
$allowedActorTypes[] = $machine_name;
}
}
}
} }
return $allowedActorTypes; // NB. this is an array of machine names only, indexed numerically. return $allowedActorTypes; // // format: ['volunteer' => 0, 'client' => 'client']
} }
public static function getAllowedCaseTypesForActorType($actor_type) { public static function getAllowedActivityTypesForCaseType($case_type) {
$allCaseTypes = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_case'); $caseTypeConfig = \Drupal::entityTypeManager()->getStorage('oc_case_type')->load($case_type);
// $allCaseTypes is array where the key is the machine name and the value is array containing label return $caseTypeConfig->get('allowedActivityTypes'); // format: ['application' => 'application', 'interview' => 0]
$allowedCaseTypes = array(); }
foreach(array_keys($allCaseTypes) as $caseType) {
if (in_array($actor_type, self::getAllowedActorTypesForCaseType($caseType))) {
$allowedCaseTypes[$caseType] = $allCaseTypes[$caseType]['label'];
}
}
return $allowedCaseTypes; // NB. this is an array of labels, indexed by machine name.
}
} }

View File

@ -47,12 +47,7 @@ class EntityTypeRelationsWidget {
public function populate(&$form) { public function populate(&$form) {
$case_type = $form['id']['#default_value']; $case_type = $form['id']['#default_value'];
$allowedActorTypes = EntityTypeRelations::getAllowedActorTypesForCaseType($case_type); $allowedActorTypes = EntityTypeRelations::getAllowedActorTypesForCaseType($case_type);
$form['allowed_actor_types']['#default_value'] = $allowedActorTypes; $form['allowed_activity_types']['#default_value'] = $allowedActivityTypes;
$caseTypeConfig = \Drupal::entityTypeManager()->getStorage('oc_case_type')->load($case_type);
$allowedActivityTypes = $caseTypeConfig->get('allowedActivityTypes');
if ($allowedActivityTypes) {
$form['allowed_activity_types']['#default_value'] = $allowedActivityTypes;
}
} }
/** /**

View File

@ -188,10 +188,18 @@ class ContextualMenu extends BlockBase {
*/ */
private function generateLinksForAddingNewCases($actor, $title, $query = []) { private function generateLinksForAddingNewCases($actor, $title, $query = []) {
$actor_type = $actor->bundle(); $actor_type = $actor->bundle();
$allowedChildBundles = EntityTypeRelations::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
// Now we pick just the allowed ones and produced an array of labels keyed by machine name
$allowedCaseTypes = array();
foreach(array_keys($allCaseTypes) as $caseType) {
if (in_array($actor_type, EntityTypeRelations::getAllowedActorTypesForCaseType($caseType))) {
$allowedCaseTypes[$caseType] = $allCaseTypes[$caseType]['label'];
}
}
$title = t($title); $title = t($title);
$markup = "<h1>$title: </h1>"; $markup = "<h1>$title: </h1>";
foreach($allowedChildBundles as $machine_name => $label) { foreach($allowedCaseTypes as $machine_name => $label) {
$url = \Drupal\Core\Url::fromRoute("entity.oc_case.add_form", ['oc_case_type' => $machine_name]); $url = \Drupal\Core\Url::fromRoute("entity.oc_case.add_form", ['oc_case_type' => $machine_name]);
$url->setOption('query', $query); $url->setOption('query', $query);
$link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString(); $link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString();
@ -204,15 +212,21 @@ class ContextualMenu extends BlockBase {
* returns html markup. * returns html markup.
*/ */
private function generateLinksForAddingNewActivities($case, $title, $query = []) { private function generateLinksForAddingNewActivities($case, $title, $query = []) {
$case_type = $case->bundle();
$allowedChildBundles = EntityTypeRelations::getAllowedChildBundles('oc_case', $case_type);
$title = t($title); $title = t($title);
$markup = "<h1>$title: </h1>"; $markup = "<h1>$title: </h1>";
foreach($allowedChildBundles as $machine_name => $label) { $caseType = $case->bundle();
$url = \Drupal\Core\Url::fromRoute("entity.oc_activity.add_form", ['oc_activity_type' => $machine_name]); $allActivityTypes = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_activity');
$url->setOption('query', $query); // $allActivityTypes is array where the key is the machine name and the value is array containing label
$link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString(); // Now we pick just the allowed ones and produced an array of labels keyed by machine name
$markup .= "<p>$link</p>"; $allowedActivityTypes = EntityTypeRelations::getAllowedActivityTypesForCaseType($caseType);
foreach($allowedActivityTypes as $machine_name => $is_allowed) {
if ($is_allowed) {
$label = $allActivityTypes[$machine_name]['label'];
$url = \Drupal\Core\Url::fromRoute("entity.oc_activity.add_form", ['oc_activity_type' => $machine_name]);
$url->setOption('query', $query);
$link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString();
$markup .= "<p>$link</p>";
}
} }
return "<div class='opencase_add_links'>$markup</div>"; return "<div class='opencase_add_links'>$markup</div>";
} }