Refactored global and contextual menu

This commit is contained in:
naomi 2018-05-03 13:35:35 +02:00
parent b89cfcc2ee
commit 0c9377f48e
3 changed files with 62 additions and 18 deletions

View File

@ -57,7 +57,7 @@ class ContextualMenu extends BlockBase {
private function actorPage() { private function actorPage() {
$actor = \Drupal::routeMatch()->getParameter('oc_actor'); $actor = \Drupal::routeMatch()->getParameter('oc_actor');
$link = $this->getCaseListLink($actor); $link = $this->getCaseListLink($actor);
return "<div class='opencase_nav_links'><p>$link</p></div>"; return $this->asNavLinks([$link]);
} }
/** /**
@ -67,7 +67,7 @@ class ContextualMenu extends BlockBase {
private function caseListPage() { private function caseListPage() {
$actor_id = \Drupal::routeMatch()->getParameter('actor_id'); $actor_id = \Drupal::routeMatch()->getParameter('actor_id');
$current_path = \Drupal::service('path.current')->getPath(); $current_path = \Drupal::service('path.current')->getPath();
$markup .= Utils::generateAddLinks('oc_case', ['actor_id' => $actor_id, 'destination' => $current_path]); $markup = Utils::generateAddLinks('oc_case', "Add new case", ['actor_id' => $actor_id, 'destination' => $current_path]);
return $markup; return $markup;
} }
@ -78,7 +78,7 @@ class ContextualMenu extends BlockBase {
*/ */
private function casePage() { private function casePage() {
$links = ''; $links = [];
// Ascertain if user has come from a case list and if so link back // Ascertain if user has come from a case list and if so link back
// This is fragle code, it needs doing better. // This is fragle code, it needs doing better.
@ -87,14 +87,12 @@ class ContextualMenu extends BlockBase {
$path_parts= explode('/', $parts[path]); $path_parts= explode('/', $parts[path]);
if ($path_parts[4] == 'case_list') { if ($path_parts[4] == 'case_list') {
$actor = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($path_parts[3]); $actor = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($path_parts[3]);
$link = $this->getCaseListLink($actor); $links[] = $this->getCaseListLink($actor);
$links .= "<p>$link</p>";
} }
// Now get the link to the activity list for the case.
$case = \Drupal::routeMatch()->getParameter('oc_case'); $case = \Drupal::routeMatch()->getParameter('oc_case');
$link = $this->getActivityListLink($case); $links[] = $this->getActivityListLink($case);
$links .= "<p>$link</p>"; return $this->asNavLinks($links);
return "<div class='opencase_nav_links'>$links</div>";
} }
/** /**
@ -103,9 +101,8 @@ class ContextualMenu extends BlockBase {
*/ */
private function activityListPage() { private function activityListPage() {
$case_id = \Drupal::routeMatch()->getParameter('case_id'); $case_id = \Drupal::routeMatch()->getParameter('case_id');
$current_path = \Drupal::service('path.current')->getPath(); $current_path = \Drupal::service('path.current')->getPath();
$markup .= Utils::generateAddLinks('oc_activity', ['case_id' => $case_id, 'destination' => $current_path]); return Utils::generateAddLinks('oc_activity', "Add activity", ['case_id' => $case_id, 'destination' => $current_path]);
return $markup;
} }
/** /**
@ -116,7 +113,7 @@ class ContextualMenu extends BlockBase {
$activity = \Drupal::routeMatch()->getParameter('oc_activity'); $activity = \Drupal::routeMatch()->getParameter('oc_activity');
$case = $activity->oc_case->entity; $case = $activity->oc_case->entity;
$link = $this->getActivityListLink($case); $link = $this->getActivityListLink($case);
return "<div class='opencase_nav_links'><p>$link</p></div>"; return $this->asNavLinks([$link]);
} }
@ -135,4 +132,16 @@ class ContextualMenu extends BlockBase {
$url = Url::fromRoute('view.cases.page_1', array('actor_id' => $actor->id())); $url = Url::fromRoute('view.cases.page_1', array('actor_id' => $actor->id()));
return Link::fromTextAndUrl(t("Case List for " . $actor->getName()), $url)->toString(); return Link::fromTextAndUrl(t("Case List for " . $actor->getName()), $url)->toString();
} }
/**
* Render given links as nav links div with heading
*/
private function asNavLinks(array $links) {
$markup = '';
foreach($links as $link) {
$markup .= "<p>$link</p>";
}
$title = t("Go to:");
return "<div class='opencase_nav_links'><h1>$title</h1><p>$link</p></div>";
}
} }

View File

@ -0,0 +1,33 @@
<?php
namespace Drupal\opencase\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\opencase\Utils;
/**
* Provides a 'GlobalMenu' block.
*
* @Block(
* id = "global_menu",
* admin_label = @Translation("OpenCase Global Menu"),
* )
*/
class GlobalMenu extends BlockBase {
/**
* - Links for adding various types of actor.
*/
public function build() {
$build = [];
$markup .= Utils::generateAddLinks('oc_actor', 'Add new');
$build['global_menu'] = [
'#markup' => "<div id='opencase_global_menu'>$markup</div",
'#cache' => ['max-age' => 0]
];
return $build;
}
}

View File

@ -13,19 +13,21 @@ class Utils {
* Generates a set of links for adding different types of a base entity * 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) * $baseEntityType the type of entity to generate the links for (it will generate one for each bundle of the base type)
* $query optionally append a query string to the links (key => value format) * $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, $query = []) { public static function generateAddLinks($baseEntityType, $title, $query = []) {
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($baseEntityType); $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($baseEntityType);
$markup = ''; $title = t($title);
$markup = "<h1>$title: </h1>";
foreach($bundles as $bundle_id => $bundle) { foreach($bundles as $bundle_id => $bundle) {
$label = $bundle['label']; $label = t($bundle['label']);
$url = \Drupal\Core\Url::fromRoute("entity.$baseEntityType.add_form", [$baseEntityType . '_type' => $bundle_id]); $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(t("Add $label"), $url)->toString(); $link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString();
$markup .= "<p>$link</p>"; $markup .= "<p>$link</p>";
} }
return "<div class='opencase_add_links'>$markup</div>"; return "<div class='opencase_add_links'>$markup</div>";