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() {
$actor = \Drupal::routeMatch()->getParameter('oc_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() {
$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 .= "<p>$link</p>";
$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 .= "<p>$link</p>";
return "<div class='opencase_nav_links'>$links</div>";
$links[] = $this->getActivityListLink($case);
return $this->asNavLinks($links);
}
/**
@ -104,8 +102,7 @@ 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;
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 "<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()));
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
*
* $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 = "<h1>$title: </h1>";
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 .= "<p>$link</p>";
}
return "<div class='opencase_add_links'>$markup</div>";