From d46142ee2b88e5203646126419d916a5f2b05bb9 Mon Sep 17 00:00:00 2001 From: naomi Date: Wed, 13 Jun 2018 17:51:14 +0200 Subject: [PATCH] finished entity type relations --- src/EntityTypeRelations.php | 28 ++++++------------------- src/EntityTypeRelationsWidget.php | 7 +------ src/Plugin/Block/ContextualMenu.php | 32 +++++++++++++++++++++-------- 3 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/EntityTypeRelations.php b/src/EntityTypeRelations.php index 0162130..c43f9c4 100644 --- a/src/EntityTypeRelations.php +++ b/src/EntityTypeRelations.php @@ -13,29 +13,13 @@ class EntityTypeRelations { $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']; - if ($targetBundleConfig) { - // example of $targetBundleConfig: ['client' => 'client', 'volunteer' => 0] - foreach($targetBundleConfig as $machine_name => $value) { - if ($value) { - $allowedActorTypes[] = $machine_name; - } - } - } + $allowedActorTypes = $base_field_override->getSettings()['handler_settings']['target_bundles']; } - 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) { - $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 $allowedCaseTypes; // NB. this is an array of labels, indexed by machine name. - - } + public static function getAllowedActivityTypesForCaseType($case_type) { + $caseTypeConfig = \Drupal::entityTypeManager()->getStorage('oc_case_type')->load($case_type); + return $caseTypeConfig->get('allowedActivityTypes'); // format: ['application' => 'application', 'interview' => 0] + } } diff --git a/src/EntityTypeRelationsWidget.php b/src/EntityTypeRelationsWidget.php index d82a5d1..242834a 100644 --- a/src/EntityTypeRelationsWidget.php +++ b/src/EntityTypeRelationsWidget.php @@ -47,12 +47,7 @@ class EntityTypeRelationsWidget { public function populate(&$form) { $case_type = $form['id']['#default_value']; $allowedActorTypes = EntityTypeRelations::getAllowedActorTypesForCaseType($case_type); - $form['allowed_actor_types']['#default_value'] = $allowedActorTypes; - $caseTypeConfig = \Drupal::entityTypeManager()->getStorage('oc_case_type')->load($case_type); - $allowedActivityTypes = $caseTypeConfig->get('allowedActivityTypes'); - if ($allowedActivityTypes) { - $form['allowed_activity_types']['#default_value'] = $allowedActivityTypes; - } + $form['allowed_activity_types']['#default_value'] = $allowedActivityTypes; } /** diff --git a/src/Plugin/Block/ContextualMenu.php b/src/Plugin/Block/ContextualMenu.php index 0d39c0d..897627b 100644 --- a/src/Plugin/Block/ContextualMenu.php +++ b/src/Plugin/Block/ContextualMenu.php @@ -188,10 +188,18 @@ class ContextualMenu extends BlockBase { */ private function generateLinksForAddingNewCases($actor, $title, $query = []) { $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); $markup = "

$title:

"; - 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->setOption('query', $query); $link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString(); @@ -204,15 +212,21 @@ class ContextualMenu extends BlockBase { * returns html markup. */ private function generateLinksForAddingNewActivities($case, $title, $query = []) { - $case_type = $case->bundle(); - $allowedChildBundles = EntityTypeRelations::getAllowedChildBundles('oc_case', $case_type); $title = t($title); $markup = "

$title:

"; - foreach($allowedChildBundles as $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 .= "

$link

"; + $caseType = $case->bundle(); + $allActivityTypes = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_activity'); + // $allActivityTypes 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 + $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 .= "

$link

"; + } } return ""; }