Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions js/scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(function($, Drupal) {
// Handle the `change` event which is triggered by webform states when an
// element is hidden or shown.
$('.os2forms-cvr-lookup-cvr-element').on('change', function(event) {
// Trigger AJAX event only when webform states is not triggering the
// `change` event and when the CVR element is actually visible.
if ('webform.states' !== extraParameters && $(this).is(':visible')) {
$(this).trigger('os2forms_cvr_lookup:change')
}
})
})(jQuery, Drupal);
6 changes: 6 additions & 0 deletions os2forms_cvr_lookup.libraries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
os2forms_cvr_lookup:
version: VERSION
js:
js/scripts.js: {}
dependencies:
- core/drupal.ajax
2 changes: 1 addition & 1 deletion src/Element/CvrElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getInfo() {
$element['#element_validate'] = [[$this, 'validate']];
$element['#ajax'] = [
'callback' => [$this, 'ajaxCallback'],
'event' => 'change',
'event' => 'os2forms_cvr_lookup:change',
'progress' => [
'type' => 'throbber',
'message' => $this->t('Looking up CVR'),
Expand Down
10 changes: 10 additions & 0 deletions src/Plugin/WebformElement/CvrElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\os2forms_cvr_lookup\Plugin\WebformElement;

use Drupal\webform\Plugin\WebformElement\TextField;
use Drupal\webform\WebformSubmissionInterface;

/**
* @WebformElement(
Expand All @@ -14,4 +15,13 @@
*/
class CvrElement extends TextField {

/**
* {@inheritdoc}
*/
public function prepare(array &$element, WebformSubmissionInterface $webform_submission = null)
{
$element['#attributes']['class'][] = 'os2forms-cvr-lookup-cvr-element';
$element['#attached']['library'][] = 'os2forms_cvr_lookup/os2forms_cvr_lookup';
parent::prepare($element, $webform_submission);
}
}