Relations between case type and actor types done

This commit is contained in:
naomi 2018-06-12 17:16:40 +02:00
parent 8941cdfb49
commit e10096c1ca
3 changed files with 55 additions and 25 deletions

View File

@ -0,0 +1,42 @@
<?php
namespace Drupal\opencase;
/**
* Stuff to do with the relationship of case types to actor types, and activity types to case types
*
*/
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();
if ($base_field_override) {
$actor_types = $base_field_override->getSettings()['handler_settings']['target_bundles'];
// example of the $actor_types array: ['client' => 'client', 'volunteer' => 0]
foreach($actor_types as $machine_name => $value) {
if ($value) {
$allowedBundles[] = $machine_name;
}
}
}
return $allowedBundles; // 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'];
}
}
return $allowedChildBundles; // NB. this is an array of labels, indexed by machine name.
}
}

View File

@ -39,17 +39,8 @@ class EntityTypeRelationsWidget {
*/ */
public function populate(&$form) { public function populate(&$form) {
$case_type_machine_name = $form['id']['#default_value']; $case_type_machine_name = $form['id']['#default_value'];
$base_field_override = \Drupal\Core\Field\Entity\BaseFieldOverride::load("oc_case.$case_type_machine_name.actors_involved"); $allowedParentBundles = EntityTypeRelations::getAllowedParentBundles('oc_case', $case_type_machine_name);
if ($base_field_override) { $form['actor_types']['#default_value'] = $allowedParentBundles;
$form['actor_types']['#default_value'] = array();
$actor_types = $base_field_override->getSettings()['handler_settings']['target_bundles'];
// example of the $actor_types array: ['client' => 'client', 'volunteer' => 0]
foreach($actor_types as $machine_name => $value) {
if ($value) {
$form['actor_types']['#default_value'][] = $machine_name;
}
}
}
} }
/** /**

View File

@ -2,6 +2,7 @@
namespace Drupal\opencase\Plugin\Block; namespace Drupal\opencase\Plugin\Block;
use Drupal\opencase\EntityTypeRelations;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Link; use Drupal\Core\Link;
use Drupal\Core\Url; use Drupal\Core\Url;
@ -81,10 +82,13 @@ class ContextualMenu extends BlockBase {
private function caseListPage() { private function caseListPage() {
$actor_id = \Drupal::routeMatch()->getParameter('actor_id'); $actor_id = \Drupal::routeMatch()->getParameter('actor_id');
\Drupal::service('user.private_tempstore')->get('opencase')->set('actor_id', $actor_id); \Drupal::service('user.private_tempstore')->get('opencase')->set('actor_id', $actor_id);
$link = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($actor_id)->toLink()->toString(); $actor = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($actor_id);
$link = $actor->toLink()->toString();
$markup = $this->asNavLinks([$link]); $markup = $this->asNavLinks([$link]);
$current_path = \Drupal::service('path.current')->getPath(); $current_path = \Drupal::service('path.current')->getPath();
$markup .= $this->generateAddLinks('oc_case', "Add new case", ['actor_id' => $actor_id, 'destination' => $current_path]); $title = "Add new case";
$query = ['actor_id' => $actor_id, 'destination' => $current_path];
$markup .= $this->generateLinksForAddingNewCases($actor, $title, $query);
return $markup; return $markup;
} }
@ -180,22 +184,15 @@ class ContextualMenu extends BlockBase {
} }
/** /**
* Generates a set of links for adding different types of a base entity
*
* $baseEntityType the type of entity to generate the links for (it will generate one for each bundle of the base type)
* $title the title to be placed above the set of links)
* $query optionally append a query string to the links (key => value format
*
* returns html markup. * returns html markup.
*/ */
public static function generateAddLinks($baseEntityType, $title, $query = []) { private function generateLinksForAddingNewCases($actor, $title, $query = []) {
$actor_type = $actor->bundle();
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($baseEntityType); $allowedChildBundles = EntityTypeRelations::getAllowedChildBundles('oc_actor', $actor_type);
$title = t($title); $title = t($title);
$markup = "<h1>$title: </h1>"; $markup = "<h1>$title: </h1>";
foreach($bundles as $bundle_id => $bundle) { foreach($allowedChildBundles as $machine_name => $label) {
$label = t($bundle['label']); $url = \Drupal\Core\Url::fromRoute("entity.oc_case.add_form", ['oc_case_type' => $machine_name]);
$url = \Drupal\Core\Url::fromRoute("entity.$baseEntityType.add_form", [$baseEntityType . '_type' => $bundle_id]);
$url->setOption('query', $query); $url->setOption('query', $query);
$link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString(); $link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString();
$markup .= "<p>$link</p>"; $markup .= "<p>$link</p>";