Added block for adding cases to actors

This commit is contained in:
naomi 2022-07-07 12:50:02 +01:00
parent 03c826087a
commit 2b4b9696db
3 changed files with 80 additions and 4 deletions

View File

@ -36,7 +36,7 @@ class OCCaseForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
public function save(array $form, FormStateInterface $form_state): void {
$entity = $this->entity;
$entity->setNewRevision();
$entity->setRevisionCreationTime(REQUEST_TIME);
@ -57,7 +57,7 @@ class OCCaseForm extends ContentEntityForm {
]));
}
// If you have unpublished the entity and you can't see unpublished entities, redirect to a more informative message than just "Access Denied".
if (!$form_state->getValue('status')['value'] && !\Drupal::currentUser()->hasPermission('view unpublished case entities')) {
if (!$this->isPublished($form_state) && !\Drupal::currentUser()->hasPermission('view unpublished case entities')) {
\Drupal::messenger()->addMessage($this->t('The record for "%label" is now unpublished & hidden from you.', [
'%label' => $entity->label(),
]));
@ -67,4 +67,12 @@ class OCCaseForm extends ContentEntityForm {
}
}
private function isPublished(FormStateInterface $form_state): bool {
if (is_null($form_state->getValue('status'))) {
return false; // some entities have nothing set for the status in which case we want to treat them as published by default.
// TODO why don't they??
}
return $form_state->getValue('status')['value'];
}
}

View File

@ -177,9 +177,23 @@ function opencase_entity_field_access($operation, \Drupal\Core\Field\FieldDefini
}
/*
Implementation of hook_relevant_activity_type_ids which is a custom hook invoked in the AddActivity block.
Custom hook
*/
function opencase_relevant_activity_type_ids($actorTypeID) {
function opencase_relevant_case_type_ids(string $actorTypeID):array {
switch ($actorTypeID) {
case 'client':
return ['accommodation', 'asylum_support', 'employability', 'health', 'immigration', 'welfare_rights'];
case 'volunteer':
return ['volunteer_engagement'];
}
return [];
}
/*
Custom hook
*/
function opencase_relevant_activity_type_ids(string $actorTypeID):array {
switch ($actorTypeID) {
case 'volunteer':
return ['email', 'phone_call', 'supervision'];
@ -188,6 +202,7 @@ function opencase_relevant_activity_type_ids($actorTypeID) {
case 'staff_member':
return ['application', 'interview'];
}
return [];
}
/**

View File

@ -0,0 +1,53 @@
<?php declare(strict_types = 1);
namespace Drupal\opencase\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a Block for adding cases to an actor
*
* @Block(
* id = "add_case",
* admin_label = @Translation("Add Case"),
* category = @Translation("Opencase"),
* )
*/
class AddCase extends BlockBase {
/**
* {@inheritdoc}
*/
public function build():array {
$actor = \Drupal::routeMatch()->getParameter('oc_actor');
$target_id = $actor->id();
$actorType = $actor->bundle();
$markup = "<ul>";
$case_types = $this->getCaseTypesToDisplay($actorType);
foreach($case_types as $id => $info) {
$label = $info['label'];
$markup .= "foo";
#$markup .= "<li><a href='/opencase/oc_case/add/$id?target_id=$target_id&destination=/opencase/oc_actor/$target_id'>$label</a></li>";
}
$markup .= "</ul>";
return array('#markup' => $markup);
}
public function getCacheMaxAge():int {
return 0;
}
private function getCaseTypesToDisplay(string $actorType): array {
// Client modules will provide a list of what case types (bundles) are relevant for each actor type.
// Check if they are implemented, and if so display them.
$implemented_case_types = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_case');
$relevant_case_type_ids = \Drupal::moduleHandler()->invokeAll('relevant_case_type_ids', [$actorType]);
$case_types_to_display = [];
foreach ($relevant_case_type_ids as $type_id) {
if (array_key_exists($type_id, $implemented_case_types)) {
$case_types_to_display[$type_id] = $implemented_case_types[$type_id];
}
}
return $case_types_to_display;
}
}