Removed obsolete code from opencase_defaults

This commit is contained in:
naomi 2021-12-04 06:19:54 +00:00
parent f617fc207e
commit 5f790c73c7
3 changed files with 0 additions and 344 deletions

View File

@ -28,31 +28,6 @@ function opencase_defaults_help($route_name, RouteMatchInterface $route_match) {
}
}
/**
* Implements hook_preprocess_page_title
*
* Modify the page title to include more information
*/
function opencase_defaults_preprocess_page_title(&$variables) {
$route_name = \Drupal::routeMatch()->getRouteName();
switch ($route_name) {
case 'entity.oc_case.canonical':
$case = \Drupal::routeMatch()->getParameter('oc_case');
$variables['title'] = $case->getName() . ": Case Details and Files";
break;
case 'view.cases.page_1':
$actor_id = \Drupal::routeMatch()->getParameter('actor_id');
$actor = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($actor_id);
$variables['title'] = $actor->getName() . ": Cases";
break;
case 'view.activities.page_1':
$case_id = \Drupal::routeMatch()->getParameter('case_id');
$case = \Drupal::entityTypeManager()->getStorage('oc_case')->load($case_id);
$variables['title'] = $case->getName() . ": Activities";
break;
}
}
/**
* Implements hook_theme().
*/
@ -64,33 +39,6 @@ function opencase_defaults_theme() {
];
}
/**
* Implements hook_block_access
*
* Forbids the opencase_contextual_menu block on pages where it has no content.
* (Without this, it was displaying an empty sidebar)
*/
function opencase_defaults_block_access(\Drupal\block\Entity\Block $block, $operation, \Drupal\Core\Session\AccountInterface $account) {
if ($operation == 'view' && $block->getPluginId() == 'opencase_contextual_menu') {
$route_name = \Drupal::routeMatch()->getRouteName();
$routes_where_it_should_be_shown = [
'entity.oc_actor.canonical',
'entity.oc_actor.edit_form',
'view.cases.page_1',
'entity.oc_case.canonical',
'entity.oc_case.edit_form',
'entity.oc_case.add_form',
'view.activities.page_1',
'entity.oc_activity.canonical',
'entity.oc_activity.edit_form',
'entity.oc_activity.add_form',
];
return AccessResult::forbiddenIf(!in_array($route_name, $routes_where_it_should_be_shown))->addCacheableDependency($block);
}
// No opinion.
return AccessResult::neutral();
}
function opencase_defaults_entity_base_field_info($entity_type) {
$fields = array();
@ -108,47 +56,9 @@ function opencase_defaults_entity_base_field_info($entity_type) {
'weight' => -6,
));
}
// Add Involved Parties field to cases
if ($entity_type->id() === 'oc_case') {
$fields['actors_involved'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Involved Parties'))
->setDescription(t('People involved in this case. To add one, start typing their name.'))
->setSetting('target_type', 'oc_actor')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setCardinality(-1)
->setDisplayOptions('form', [
'label' => 'above',
'type' => 'entity_reference_autocomplete',
'weight' => -2,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayOptions('view', [
'label' => 'above',
])
->setDefaultValueCallback('opencase_defaults_actors_involved_callback')
->setRequired(TRUE);
}
return $fields;
}
/**
* When creating a case, it sets the first involved party to the actor
* id from the URL, and the second to the author's linked actor
* (if it exists and is different)
*/
function opencase_defaults_actors_involved_callback() {
$author_linked_actor_id = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id())->get('field_linked_opencase_actor')->target_id;
$currently_viewed_actor_id = \Drupal::request()->query->get('actor_id');
return array_unique([$currently_viewed_actor_id, $author_linked_actor_id]);
}
/**
* Implements hook_form_ID_alter
*/

View File

@ -8,16 +8,6 @@ namespace Drupal\opencase;
*/
class EntityTypeRelations {
public static function getAllowedActorTypesForCaseType($case_type) {
$base_field_override = \Drupal\Core\Field\Entity\BaseFieldOverride::load("oc_case.$case_type.actors_involved");
$allowedActorTypes = array();
if ($base_field_override) {
$allowedActorTypes = $base_field_override->getSettings()['handler_settings']['target_bundles'];
}
return $allowedActorTypes; // // format: ['volunteer' => 0, 'client' => 'client']
}
public static function getAllowedActivityTypesForCaseType($case_type) {
$caseTypeConfig = \Drupal::entityTypeManager()->getStorage('oc_case_type')->load($case_type);
$allowedActivityTypes = $caseTypeConfig->get('allowedActivityTypes'); // format: ['application' => 'application', 'interview' => 0]

View File

@ -1,244 +0,0 @@
<?php
namespace Drupal\opencase\Plugin\Block;
use Drupal\opencase\EntityTypeRelations;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Link;
use Drupal\Core\Url;
/**
* Provides a 'ContextualMenu' block.
*
* Displays contextual links on certain pages.
* The block is forbidden by hook_block_access on other pages, so if more are added they need adding there too.
*
* @Block(
* id = "opencase_contextual_menu",
* admin_label = @Translation("OpenCase Contextual Menu"),
* )
*/
class ContextualMenu extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$route_name = \Drupal::routeMatch()->getRouteName();
switch ($route_name) {
case 'entity.oc_actor.canonical':
case 'entity.oc_actor.edit_form':
$markup = $this->actorPage();
break;
case 'view.cases.page_1':
$markup = $this->caseListPageForActor();
break;
case 'entity.oc_case.canonical':
case 'entity.oc_case.edit_form':
$markup = $this->casePage();
break;
case 'entity.oc_case.add_form':
$markup = $this->caseAddPage();
break;
case 'view.activities.page_1':
$markup = $this->activityListPage();
break;
case 'entity.oc_activity.canonical':
case 'entity.oc_activity.edit_form':
$markup = $this->activityPage();
break;
case 'entity.oc_activity.add_form':
$markup = $this->activityAddPage();
break;
}
$build = [];
$build['contextual_menu'] = [
'#markup' => "<div id='opencase_contextual_menu'>$markup</div",
'#cache' => ['max-age' => 0]
];
return $build;
}
/**
* Contextual menu for Actor page
* - Link to case list for that actor
*/
private function actorPage() {
$actor_id = \Drupal::routeMatch()->getParameter('oc_actor')->id();
$link = $this->getCaseListLink('oc_actor', $actor_id);
return $this->asNavLinks([$link]);
}
/**
* Contextual menu for Case list page
* - Link to actor whose case list this is
* - Links to add cases of various types
* - Store the actor id in the session, so that the user experiences
* a hierachy actor->case->activities which they can navigate
*/
private function caseListPageForActor() {
$actor_id = \Drupal::routeMatch()->getParameter('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->generateLinksForAddingNewCasesForActor($actor, $title, $query);
return $markup;
}
/**
* Contextual menu for Case page
* - Link either the case list for the actor stored in the session (because their case list page was previously loaded)
* or the home page
* - Link to Activity list for that case
*/
private function casePage() {
$case = \Drupal::routeMatch()->getParameter('oc_case');
$links = [$this->getCaseListLinkForParentEntity(), $this->getActivityListLink($case)];
return $this->asNavLinks($links);
}
/**
* Contextual menu for Add-New-Case page
* - 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() {
$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
* - Links to add activities of various types
*/
private function activityListPage() {
$case_id = \Drupal::routeMatch()->getParameter('case_id');
$case = \Drupal::entityTypeManager()->getStorage('oc_case')->load($case_id);
$url = $case->toUrl();
$link = Link::fromTextAndUrl(t($case->getName() .": Case Details and Files"), $url)->toString();
$markup = $this->asNavLinks([$link]);
$current_path = \Drupal::service('path.current')->getPath();
return $markup . $this->generateLinksForAddingNewActivities($case, "Add activity", ['case_id' => $case_id, 'destination' => $current_path]);
}
/**
* Contextual menu for Activity page
* - Link to the activity list for the case
*/
private function activityPage() {
$activity = \Drupal::routeMatch()->getParameter('oc_activity');
$case = $activity->oc_case->entity;
$link = $this->getActivityListLink($case);
return $this->asNavLinks([$link]);
}
/**
* Contextual menu for Add-New-Activity page
* - Link to the activity list for the case
*/
private function activityAddPage() {
$case_id = \Drupal::request()->query->get('case_id');
$case = \Drupal::entityTypeManager()->getStorage('oc_case')->load($case_id);
$link = $this->getActivityListLink($case);
return $this->asNavLinks([$link]);
}
/**
* Given an case entity, returns a link to the activity list
*/
private function getActivityListLink($case) {
$url = Url::fromRoute('view.activities.page_1', ['case_id' => $case->id()]);
return Link::fromTextAndUrl(t($case->getName() .": Activities"), $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>$markup</div>";
}
/**
* returns html markup.
*/
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
// Now we pick just the allowed ones and produced an array of labels keyed by machine name
$allowedCaseTypes = array();
foreach(array_keys($allCaseTypes) as $caseType) {
if (in_array($actor_type, EntityTypeRelations::getAllowedActorTypesForCaseType($caseType))) {
$allowedCaseTypes[$caseType] = $allCaseTypes[$caseType]['label'];
}
}
$title = t($title);
$markup = "<h1>$title: </h1>";
foreach($allowedCaseTypes as $machine_name => $label) {
$url = \Drupal\Core\Url::fromRoute("entity.oc_case.add_form", ['oc_case_type' => $machine_name]);
$url->setOption('query', $query);
$link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString();
$markup .= "<p>$link</p>";
}
return "<div class='opencase_add_links'>$markup</div>";
}
/**
* returns html markup.
*/
private function generateLinksForAddingNewActivities($case, $title, $query = []) {
$title = t($title);
$markup = "<h1>$title: </h1>";
$caseType = $case->bundle();
$allActivityTypes = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_activity');
// $allActivityTypes is array where the key is the machine name and the value is array containing label
// Now we pick just the allowed ones and produced an array of labels keyed by machine name
$allowedActivityTypes = EntityTypeRelations::getAllowedActivityTypesForCaseType($caseType);
foreach($allowedActivityTypes as $machine_name => $is_allowed) {
if ($is_allowed) {
$label = $allActivityTypes[$machine_name]['label'];
$url = \Drupal\Core\Url::fromRoute("entity.oc_activity.add_form", ['oc_activity_type' => $machine_name]);
$url->setOption('query', $query);
$link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString();
$markup .= "<p>$link</p>";
}
}
return "<div class='opencase_add_links'>$markup</div>";
}
}