contextual menu parameterised

This commit is contained in:
Naomi 2020-11-10 13:42:32 +00:00
parent 27cb58512e
commit 17f8ddba2e

View File

@ -32,7 +32,7 @@ class ContextualMenu extends BlockBase {
$markup = $this->actorPage();
break;
case 'view.cases.page_1':
$markup = $this->caseListPage();
$markup = $this->caseListPageForActor();
break;
case 'entity.oc_case.canonical':
case 'entity.oc_case.edit_form':
@ -67,8 +67,8 @@ class ContextualMenu extends BlockBase {
* - Link to case list for that actor
*/
private function actorPage() {
$actor = \Drupal::routeMatch()->getParameter('oc_actor');
$link = $this->getCaseListLink($actor);
$actor_id = \Drupal::routeMatch()->getParameter('oc_actor')->id();
$link = $this->getCaseListLink('oc_actor', $actor_id);
return $this->asNavLinks([$link]);
}
@ -79,16 +79,17 @@ class ContextualMenu extends BlockBase {
* - Store the actor id in the session, so that the user experiences
* a hierachy actor->case->activities which they can navigate
*/
private function caseListPage() {
private function caseListPageForActor() {
$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('parent_type', 'oc_actor');
\Drupal::service('user.private_tempstore')->get('opencase')->set('parent_id', $actor_id);
$actor = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($actor_id);
$link = $actor->toLink()->toString();
$markup = $this->asNavLinks([$link]);
$current_path = \Drupal::service('path.current')->getPath();
$title = "Add new case";
$query = ['actor_id' => $actor_id, 'destination' => $current_path];
$markup .= $this->generateLinksForAddingNewCases($actor, $title, $query);
$markup .= $this->generateLinksForAddingNewCasesForActor($actor, $title, $query);
return $markup;
}
@ -100,32 +101,40 @@ class ContextualMenu extends BlockBase {
*/
private function casePage() {
$case = \Drupal::routeMatch()->getParameter('oc_case');
$actor_id = \Drupal::service('user.private_tempstore')->get('opencase')->get('actor_id');
if ($actor_id) { // there is not always one stored.
$actor = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($actor_id);
if ($actor) { // actor may have been deleted.
$caseListLink = $this->getCaseListLink($actor);
} else {
$caseListLink = $this->getCaseListLinkAll();
}
} else {
$caseListLink = $this->getCaseListLinkAll();
}
$links = [$caseListLink, $this->getActivityListLink($case)];
$links = [$this->getCaseListLinkForParentEntity(), $this->getActivityListLink($case)];
return $this->asNavLinks($links);
}
/**
* Contextual menu for Add-New-Case page
* - Link to Case list for the actor that is stored in the session
* - Link to Case list for the parent entity (actor by default but plugins can change the type)
* that is stored in the session
*/
private function caseAddPage() {
$actor_id = \Drupal::service('user.private_tempstore')->get('opencase')->get('actor_id');
$actor = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($actor_id);
$link = $this->getCaseListLink($actor);
$link = $this->getCaseListLinkForParentEntity();
return $this->asNavLinks([$link]);
}
private function getCaseListLinkForParentEntity() {
$parent_type = \Drupal::service('user.private_tempstore')->get('opencase')->get('parent_type');
$parent_id = \Drupal::service('user.private_tempstore')->get('opencase')->get('parent_id');
return $this->getCaseListLink($parent_type, $parent_id);
}
private function getCaseListLink($entity_type, $entity_id) {
$url = "/opencase/$entity_type/$entity_id/case_list";
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($entity_id);
$link_text = $entity->getName(). ": Cases";
return "<a href=$url>$link_text</a>";
}
/**
* Returns a link to the list of all cases
*/
private function getCaseListLinkAll() {
$url = Url::fromRoute('view.cases.page_2');
return Link::fromTextAndUrl(t("All cases"), $url)->toString();
}
/**
* Contextual menu for Activity list page
* - Link to the case that the activity list is for
@ -173,22 +182,6 @@ class ContextualMenu extends BlockBase {
return Link::fromTextAndUrl(t($case->getName() .": Activities"), $url)->toString();
}
/**
* Given an actor entity, returns a link to their case list
*/
private function getCaseListLink($actor) {
$url = Url::fromRoute('view.cases.page_1', ['actor_id' => $actor->id()]);
return Link::fromTextAndUrl(t($actor->getName(). ": Cases"), $url)->toString();
}
/**
* Returns a link to the list of all cases
*/
private function getCaseListLinkAll() {
$url = Url::fromRoute('view.cases.page_2');
return Link::fromTextAndUrl(t("All cases"), $url)->toString();
}
/**
* Render given links as nav links div with heading
*/
@ -204,7 +197,7 @@ class ContextualMenu extends BlockBase {
/**
* returns html markup.
*/
private function generateLinksForAddingNewCases($actor, $title, $query = []) {
private function generateLinksForAddingNewCasesForActor($actor, $title, $query = []) {
$actor_type = $actor->bundle();
$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