Refactored case-type - client-type relationship
and corresponding part of widget
This commit is contained in:
parent
74b6f90c94
commit
e601fde1ef
@ -118,33 +118,15 @@ function opencase_theme() {
|
|||||||
*/
|
*/
|
||||||
function opencase_form_oc_case_type_add_form_alter(&$form, $form_state) {
|
function opencase_form_oc_case_type_add_form_alter(&$form, $form_state) {
|
||||||
$widget = new EntityTypeRelationsWidget();
|
$widget = new EntityTypeRelationsWidget();
|
||||||
$widget->setup_for_case_type($form);
|
$widget->setup($form);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements hook_form_ID_alter
|
|
||||||
*/
|
|
||||||
function opencase_form_oc_activity_type_add_form_alter(&$form, $form_state) {
|
|
||||||
$widget = new EntityTypeRelationsWidget();
|
|
||||||
$widget->setup_for_activity_type($form);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements hook_form_ID_alter
|
* Implements hook_form_ID_alter
|
||||||
*/
|
*/
|
||||||
function opencase_form_oc_case_type_edit_form_alter(&$form, $form_state) {
|
function opencase_form_oc_case_type_edit_form_alter(&$form, $form_state) {
|
||||||
$widget = new EntityTypeRelationsWidget();
|
$widget = new EntityTypeRelationsWidget();
|
||||||
$widget->setup_for_case_type($form);
|
$widget->setup($form);
|
||||||
$widget->populate($form, 'oc_case');
|
$widget->populate($form);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements hook_form_ID_alter
|
|
||||||
*/
|
|
||||||
function opencase_form_oc_activity_type_edit_form_alter(&$form, $form_state) {
|
|
||||||
$widget = new EntityTypeRelationsWidget();
|
|
||||||
$widget->setup_for_activity_type($form);
|
|
||||||
$widget->populate($form, 'oc_activity');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -9,34 +9,33 @@ namespace Drupal\opencase;
|
|||||||
class EntityTypeRelations {
|
class EntityTypeRelations {
|
||||||
|
|
||||||
|
|
||||||
public static function getAllowedParentBundles($childEntityType, $childBundle) {
|
public static function getAllowedActorTypesForCaseType($case_type) {
|
||||||
$field = $childEntityType == 'oc_case' ? 'actors_involved' : 'oc_case';
|
$base_field_override = \Drupal\Core\Field\Entity\BaseFieldOverride::load("oc_case.$case_type.actors_involved");
|
||||||
$base_field_override = \Drupal\Core\Field\Entity\BaseFieldOverride::load("$childEntityType.$childBundle.$field");
|
$allowedActorTypes = array();
|
||||||
$allowedBundles = array();
|
|
||||||
if ($base_field_override) {
|
if ($base_field_override) {
|
||||||
$targetBundleConfig = $base_field_override->getSettings()['handler_settings']['target_bundles'];
|
$targetBundleConfig = $base_field_override->getSettings()['handler_settings']['target_bundles'];
|
||||||
// example of $targetBudleConfig: ['client' => 'client', 'volunteer' => 0]
|
if ($targetBundleConfig) {
|
||||||
|
// example of $targetBundleConfig: ['client' => 'client', 'volunteer' => 0]
|
||||||
foreach($targetBundleConfig as $machine_name => $value) {
|
foreach($targetBundleConfig as $machine_name => $value) {
|
||||||
if ($value) {
|
if ($value) {
|
||||||
$allowedBundles[] = $machine_name;
|
$allowedActorTypes[] = $machine_name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $allowedBundles; // NB. this is an array of machine names only, indexed numerically.
|
}
|
||||||
|
return $allowedActorTypes; // NB. this is an array of machine names only, indexed numerically.
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getAllowedChildBundles($parentEntityType, $parentBundle) {
|
public static function getAllowedCaseTypesForActorType($actor_type) {
|
||||||
$childEntityType = $parentEntityType == 'oc_case' ? 'oc_activity' : 'oc_case';
|
$allCaseTypes = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_case');
|
||||||
// get all the child bundles
|
// $allCaseTypes is array where the key is the machine name and the value is array containing label
|
||||||
$childBundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($childEntityType);
|
$allowedCaseTypes = array();
|
||||||
// $childBundles is array where the key is the machine name and the value is array containing label
|
foreach(array_keys($allCaseTypes) as $caseType) {
|
||||||
$allowedChildBundles = array();
|
if (in_array($actor_type, self::getAllowedActorTypesForCaseType($caseType))) {
|
||||||
foreach(array_keys($childBundles) as $childBundle) {
|
$allowedCaseTypes[$caseType] = $allCaseTypes[$caseType]['label'];
|
||||||
if (in_array($parentBundle, self::getAllowedParentBundles($childEntityType, $childBundle))) {
|
|
||||||
$allowedChildBundles[$childBundle] = $childBundles[$childBundle]['label'];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $allowedChildBundles; // NB. this is an array of labels, indexed by machine name.
|
return $allowedCaseTypes; // NB. this is an array of labels, indexed by machine name.
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,64 +9,44 @@ namespace Drupal\opencase;
|
|||||||
class EntityTypeRelationsWidget {
|
class EntityTypeRelationsWidget {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds actor type checkboxes to the case type form, and adds the submit handler
|
* Adds actor type and activity type checkboxes to the case type form, and adds the submit handler
|
||||||
*
|
*
|
||||||
* $form - the form to be modified (reference)
|
* $form - the form to be modified (reference)
|
||||||
*/
|
*/
|
||||||
public function setup_for_case_type(&$form) {
|
public function setup(&$form) {
|
||||||
$actor_types = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_actor');
|
$actor_types = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_actor');
|
||||||
$options = array();
|
$options = array();
|
||||||
foreach($actor_types as $machine_name => $info) {
|
foreach($actor_types as $machine_name => $info) {
|
||||||
$options[$machine_name] = $info['label'];
|
$options[$machine_name] = $info['label'];
|
||||||
}
|
}
|
||||||
$form['allowed_parent_bundles'] = array(
|
$form['allowed_actor_types'] = array(
|
||||||
'#title' => t('Actor types'),
|
'#title' => t('Actor types'),
|
||||||
'#description' => t('Types of people that can be involved in this kind of case.'),
|
'#description' => t('Types of people that can be involved in this kind of case.'),
|
||||||
'#type' => 'checkboxes',
|
'#type' => 'checkboxes',
|
||||||
'#options' => $options
|
'#options' => $options
|
||||||
);
|
);
|
||||||
$form['actions']['submit']['#submit'][] = array($this, 'submit_case_type');
|
$form['actions']['submit']['#submit'][] = array($this, 'submit');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds case type checkboxes to the activity type form, and adds the submit handler
|
* Populates the form with actor/activity types that are already set
|
||||||
*
|
*
|
||||||
* $form - the form to be modified (reference)
|
* $form - the form to be modified (reference)
|
||||||
*/
|
*/
|
||||||
public function setup_for_activity_type(&$form) {
|
public function populate(&$form) {
|
||||||
$case_types = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_case');
|
$case_type = $form['id']['#default_value'];
|
||||||
$options = array();
|
$allowedActorTypes = EntityTypeRelations::getAllowedActorTypesForCaseType($case_type);
|
||||||
foreach($case_types as $machine_name => $info) {
|
$form['allowed_actor_types']['#default_value'] = $allowedActorTypes;
|
||||||
$options[$machine_name] = $info['label'];
|
|
||||||
}
|
|
||||||
$form['allowed_parent_bundles'] = array(
|
|
||||||
'#title' => t('Case types'),
|
|
||||||
'#description' => t('Types of cases in which this activity can appear.'),
|
|
||||||
'#type' => 'checkboxes',
|
|
||||||
'#options' => $options
|
|
||||||
);
|
|
||||||
$form['actions']['submit']['#submit'][] = array($this, 'submit_activity_type');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Populates with existing case/activity types if they exist
|
* Submit callback which takes the data from the actor types and activity types fields and
|
||||||
*
|
* creates/edits the relevant config objects
|
||||||
* $form - the form to be modified (reference)
|
|
||||||
*/
|
|
||||||
public function populate(&$form, $entityType) {
|
|
||||||
$bundle = $form['id']['#default_value'];
|
|
||||||
$allowedParentBundles = EntityTypeRelations::getAllowedParentBundles($entityType, $bundle);
|
|
||||||
$form['allowed_parent_bundles']['#default_value'] = $allowedParentBundles;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Submit callback which takes the data from the actor types field and
|
|
||||||
* creates/edits a base_field_override accordingly
|
|
||||||
*
|
*
|
||||||
* $form - the form that is being submitted
|
* $form - the form that is being submitted
|
||||||
* $form_state - the data in the form
|
* $form_state - the data in the form
|
||||||
*/
|
*/
|
||||||
public function submit_case_type($form, $form_state) {
|
public function submit($form, $form_state) {
|
||||||
$case_type_machine_name = $form['id']['#default_value'] ? $form['id']['#default_value'] : $form_state->getValue('id');
|
$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");
|
$base_field_override = \Drupal\Core\Field\Entity\BaseFieldOverride::load("oc_case.$case_type_machine_name.actors_involved");
|
||||||
if (!$base_field_override) {
|
if (!$base_field_override) {
|
||||||
@ -74,26 +54,7 @@ class EntityTypeRelationsWidget {
|
|||||||
$field_definition = $entity_fields['actors_involved'];
|
$field_definition = $entity_fields['actors_involved'];
|
||||||
$base_field_override = \Drupal\Core\field\Entity\BaseFieldOverride::createFromBaseFieldDefinition($field_definition, $case_type_machine_name);
|
$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('allowed_parent_bundles')]);
|
$base_field_override->setSetting('handler_settings', ['target_bundles' => $form_state->getValue('allowed_actor_types')]);
|
||||||
$base_field_override->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Submit callback which takes the data from the case types field and
|
|
||||||
* creates/edits a base_field_override accordingly
|
|
||||||
*
|
|
||||||
* $form - the form that is being submitted
|
|
||||||
* $form_state - the data in the form
|
|
||||||
*/
|
|
||||||
public function submit_activity_type($form, $form_state) {
|
|
||||||
$activity_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_activity.$activity_type_machine_name.oc_case");
|
|
||||||
if (!$base_field_override) {
|
|
||||||
$entity_fields = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions('oc_activity');
|
|
||||||
$field_definition = $entity_fields['oc_case'];
|
|
||||||
$base_field_override = \Drupal\Core\field\Entity\BaseFieldOverride::createFromBaseFieldDefinition($field_definition, $activity_type_machine_name);
|
|
||||||
}
|
|
||||||
$base_field_override->setSetting('handler_settings', ['target_bundles' => $form_state->getValue('allowed_parent_bundles')]);
|
|
||||||
$base_field_override->save();
|
$base_field_override->save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -188,7 +188,7 @@ class ContextualMenu extends BlockBase {
|
|||||||
*/
|
*/
|
||||||
private function generateLinksForAddingNewCases($actor, $title, $query = []) {
|
private function generateLinksForAddingNewCases($actor, $title, $query = []) {
|
||||||
$actor_type = $actor->bundle();
|
$actor_type = $actor->bundle();
|
||||||
$allowedChildBundles = EntityTypeRelations::getAllowedChildBundles('oc_actor', $actor_type);
|
$allowedChildBundles = EntityTypeRelations::getAllowedCaseTypesForActorType($actor_type);
|
||||||
$title = t($title);
|
$title = t($title);
|
||||||
$markup = "<h1>$title: </h1>";
|
$markup = "<h1>$title: </h1>";
|
||||||
foreach($allowedChildBundles as $machine_name => $label) {
|
foreach($allowedChildBundles as $machine_name => $label) {
|
||||||
|
Reference in New Issue
Block a user