diff --git a/src/Plugin/Block/ContextualMenu.php b/src/Plugin/Block/ContextualMenu.php index 792c726..4a90381 100644 --- a/src/Plugin/Block/ContextualMenu.php +++ b/src/Plugin/Block/ContextualMenu.php @@ -57,7 +57,7 @@ class ContextualMenu extends BlockBase { private function actorPage() { $actor = \Drupal::routeMatch()->getParameter('oc_actor'); $link = $this->getCaseListLink($actor); - return ""; + return $this->asNavLinks([$link]); } /** @@ -67,7 +67,7 @@ class ContextualMenu extends BlockBase { private function caseListPage() { $actor_id = \Drupal::routeMatch()->getParameter('actor_id'); $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; } @@ -78,7 +78,7 @@ class ContextualMenu extends BlockBase { */ private function casePage() { - $links = ''; + $links = []; // Ascertain if user has come from a case list and if so link back // This is fragle code, it needs doing better. @@ -87,14 +87,12 @@ class ContextualMenu extends BlockBase { $path_parts= explode('/', $parts[path]); if ($path_parts[4] == 'case_list') { $actor = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($path_parts[3]); - $link = $this->getCaseListLink($actor); - $links .= "

$link

"; + $links[] = $this->getCaseListLink($actor); } - + // Now get the link to the activity list for the case. $case = \Drupal::routeMatch()->getParameter('oc_case'); - $link = $this->getActivityListLink($case); - $links .= "

$link

"; - return ""; + $links[] = $this->getActivityListLink($case); + return $this->asNavLinks($links); } /** @@ -103,9 +101,8 @@ class ContextualMenu extends BlockBase { */ private function activityListPage() { $case_id = \Drupal::routeMatch()->getParameter('case_id'); - $current_path = \Drupal::service('path.current')->getPath(); - $markup .= Utils::generateAddLinks('oc_activity', ['case_id' => $case_id, 'destination' => $current_path]); - return $markup; + $current_path = \Drupal::service('path.current')->getPath(); + return Utils::generateAddLinks('oc_activity', "Add activity", ['case_id' => $case_id, 'destination' => $current_path]); } /** @@ -116,7 +113,7 @@ class ContextualMenu extends BlockBase { $activity = \Drupal::routeMatch()->getParameter('oc_activity'); $case = $activity->oc_case->entity; $link = $this->getActivityListLink($case); - return ""; + return $this->asNavLinks([$link]); } @@ -135,4 +132,16 @@ class ContextualMenu extends BlockBase { $url = Url::fromRoute('view.cases.page_1', array('actor_id' => $actor->id())); 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 .= "

$link

"; + } + $title = t("Go to:"); + return ""; + } } diff --git a/src/Plugin/Block/GlobalMenu.php b/src/Plugin/Block/GlobalMenu.php new file mode 100644 index 0000000..15e21f4 --- /dev/null +++ b/src/Plugin/Block/GlobalMenu.php @@ -0,0 +1,33 @@ + "
$markup ['max-age' => 0] + ]; + return $build; + } +} diff --git a/src/Utils.php b/src/Utils.php index a4a4911..511a471 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -13,19 +13,21 @@ class Utils { * 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) - * $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. */ - public static function generateAddLinks($baseEntityType, $query = []) { + public static function generateAddLinks($baseEntityType, $title, $query = []) { $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($baseEntityType); - $markup = ''; + $title = t($title); + $markup = "

$title:

"; 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->setOption('query', $query); - $link = \Drupal\Core\Link::fromTextAndUrl(t("Add $label"), $url)->toString(); + $link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString(); $markup .= "

$link

"; } return "";