2021-02-01 16:49:49 +00:00
<?php
2021-11-12 09:11:00 +00:00
declare(strict_types = 1);
2021-02-01 16:49:49 +00:00
/**
* @file
* Contains opencase_cases.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
2022-04-03 18:27:43 +00:00
use PHPUnit\Framework\Constraint\IsEmpty;
2021-02-01 16:49:49 +00:00
2022-04-03 16:52:12 +00:00
use function PHPUnit\Framework\isEmpty;
2021-02-01 16:49:49 +00:00
/**
* Implements hook_help().
*/
function opencase_cases_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the opencase_cases module.
case 'help.page.opencase_cases':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
2021-12-04 07:26:17 +00:00
$output .= '<p>' . t('Provides case entities and a required field on activities linking them to a case, plus case fee and case provision entities.') . '</p>';
2021-02-01 16:49:49 +00:00
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function opencase_cases_theme() {
return [
'opencase_cases' => [
'render element' => 'children',
],
2021-02-08 17:27:18 +00:00
'oc_case' => [
'render element' => 'elements',
'file' => 'oc_case.page.inc',
'template' => 'oc_case',
2021-02-18 12:02:25 +00:00
],
2021-02-08 17:27:18 +00:00
'oc_case_content_add_list' => [
'render element' => 'content',
'variables' => ['content' => NULL],
'file' => 'oc_case.page.inc',
2021-11-11 08:39:18 +00:00
],
'oc_case_provision' => [
'render element' => 'elements',
'file' => 'oc_case_provision.page.inc',
'template' => 'oc_case_provision',
2021-11-11 14:07:13 +00:00
],
'oc_case_provision_content_add_list' => [
'render element' => 'content',
'variables' => ['content' => NULL],
'file' => 'oc_case_provision.page.inc',
],
'oc_case_fee' => [
'render element' => 'elements',
'file' => 'oc_case_fee.page.inc',
'template' => 'oc_case_fee',
],
'oc_case_fee_content_add_list' => [
'render element' => 'content',
'variables' => ['content' => NULL],
'file' => 'oc_case_fee.page.inc',
],
2021-11-11 08:39:18 +00:00
];
2021-02-01 16:49:49 +00:00
}
2021-12-30 13:43:19 +00:00
2021-11-12 09:11:00 +00:00
function opencase_cases_oc_case_fee_update(Drupal\opencase_cases\entity\OCCaseFee $case_fee): void {
2022-04-21 11:20:22 +00:00
$case = $case_fee->getCase();
$case->addToTotalFee($case_fee->amount->value - $case_fee->original->amount->value);
2021-11-12 09:11:00 +00:00
}
function opencase_cases_oc_case_fee_insert(Drupal\opencase_cases\entity\OCCaseFee $case_fee): void {
2022-04-21 11:11:56 +00:00
$case = $case_fee->getCase();
$case->addToTotalFee($case_fee->amount->value);
2021-11-12 09:11:00 +00:00
}
function opencase_cases_oc_case_fee_delete(Drupal\opencase_cases\entity\OCCaseFee $case_fee): void {
2022-04-21 11:15:00 +00:00
$case = $case_fee->getCase();
2022-04-21 11:20:22 +00:00
$case->addToTotalFee(0 - $case_fee->amount->value);
2021-11-12 09:11:00 +00:00
}
2021-02-01 16:49:49 +00:00
function opencase_cases_entity_base_field_info($entity_type) {
$fields = array();
// Add Case field to Activity
if ($entity_type->id() === 'oc_activity') {
2021-02-18 12:02:25 +00:00
$fields['oc_case'] = \Drupal\Core\Field\BaseFieldDefinition::create('entity_reference')
2021-02-01 16:49:49 +00:00
->setLabel(t('Case'))
->setDescription(t('The case this activity belongs to.'))
->setSetting('target_type', 'oc_case')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setCardinality(1)
2021-11-11 08:39:18 +00:00
->setDefaultValueCallback('opencase_cases_default_case_id')
2021-02-01 16:49:49 +00:00
->setDisplayOptions('view', [
'type' => 'string',
'weight' => -3,
])
->setRequired(TRUE);
}
return $fields;
}
2021-12-30 13:43:19 +00:00
2021-02-01 16:49:49 +00:00
/**
* When creating an activity, it sets the case id from the URL.
*/
2021-11-11 08:39:18 +00:00
function opencase_cases_default_case_id() {
2021-02-01 16:49:49 +00:00
return array(\Drupal::request()->query->get('case_id'));
}
2021-02-05 13:43:11 +00:00
/**
* Implementation of hook_form_alter()
* Changes what page is redirected to after deleting things
*/
function opencase_cases_form_alter(&$form, &$form_state, $form_id) {
if (preg_match('/oc_actor_.*_delete_form/', $form_id) or (preg_match('/oc_case_.*_delete_form/', $form_id)) or (preg_match('/oc_organisation_.*_delete_form/', $form_id))) {
$form['actions']['submit']['#submit'][] = '_opencase_cases_redirect_to_home';
$form['actions']['cancel']['#url'] = $form_state->getFormObject()->getEntity()->toUrl();
}
if (preg_match('/oc_activity_.*_delete_form/', $form_id)) {
$form['actions']['submit']['#submit'][] = '_opencase_cases_delete_activity_redirect';
$form['actions']['cancel']['#url'] = $form_state->getFormObject()->getEntity()->toUrl();
}
}
function _opencase_cases_redirect_to_home($form, &$form_state) {
$form_state->setRedirect('<front>');
}
2022-04-03 18:27:43 +00:00
// Upon deletion of an activity, go to the related case if there is one, otherwise to its target.
2021-02-05 13:43:11 +00:00
function _opencase_cases_delete_activity_redirect($form, &$form_state) {
2022-05-12 13:57:15 +00:00
$entity = $form_state->getFormObject()->getEntity();
// for some reason this fires when deleting an activity type, shouldn't do but.s
if ($entity instanceof \Drupal\opencase_entities\Entity\OCActivityType) return;
if (!IsEmpty($entity->oc_case)) {
$case_id = $entity->oc_case->target_id;
2022-04-03 18:27:43 +00:00
$form_state->setRedirect('entity.oc_case.canonical', ['oc_case' => $case_id]);
} else {
2022-05-12 13:57:15 +00:00
$target_actor_id = $entity->oc_target->target_id;
2022-04-03 18:27:43 +00:00
$form_state->setRedirect('entity.oc_actor.canonical', ['oc_actor' => $target_actor_id]);
}
2021-02-05 13:43:11 +00:00
}
2021-02-08 17:27:18 +00:00
/**
* Implements hook_theme_suggestions_HOOK().
*/
function opencase_cases_theme_suggestions_oc_case(array $variables) {
$suggestions = [];
$entity = $variables['elements']['#oc_case'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'oc_case__' . $sanitized_view_mode;
$suggestions[] = 'oc_case__' . $entity->bundle();
$suggestions[] = 'oc_case__' . $entity->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'oc_case__' . $entity->id();
$suggestions[] = 'oc_case__' . $entity->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
2021-11-11 08:39:18 +00:00
/**
* Implements hook_theme_suggestions_HOOK().
*/
function opencase_cases_theme_suggestions_oc_case_provision(array $variables) {
$suggestions = [];
$entity = $variables['elements']['#oc_case_provision'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'oc_case_provision__' . $sanitized_view_mode;
$suggestions[] = 'oc_case_provision__' . $entity->bundle();
$suggestions[] = 'oc_case_provision__' . $entity->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'oc_case_provision__' . $entity->id();
$suggestions[] = 'oc_case_provision__' . $entity->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
2021-11-11 14:07:13 +00:00
/**
* Implements hook_theme_suggestions_HOOK().
*/
function opencase_cases_theme_suggestions_oc_case_fee(array $variables) {
$suggestions = [];
$entity = $variables['elements']['#oc_case_fee'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'oc_case_fee__' . $sanitized_view_mode;
$suggestions[] = 'oc_case_fee__' . $entity->bundle();
$suggestions[] = 'oc_case_fee__' . $entity->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'oc_case_fee__' . $entity->id();
$suggestions[] = 'oc_case_fee__' . $entity->id() . '__' . $sanitized_view_mode;
return $suggestions;
}