Factored actor type widget stuff into a class

This commit is contained in:
naomi 2018-06-12 15:38:30 +02:00
parent 7448048f2d
commit 9715401dfc
2 changed files with 81 additions and 43 deletions

View File

@ -6,6 +6,7 @@
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\opencase\EntityTypeRelationsWidget;
/**
@ -112,54 +113,22 @@ function opencase_theme() {
];
}
/**
* Implements hook_form_ID_alter
*/
function opencase_form_oc_case_type_add_form_alter(&$form, $form_state) {
opencase_configure_actor_types($form);
$widget = new EntityTypeRelationsWidget();
$widget->setup($form);
}
/**
* Implements hook_form_ID_alter
*/
function opencase_form_oc_case_type_edit_form_alter(&$form, $form_state) {
opencase_configure_actor_types($form);
$form['actor_types']['#default_value'] = array();
$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) opencase_load_default_actor_types($form, $base_field_override);
if ($base_field_override) $widget->populate($form, $base_field_override);
}
// Take the base field override configuration,
// extract list of actor types that are allowed for the case type
// and put these into the default values for the checkboxes
function opencase_load_default_actor_types(&$form, $base_field_override) {
$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;
}
}
}
function opencase_configure_actor_types(&$form) {
$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'];
}
$form['actor_types'] = array(
'#title' => t('Actor types'),
'#description' => t('Types of people that can be involved in this kind of case.'),
'#type' => 'checkboxes',
'#options' => $options
);
$form['actions']['submit']['#submit'][] = 'opencase_configure_actor_types_submit';
}
function opencase_configure_actor_types_submit($form, $form_state) {
$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);
}
$base_field_override->setSetting('handler_settings', ['target_bundles' => $form_state->getValue('actor_types')]);
$base_field_override->save();
}

View File

@ -0,0 +1,69 @@
<?php
namespace Drupal\opencase;
/**
* Manages relations between case types and actor types, or activity types and case types
*
*/
class EntityTypeRelationsWidget {
/**
* Adds actor type checkboxes to the form, and adds the submit handler
*
* $form - the form to be modified (reference)
*/
public function setup(&$form) {
$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'];
}
$form['actor_types'] = array(
'#title' => t('Actor types'),
'#description' => t('Types of people that can be involved in this kind of case.'),
'#type' => 'checkboxes',
'#options' => $options
);
$form['actions']['submit']['#submit'][] = array($this, 'submit');
}
/**
* Takes a base_field_override configuration,
* extracts list of actor types that are allowed for the case type
* 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;
}
}
}
/**
* 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_state - the data in the form
*/
public function submit($form, $form_state) {
$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);
}
$base_field_override->setSetting('handler_settings', ['target_bundles' => $form_state->getValue('actor_types')]);
$base_field_override->save();
}
}