48e5080a9c
Allowed actor types for a case are saved in a base field override for the "involved parties" field.
166 lines
5.5 KiB
Plaintext
166 lines
5.5 KiB
Plaintext
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Contains opencase.module.
|
|
*/
|
|
|
|
use Drupal\Core\Routing\RouteMatchInterface;
|
|
|
|
|
|
/**
|
|
* 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_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 \Drupal\Core\Access\AccessResult::forbiddenIf(!in_array($route_name, $routes_where_it_should_be_shown))
|
|
->addCacheableDependency($block);
|
|
}
|
|
|
|
// No opinion.
|
|
return \Drupal\Core\Access\AccessResult::neutral();
|
|
}
|
|
|
|
|
|
/**
|
|
* Implements hook_page_attachments
|
|
*
|
|
* Add the opencase library to every page
|
|
*/
|
|
function opencase_page_attachments(array &$page) {
|
|
$page['#attached']['library'][] = 'opencase/opencase-lib';
|
|
}
|
|
|
|
|
|
/**
|
|
* Implements hook_link_alter
|
|
*
|
|
* Makes menu items that are external links open in new tab.
|
|
*/
|
|
function opencase_link_alter(&$variables) {
|
|
if ($variables['url']->isExternal()) {
|
|
$variables['options']['attributes'] = ['target' => '_blank'];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_preprocess_page_title
|
|
*
|
|
* Modify the page title to include more information
|
|
*/
|
|
function opencase_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_help().
|
|
*/
|
|
function opencase_help($route_name, RouteMatchInterface $route_match) {
|
|
switch ($route_name) {
|
|
// Main module help for the opencase module.
|
|
case 'help.page.opencase':
|
|
$output = '';
|
|
$output .= '<h3>' . t('About') . '</h3>';
|
|
$output .= '<p>' . t('Simple Case Management') . '</p>';
|
|
return $output;
|
|
|
|
default:
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_theme().
|
|
*/
|
|
function opencase_theme() {
|
|
return [
|
|
'opencase' => [
|
|
'render element' => 'children',
|
|
],
|
|
];
|
|
}
|
|
|
|
function opencase_form_oc_case_type_add_form_alter(&$form, $form_state) {
|
|
opencase_configure_actor_types($form);
|
|
}
|
|
|
|
function opencase_form_oc_case_type_edit_form_alter(&$form, $form_state) {
|
|
opencase_configure_actor_types($form);
|
|
$form['actor_types']['#default_value'] = array();
|
|
$case_type_machine_name = $form['id']['#default_value'];
|
|
$base_field_override = \Drupal\Core\Field\Entity\BaseFieldOverride::load("oc_case.$case_type_machine_name.actors_involved");
|
|
if ($base_field_override) opencase_load_default_actor_types($form, $base_field_override);
|
|
}
|
|
|
|
// Take the base field override configuration,
|
|
// extract list of actor types that are allowed for the case type
|
|
// and put these into the default values for the checkboxes
|
|
function opencase_load_default_actor_types(&$form, $base_field_override) {
|
|
$actor_types = $base_field_override->getSettings()['handler_settings']['target_bundles'];
|
|
// example of the $actor_types array: ['client' => 'client', 'volunteer' => 0]
|
|
foreach($actor_types as $machine_name => $value) {
|
|
if ($value) {
|
|
$form['actor_types']['#default_value'][] = $machine_name;
|
|
}
|
|
}
|
|
}
|
|
|
|
function opencase_configure_actor_types(&$form) {
|
|
$actor_types = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_actor');
|
|
$options = array();
|
|
foreach($actor_types as $machine_name => $info) {
|
|
$options[$machine_name] = $info['label'];
|
|
}
|
|
$form['actor_types'] = array(
|
|
'#title' => t('Actor types'),
|
|
'#description' => t('Types of people that can be involved in this kind of case.'),
|
|
'#type' => 'checkboxes',
|
|
'#options' => $options
|
|
);
|
|
$form['actions']['submit']['#submit'][] = 'opencase_configure_actor_types_submit';
|
|
}
|
|
|
|
function opencase_configure_actor_types_submit($form, $form_state) {
|
|
$case_type_machine_name = $form['id']['#default_value'] ? $form['id']['#default_value'] : $form_state->getValue('id');
|
|
$base_field_override = \Drupal\Core\Field\Entity\BaseFieldOverride::load("oc_case.$case_type_machine_name.actors_involved");
|
|
if (!$base_field_override) {
|
|
$entity_fields = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions('oc_case');
|
|
$field_definition = $entity_fields['actors_involved'];
|
|
$base_field_override = \Drupal\Core\field\Entity\BaseFieldOverride::createFromBaseFieldDefinition($field_definition, $case_type_machine_name);
|
|
}
|
|
$base_field_override->setSetting('handler_settings', ['target_bundles' => $form_state->getValue('actor_types')]);
|
|
$base_field_override->save();
|
|
}
|