Added Utils class and refactored

This commit is contained in:
naomi 2018-04-30 16:49:44 +02:00
parent 3b7e8c9cbe
commit 59964e653e
2 changed files with 37 additions and 13 deletions

View File

@ -5,6 +5,7 @@ namespace Drupal\opencase\Plugin\Block;
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;
use Drupal\opencase\Utils;
/** /**
* Provides a 'ContextualMenu' block. * Provides a 'ContextualMenu' block.
@ -48,7 +49,7 @@ class ContextualMenu extends BlockBase {
$actor = \Drupal::routeMatch()->getParameter('oc_actor'); $actor = \Drupal::routeMatch()->getParameter('oc_actor');
$url = Url::fromRoute('view.cases.page_1', array('actor_id' => $actor->id())); $url = Url::fromRoute('view.cases.page_1', array('actor_id' => $actor->id()));
$link = Link::fromTextAndUrl(t("Case List"), $url)->toString(); $link = Link::fromTextAndUrl(t("Case List"), $url)->toString();
return "<div id='opencase_contextual_menu_nav'><p>$link</p></div>"; return "<div class='opencase_nav_links'><p>$link</p></div>";
} }
/** /**
@ -60,18 +61,8 @@ class ContextualMenu extends BlockBase {
$actor_id = \Drupal::routeMatch()->getParameter('actor_id'); $actor_id = \Drupal::routeMatch()->getParameter('actor_id');
$actor = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($actor_id); $actor = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($actor_id);
$link = $actor->toLink()->toString(); $link = $actor->toLink()->toString();
$markup = "<div id='opencase_contextual_menu_nav'><p>$link</p></div>"; $markup = "<div class='opencase_nav_links'><p>$link</p></div>";
$markup .= Utils::generateAddLinks('oc_case', ['actor_id' => $actor_id]);
$case_types = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_case');
$add_links = '';
foreach($case_types as $case_type_id => $type) {
$label = $type['label'];
$url = Url::fromRoute('entity.oc_case.add_form', ['oc_case_type' => $case_type_id]);
$url->setOption('query', ['actor_id' => $actor_id]);
$link = Link::fromTextAndUrl(t("Add a $label case"), $url)->toString();
$add_links .= "<p>$link</p>";
}
$markup .= "<div id='opencase_contextual_menu_add'>$add_links</div>";
return $markup; return $markup;
} }

33
src/Utils.php Normal file
View File

@ -0,0 +1,33 @@
<?php
namespace Drupal\opencase;
/**
* Shared functions for the opencase module
*
*/
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)
*
* returns html markup.
*/
public static function generateAddLinks($baseEntityType, $query = []) {
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($baseEntityType);
$markup = '';
foreach($bundles as $bundle_id => $bundle) {
$label = $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();
$markup .= "<p>$link</p>";
}
return "<div class='opencase_add_links'>$markup</div>";
}
}