Entity relations working in all directions

This commit is contained in:
naomi 2018-06-12 18:12:30 +02:00
parent e10096c1ca
commit 74b6f90c94
5 changed files with 94 additions and 24 deletions

View File

@ -78,7 +78,7 @@ class OCActivity extends RevisionableContentEntityBase implements OCActivityInte
* When creating an activity, it sets the case id from the URL. * When creating an activity, it sets the case id from the URL.
*/ */
public static function defaultVal() { public static function defaultVal() {
return \Drupal::request()->query->get('case_id');; return array(\Drupal::request()->query->get('case_id'));
} }
/** /**

View File

@ -118,7 +118,16 @@ 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($form); $widget->setup_for_case_type($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);
} }
/** /**
@ -126,7 +135,16 @@ function opencase_form_oc_case_type_add_form_alter(&$form, $form_state) {
*/ */
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($form); $widget->setup_for_case_type($form);
$widget->populate($form); $widget->populate($form, 'oc_case');
}
/**
* 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');
} }

View File

@ -14,14 +14,14 @@ class EntityTypeRelations {
$base_field_override = \Drupal\Core\Field\Entity\BaseFieldOverride::load("$childEntityType.$childBundle.$field"); $base_field_override = \Drupal\Core\Field\Entity\BaseFieldOverride::load("$childEntityType.$childBundle.$field");
$allowedBundles = array(); $allowedBundles = array();
if ($base_field_override) { if ($base_field_override) {
$actor_types = $base_field_override->getSettings()['handler_settings']['target_bundles']; $targetBundleConfig = $base_field_override->getSettings()['handler_settings']['target_bundles'];
// example of the $actor_types array: ['client' => 'client', 'volunteer' => 0] // example of $targetBudleConfig: ['client' => 'client', 'volunteer' => 0]
foreach($actor_types as $machine_name => $value) { foreach($targetBundleConfig as $machine_name => $value) {
if ($value) { if ($value) {
$allowedBundles[] = $machine_name; $allowedBundles[] = $machine_name;
} }
} }
} }
return $allowedBundles; // NB. this is an array of machine names only, indexed numerically. return $allowedBundles; // NB. this is an array of machine names only, indexed numerically.
} }

View File

@ -9,38 +9,54 @@ namespace Drupal\opencase;
class EntityTypeRelationsWidget { class EntityTypeRelationsWidget {
/** /**
* Adds actor type checkboxes to the form, and adds the submit handler * Adds actor 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(&$form) { public function setup_for_case_type(&$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['actor_types'] = array( $form['allowed_parent_bundles'] = 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'); $form['actions']['submit']['#submit'][] = array($this, 'submit_case_type');
} }
/** /**
* Finds out which case type is being edited, then sees if it already * Adds case type checkboxes to the activity type form, and adds the submit handler
* has its allowed actor types stored in a base_field_override; if so,
* extracts list of actor types
* and put these into the default values for the checkboxes
* *
* $form - the form to be modified (reference) * $form - the form to be modified (reference)
* $base_field_override - the config entity
*/ */
public function populate(&$form) { public function setup_for_activity_type(&$form) {
$case_type_machine_name = $form['id']['#default_value']; $case_types = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_case');
$allowedParentBundles = EntityTypeRelations::getAllowedParentBundles('oc_case', $case_type_machine_name); $options = array();
$form['actor_types']['#default_value'] = $allowedParentBundles; foreach($case_types as $machine_name => $info) {
$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
*
* $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;
} }
/** /**
@ -50,7 +66,7 @@ class EntityTypeRelationsWidget {
* $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($form, $form_state) { public function submit_case_type($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) {
@ -58,7 +74,26 @@ 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('actor_types')]); $base_field_override->setSetting('handler_settings', ['target_bundles' => $form_state->getValue('allowed_parent_bundles')]);
$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();
} }
} }

View File

@ -128,7 +128,7 @@ class ContextualMenu extends BlockBase {
$link = Link::fromTextAndUrl(t($case->getName() .": Case Details and Files"), $url)->toString(); $link = Link::fromTextAndUrl(t($case->getName() .": Case Details and Files"), $url)->toString();
$markup = $this->asNavLinks([$link]); $markup = $this->asNavLinks([$link]);
$current_path = \Drupal::service('path.current')->getPath(); $current_path = \Drupal::service('path.current')->getPath();
return $markup . $this->generateAddLinks('oc_activity', "Add activity", ['case_id' => $case_id, 'destination' => $current_path]); return $markup . $this->generateLinksForAddingNewActivities($case, "Add activity", ['case_id' => $case_id, 'destination' => $current_path]);
} }
/** /**
@ -199,4 +199,21 @@ class ContextualMenu extends BlockBase {
} }
return "<div class='opencase_add_links'>$markup</div>"; return "<div class='opencase_add_links'>$markup</div>";
} }
/**
* returns html markup.
*/
private function generateLinksForAddingNewActivities($case, $title, $query = []) {
$case_type = $case->bundle();
$allowedChildBundles = EntityTypeRelations::getAllowedChildBundles('oc_case', $case_type);
$title = t($title);
$markup = "<h1>$title: </h1>";
foreach($allowedChildBundles as $machine_name => $label) {
$url = \Drupal\Core\Url::fromRoute("entity.oc_activity.add_form", ['oc_activity_type' => $machine_name]);
$url->setOption('query', $query);
$link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString();
$markup .= "<p>$link</p>";
}
return "<div class='opencase_add_links'>$markup</div>";
}
} }