2018-06-12 13:38:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Drupal\opencase;
|
|
|
|
|
|
|
|
/**
|
2018-06-12 14:11:28 +00:00
|
|
|
* Manages GUI for configuring relations between case types and actor types, or activity types and case types
|
2018-06-12 13:38:30 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
class EntityTypeRelationsWidget {
|
|
|
|
|
|
|
|
/**
|
2018-06-13 11:31:04 +00:00
|
|
|
* Adds actor type and activity type checkboxes to the case type form, and adds the submit handler
|
2018-06-12 13:38:30 +00:00
|
|
|
*
|
|
|
|
* $form - the form to be modified (reference)
|
|
|
|
*/
|
2018-06-13 11:31:04 +00:00
|
|
|
public function setup(&$form) {
|
2018-06-12 13:38:30 +00:00
|
|
|
$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'];
|
|
|
|
}
|
2018-06-13 11:31:04 +00:00
|
|
|
$form['allowed_actor_types'] = array(
|
2018-11-09 16:14:48 +00:00
|
|
|
'#title' => t('Allowed involved parties'),
|
2018-06-12 13:38:30 +00:00
|
|
|
'#description' => t('Types of people that can be involved in this kind of case.'),
|
|
|
|
'#type' => 'checkboxes',
|
|
|
|
'#options' => $options
|
|
|
|
);
|
2018-06-13 14:29:21 +00:00
|
|
|
$activity_types = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_activity');
|
|
|
|
$options = array();
|
|
|
|
foreach($activity_types as $machine_name => $info) {
|
|
|
|
$options[$machine_name] = $info['label'];
|
|
|
|
}
|
|
|
|
$form['allowed_activity_types'] = array(
|
|
|
|
'#title' => t('Activity types'),
|
|
|
|
'#description' => t('Types of activities that can be logged against this case.'),
|
|
|
|
'#type' => 'checkboxes',
|
|
|
|
'#options' => $options
|
|
|
|
);
|
2018-06-13 11:31:04 +00:00
|
|
|
$form['actions']['submit']['#submit'][] = array($this, 'submit');
|
2018-06-12 13:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-13 11:31:04 +00:00
|
|
|
* Populates the form with actor/activity types that are already set
|
2018-06-12 13:38:30 +00:00
|
|
|
*
|
|
|
|
* $form - the form to be modified (reference)
|
|
|
|
*/
|
2018-06-13 11:31:04 +00:00
|
|
|
public function populate(&$form) {
|
|
|
|
$case_type = $form['id']['#default_value'];
|
|
|
|
$allowedActorTypes = EntityTypeRelations::getAllowedActorTypesForCaseType($case_type);
|
2018-06-18 14:29:24 +00:00
|
|
|
$form['allowed_actor_types']['#default_value'] = $allowedActorTypes;
|
|
|
|
$allowedActivityTypes = EntityTypeRelations::getAllowedActivityTypesForCaseType($case_type);
|
2018-06-13 15:51:14 +00:00
|
|
|
$form['allowed_activity_types']['#default_value'] = $allowedActivityTypes;
|
2018-06-12 13:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-13 11:31:04 +00:00
|
|
|
* Submit callback which takes the data from the actor types and activity types fields and
|
|
|
|
* creates/edits the relevant config objects
|
2018-06-12 13:38:30 +00:00
|
|
|
*
|
|
|
|
* $form - the form that is being submitted
|
|
|
|
* $form_state - the data in the form
|
|
|
|
*/
|
2018-06-13 11:31:04 +00:00
|
|
|
public function submit($form, $form_state) {
|
2018-06-12 13:38:30 +00:00
|
|
|
$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);
|
|
|
|
}
|
2018-06-13 11:31:04 +00:00
|
|
|
$base_field_override->setSetting('handler_settings', ['target_bundles' => $form_state->getValue('allowed_actor_types')]);
|
2018-06-12 13:38:30 +00:00
|
|
|
$base_field_override->save();
|
2018-06-13 14:29:21 +00:00
|
|
|
$caseTypeConfig = \Drupal::entityTypeManager()->getStorage('oc_case_type')->load($case_type_machine_name);
|
|
|
|
$caseTypeConfig->set('allowedActivityTypes', $form_state->getValue('allowed_activity_types'));
|
|
|
|
$caseTypeConfig->save();
|
2018-06-12 13:38:30 +00:00
|
|
|
}
|
|
|
|
}
|