2018-04-29 07:22:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Contains opencase.module.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use Drupal\Core\Routing\RouteMatchInterface;
|
2018-07-18 10:06:22 +00:00
|
|
|
use Drupal\Core\Access\AccessResult;
|
2018-06-12 13:38:30 +00:00
|
|
|
use Drupal\opencase\EntityTypeRelationsWidget;
|
2018-04-29 07:22:16 +00:00
|
|
|
|
2018-05-07 17:20:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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',
|
2018-05-08 11:21:12 +00:00
|
|
|
'entity.oc_actor.edit_form',
|
2018-05-07 17:20:09 +00:00
|
|
|
'view.cases.page_1',
|
|
|
|
'entity.oc_case.canonical',
|
2018-05-08 11:21:12 +00:00
|
|
|
'entity.oc_case.edit_form',
|
|
|
|
'entity.oc_case.add_form',
|
2018-05-07 17:20:09 +00:00
|
|
|
'view.activities.page_1',
|
2018-05-08 11:21:12 +00:00
|
|
|
'entity.oc_activity.canonical',
|
|
|
|
'entity.oc_activity.edit_form',
|
2018-07-19 18:01:59 +00:00
|
|
|
'entity.oc_activity.add_form',
|
|
|
|
'system.menu.opencase-add-actors',
|
|
|
|
'block.block.opencaseaddactors',
|
2018-05-07 17:20:09 +00:00
|
|
|
];
|
2018-07-18 10:06:22 +00:00
|
|
|
return AccessResult::forbiddenIf(!in_array($route_name, $routes_where_it_should_be_shown))->addCacheableDependency($block);
|
2018-05-07 17:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No opinion.
|
2018-07-18 10:06:22 +00:00
|
|
|
return AccessResult::neutral();
|
2018-05-07 17:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-07 16:54:08 +00:00
|
|
|
/**
|
|
|
|
* Implements hook_page_attachments
|
|
|
|
*
|
|
|
|
* Add the opencase library to every page
|
|
|
|
*/
|
|
|
|
function opencase_page_attachments(array &$page) {
|
|
|
|
$page['#attached']['library'][] = 'opencase/opencase-lib';
|
|
|
|
}
|
|
|
|
|
2018-05-07 16:31:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 12:34:55 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-29 07:22:16 +00:00
|
|
|
/**
|
|
|
|
* 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',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2018-05-28 14:21:56 +00:00
|
|
|
|
2018-06-12 13:38:30 +00:00
|
|
|
/**
|
|
|
|
* Implements hook_form_ID_alter
|
|
|
|
*/
|
2018-05-28 14:21:56 +00:00
|
|
|
function opencase_form_oc_case_type_add_form_alter(&$form, $form_state) {
|
2018-06-12 13:38:30 +00:00
|
|
|
$widget = new EntityTypeRelationsWidget();
|
2018-06-13 11:31:04 +00:00
|
|
|
$widget->setup($form);
|
2018-06-12 16:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-12 13:38:30 +00:00
|
|
|
/**
|
|
|
|
* Implements hook_form_ID_alter
|
|
|
|
*/
|
2018-05-28 14:21:56 +00:00
|
|
|
function opencase_form_oc_case_type_edit_form_alter(&$form, $form_state) {
|
2018-06-12 13:38:30 +00:00
|
|
|
$widget = new EntityTypeRelationsWidget();
|
2018-06-13 11:31:04 +00:00
|
|
|
$widget->setup($form);
|
|
|
|
$widget->populate($form);
|
2018-06-12 16:12:30 +00:00
|
|
|
}
|
2018-07-05 12:05:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_uninstall().
|
|
|
|
* Removes configs.
|
|
|
|
*/
|
|
|
|
function opencase_uninstall() {
|
|
|
|
$configs = [
|
|
|
|
'block.block.opencasecontextualmenu',
|
|
|
|
'block.block.opencase',
|
2018-09-06 15:27:14 +00:00
|
|
|
'block.block.opencaseaddactors',
|
|
|
|
'system.menu.opencase-add-actors',
|
2018-07-05 12:05:50 +00:00
|
|
|
'system.menu.openc',
|
|
|
|
'views.view.cases',
|
|
|
|
'block.block.opencaseglobal',
|
|
|
|
'system.menu.opencase-admin',
|
|
|
|
'views.view.activities'
|
|
|
|
];
|
|
|
|
foreach($configs as $config) {
|
|
|
|
Drupal::configFactory()->getEditable($config)->delete();
|
|
|
|
}
|
|
|
|
}
|
2018-07-09 18:38:48 +00:00
|
|
|
|
|
|
|
function opencase_views_pre_render($view) {
|
|
|
|
if (empty($view->result) && empty($view->exposed_input)) {
|
|
|
|
$view->exposed_widgets = NULL;
|
|
|
|
}
|
|
|
|
}
|
2018-07-18 10:06:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
function opencase_entity_field_access($operation, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, $account, $items = NULL) {
|
|
|
|
if ($field_definition->getName() == 'field_linked_opencase_actor'
|
|
|
|
&& $operation == 'edit'
|
|
|
|
&& !$account->hasPermission('administer users')) {
|
|
|
|
return AccessResult::forbidden();
|
|
|
|
}
|
|
|
|
return AccessResult::neutral();
|
|
|
|
}
|
2018-07-18 20:15:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of hook_form_alter()
|
2018-07-18 20:39:27 +00:00
|
|
|
* After deleting an actor or a case, redirect to the main page, which is a view of all cases.
|
2018-07-18 20:15:45 +00:00
|
|
|
*/
|
|
|
|
function opencase_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))) {
|
|
|
|
$form['actions']['submit']['#submit'][] = '_opencase_delete_case_redirect';
|
2018-07-19 13:51:52 +00:00
|
|
|
$form['actions']['cancel']['#url'] = $form_state->getFormObject()->getEntity()->toUrl();
|
2018-07-18 20:15:45 +00:00
|
|
|
}
|
2018-07-18 20:39:27 +00:00
|
|
|
if (preg_match('/oc_activity_.*_delete_form/', $form_id)) {
|
|
|
|
$form['actions']['submit']['#submit'][] = '_opencase_delete_activity_redirect';
|
2018-07-19 13:51:52 +00:00
|
|
|
$form['actions']['cancel']['#url'] = $form_state->getFormObject()->getEntity()->toUrl();
|
2018-07-18 20:39:27 +00:00
|
|
|
}
|
2018-07-18 20:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _opencase_delete_case_redirect($form, &$form_state) {
|
|
|
|
$form_state->setRedirect('view.cases.page_2'); // redirect to view of all cases
|
|
|
|
}
|
2018-07-18 20:39:27 +00:00
|
|
|
function _opencase_delete_activity_redirect($form, &$form_state) {
|
|
|
|
$case_id = $form_state->getFormObject()->getEntity()->oc_case->target_id;
|
|
|
|
$form_state->setRedirect('view.activities.page_1', ['case_id' => $case_id]); // redirect to the activity list
|
|
|
|
}
|
2018-07-19 13:51:52 +00:00
|
|
|
function _opencase_cancel_delete_activity_redirect($form, &$form_state) {
|
|
|
|
$entity = $form_state->getFormObject()->getEntity();
|
|
|
|
$form_state->setRedirect('entity.oc_activity.canonical', ['oc_activity' => $entity->id()]); // redirect to the activity
|
|
|
|
}
|