diff --git a/opencase.module b/opencase.module index 60f0068..46e3e8a 100644 --- a/opencase.module +++ b/opencase.module @@ -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) { $widget = new EntityTypeRelationsWidget(); $widget->setup($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) $widget->populate($form, $base_field_override); + $widget->populate($form); } diff --git a/src/EntityTypeRelationsWidget.php b/src/EntityTypeRelationsWidget.php index db3c445..e0a86de 100644 --- a/src/EntityTypeRelationsWidget.php +++ b/src/EntityTypeRelationsWidget.php @@ -3,7 +3,7 @@ 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 { @@ -29,24 +29,28 @@ class EntityTypeRelationsWidget { } /** - * Takes a base_field_override configuration, - * extracts list of actor types that are allowed for the case type + * Finds out which case type is being edited, then sees if it already + * 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) * $base_field_override - the config entity */ - public function populate(&$form, $base_field_override) { - $form['actor_types']['#default_value'] = array(); - $actor_types = $base_field_override->getSettings()['handler_settings']['target_bundles']; - // example of the $actor_types array: ['client' => 'client', 'volunteer' => 0] - foreach($actor_types as $machine_name => $value) { - if ($value) { - $form['actor_types']['#default_value'][] = $machine_name; + 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(); + $actor_types = $base_field_override->getSettings()['handler_settings']['target_bundles']; + // example of the $actor_types array: ['client' => 'client', 'volunteer' => 0] + foreach($actor_types as $machine_name => $value) { + if ($value) { + $form['actor_types']['#default_value'][] = $machine_name; + } } } } - /** * Submit callback which takes the data from the actor types field and diff --git a/src/Plugin/Block/ContextualMenu.php b/src/Plugin/Block/ContextualMenu.php index 3b71a09..b3c64b6 100644 --- a/src/Plugin/Block/ContextualMenu.php +++ b/src/Plugin/Block/ContextualMenu.php @@ -5,7 +5,6 @@ namespace Drupal\opencase\Plugin\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Link; use Drupal\Core\Url; -use Drupal\opencase\Utils; /** * Provides a 'ContextualMenu' block. @@ -85,7 +84,7 @@ class ContextualMenu extends BlockBase { $link = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($actor_id)->toLink()->toString(); $markup = $this->asNavLinks([$link]); $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; } @@ -125,7 +124,7 @@ class ContextualMenu extends BlockBase { $link = Link::fromTextAndUrl(t($case->getName() .": Case Details and Files"), $url)->toString(); $markup = $this->asNavLinks([$link]); $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 ""; } + /** + * 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 = "

$title:

"; + 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 .= "

$link

"; + } + return ""; + } } diff --git a/src/Utils.php b/src/Utils.php deleted file mode 100644 index 511a471..0000000 --- a/src/Utils.php +++ /dev/null @@ -1,35 +0,0 @@ - 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 = "

$title:

"; - 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 .= "

$link

"; - } - return ""; - } -}