Skip to content

Commit

Permalink
Fixing DAWA elements preview
Browse files Browse the repository at this point in the history
  • Loading branch information
stankut committed Feb 10, 2020
1 parent a00c0fb commit 81822bf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
8 changes: 2 additions & 6 deletions modules/os2forms_dawa/os2forms_dawa.routing.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
os2forms_dawa.element.autocomplete:
path: '/os2forms-dawa/{webform}/autocomplete/{element_type}'
path: '/os2forms-dawa/autocomplete/{element_type}'
defaults:
_controller: '\Drupal\os2forms_dawa\Controller\DawaElementController::autocomplete'
_format: json
options:
parameters:
webform:
type: 'entity:webform'
requirements:
_entity_access: 'webform.submission_page'
_permission: 'access content'
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ class DawaElementController extends ControllerBase {
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object containing the search string.
* @param \Drupal\webform\WebformInterface $webform
* A webform.
* @param string $element_type
* Type of the webform element.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* A JSON response containing the autocomplete suggestions.
*/
public function autocomplete(Request $request, WebformInterface $webform, $element_type) {
public function autocomplete(Request $request, $element_type) {
// Get autocomplete query.
$query = $request->query;
$q = $query->get('q') ?: '';
Expand Down
54 changes: 32 additions & 22 deletions modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,9 @@ class DawaElementAddressMatrikula extends WebformCompositeBase {
public static function getCompositeElements(array $element) {
$elements = [];
if ($element) {
$matrikula_wrapper_id = $element['#webform_id'] . '-matrikula-wrapper';

$elements['address'] = [
'#type' => 'os2forms_dawa_address',
'#title' => isset($element['#address_field_title']) ? $element['#address_field_title'] : t('Address'),
'#ajax' => [
'callback' => [DawaElementAddressMatrikula::class, 'matrikulaUpdateSelectOptions'],
'event' => 'change',
'wrapper' => $matrikula_wrapper_id,
'progress' => [
'type' => 'none',
],
],
'#remove_place_name' => isset($element['#remove_place_name']) ? $element['#remove_place_name'] : FALSE,
'#remove_code' => isset($element['#remove_code']) ? $element['#remove_code'] : FALSE,
'#limit_by_municipality' => isset($element['#limit_by_municipality']) ? $element['#limit_by_municipality'] : FALSE,
Expand All @@ -40,29 +30,49 @@ public static function getCompositeElements(array $element) {
$elements['matrikula'] = [
'#type' => 'select',
'#title' => isset($element['#matrikula_field_title']) ? $element['#matrikula_field_title'] : t('Matrikula'),
'#prefix' => '<div id="' . $matrikula_wrapper_id . '">',
'#suffix' => '</div>',
'#options' => [],
'#empty_value' => NULL,
'#validated' => TRUE,
'#attributes' => [
'disabled' => 'disabled',
],
'#description' => t('Options autofill is disabled during the element preview'),
];

if (isset($element['#value']) && !empty($element['#value']['address'])) {
$addressValue = $element['#value']['address'];
// If that is just element preview (no webform_id), then keep the
// element simple. Don't add AJAX behaviour.
if (isset($element['#webform_id'])) {
$matrikula_wrapper_id = $element['#webform_id'] . '-matrikula-wrapper';

$elements['address']['#ajax'] = [
'callback' => [DawaElementAddressMatrikula::class, 'matrikulaUpdateSelectOptions'],
'event' => 'change',
'wrapper' => $matrikula_wrapper_id,
'progress' => [
'type' => 'none',
],
];

$elements['matrikula'] += [
'#prefix' => '<div id="' . $matrikula_wrapper_id . '">',
'#suffix' => '</div>',
];
unset($elements['matrikula']['#description']);

if (isset($element['#value']) && !empty($element['#value']['address'])) {
$addressValue = $element['#value']['address'];

$matrikulaOptions = self::getMatrikulaOptions($addressValue, $element);
$matrikulaOptions = self::getMatrikulaOptions($addressValue, $element);

// Populating the element.
if (!empty($matrikulaOptions)) {
$elements['matrikula']['#options'] = $matrikulaOptions;
$matrikulaOptionKeys = array_keys($matrikulaOptions);
$elements['matrikula']['matrikula']['#value'] = reset($matrikulaOptionKeys);
// Populating the element.
if (!empty($matrikulaOptions)) {
$elements['matrikula']['#options'] = $matrikulaOptions;
$matrikulaOptionKeys = array_keys($matrikulaOptions);
$elements['matrikula']['matrikula']['#value'] = reset($matrikulaOptionKeys);

// Make element enabled.
unset($elements['matrikula']['#attributes']['disabled']);
// Make element enabled.
unset($elements['matrikula']['#attributes']['disabled']);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function prepare(array &$element, WebformSubmissionInterface $webform_sub

$element['#autocomplete_route_name'] = 'os2forms_dawa.element.autocomplete';
$element['#autocomplete_route_parameters'] = [
'webform' => $webform_submission->getWebform()->id(),
'element_type' => $element['#type'],
];
}
Expand Down

0 comments on commit 81822bf

Please sign in to comment.