refactoring

This commit is contained in:
naomi 2018-06-12 16:11:28 +02:00
parent 9715401dfc
commit 8941cdfb49
4 changed files with 41 additions and 52 deletions

View File

@ -127,8 +127,6 @@ 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($form);
$case_type_machine_name = $form['id']['#default_value']; $widget->populate($form);
$base_field_override = \Drupal\Core\Field\Entity\BaseFieldOverride::load("oc_case.$case_type_machine_name.actors_involved");
if ($base_field_override) $widget->populate($form, $base_field_override);
} }

View File

@ -3,7 +3,7 @@
namespace Drupal\opencase; namespace Drupal\opencase;
/** /**
* Manages relations between case types and actor types, or activity types and case types * Manages GUI for configuring relations between case types and actor types, or activity types and case types
* *
*/ */
class EntityTypeRelationsWidget { class EntityTypeRelationsWidget {
@ -29,14 +29,18 @@ class EntityTypeRelationsWidget {
} }
/** /**
* Takes a base_field_override configuration, * Finds out which case type is being edited, then sees if it already
* extracts list of actor types that are allowed for the case type * 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 * 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 * $base_field_override - the config entity
*/ */
public function populate(&$form, $base_field_override) { public function populate(&$form) {
$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) {
$form['actor_types']['#default_value'] = array(); $form['actor_types']['#default_value'] = array();
$actor_types = $base_field_override->getSettings()['handler_settings']['target_bundles']; $actor_types = $base_field_override->getSettings()['handler_settings']['target_bundles'];
// example of the $actor_types array: ['client' => 'client', 'volunteer' => 0] // example of the $actor_types array: ['client' => 'client', 'volunteer' => 0]
@ -46,7 +50,7 @@ class EntityTypeRelationsWidget {
} }
} }
} }
}
/** /**
* Submit callback which takes the data from the actor types field and * Submit callback which takes the data from the actor types field and

View File

@ -5,7 +5,6 @@ namespace Drupal\opencase\Plugin\Block;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Link; use Drupal\Core\Link;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\opencase\Utils;
/** /**
* Provides a 'ContextualMenu' block. * Provides a 'ContextualMenu' block.
@ -85,7 +84,7 @@ class ContextualMenu extends BlockBase {
$link = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($actor_id)->toLink()->toString(); $link = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($actor_id)->toLink()->toString();
$markup = $this->asNavLinks([$link]); $markup = $this->asNavLinks([$link]);
$current_path = \Drupal::service('path.current')->getPath(); $current_path = \Drupal::service('path.current')->getPath();
$markup .= Utils::generateAddLinks('oc_case', "Add new case", ['actor_id' => $actor_id, 'destination' => $current_path]); $markup .= $this->generateAddLinks('oc_case', "Add new case", ['actor_id' => $actor_id, 'destination' => $current_path]);
return $markup; return $markup;
} }
@ -125,7 +124,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 . Utils::generateAddLinks('oc_activity', "Add activity", ['case_id' => $case_id, 'destination' => $current_path]); return $markup . $this->generateAddLinks('oc_activity', "Add activity", ['case_id' => $case_id, 'destination' => $current_path]);
} }
/** /**
@ -180,4 +179,27 @@ class ContextualMenu extends BlockBase {
return "<div class='opencase_nav_links'><h1>$title</h1>$markup</div>"; return "<div class='opencase_nav_links'><h1>$title</h1>$markup</div>";
} }
/**
* Generates a set of links for adding different types of a base entity
*
* $baseEntityType the type of entity to generate the links for (it will generate one for each bundle of the base type)
* $title the title to be placed above the set of links)
* $query optionally append a query string to the links (key => value format
*
* returns html markup.
*/
public static function generateAddLinks($baseEntityType, $title, $query = []) {
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($baseEntityType);
$title = t($title);
$markup = "<h1>$title: </h1>";
foreach($bundles as $bundle_id => $bundle) {
$label = t($bundle['label']);
$url = \Drupal\Core\Url::fromRoute("entity.$baseEntityType.add_form", [$baseEntityType . '_type' => $bundle_id]);
$url->setOption('query', $query);
$link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString();
$markup .= "<p>$link</p>";
}
return "<div class='opencase_add_links'>$markup</div>";
}
} }

View File

@ -1,35 +0,0 @@
<?php
namespace Drupal\opencase;
/**
* Shared functions for the opencase module
*
*/
class Utils {
/**
* Generates a set of links for adding different types of a base entity
*
* $baseEntityType the type of entity to generate the links for (it will generate one for each bundle of the base type)
* $title the title to be placed above the set of links)
* $query optionally append a query string to the links (key => value format
*
* returns html markup.
*/
public static function generateAddLinks($baseEntityType, $title, $query = []) {
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($baseEntityType);
$title = t($title);
$markup = "<h1>$title: </h1>";
foreach($bundles as $bundle_id => $bundle) {
$label = t($bundle['label']);
$url = \Drupal\Core\Url::fromRoute("entity.$baseEntityType.add_form", [$baseEntityType . '_type' => $bundle_id]);
$url->setOption('query', $query);
$link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString();
$markup .= "<p>$link</p>";
}
return "<div class='opencase_add_links'>$markup</div>";
}
}