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");
$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]
}
}

View File

@ -47,13 +47,8 @@ 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;
}
}
/**
* Submit callback which takes the data from the actor types and activity types fields and

View File

@ -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 = "<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->setOption('query', $query);
$link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString();
@ -204,16 +212,22 @@ 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 = "<h1>$title: </h1>";
foreach($allowedChildBundles as $machine_name => $label) {
$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 .= "<p>$link</p>";
}
}
return "<div class='opencase_add_links'>$markup</div>";
}
}