From aa491fea812dd1b97b557441c60eb764b3941fff Mon Sep 17 00:00:00 2001 From: komejo Date: Thu, 12 Sep 2024 17:37:40 -0700 Subject: [PATCH 1/3] #314: Add new IP Address element, update hook. --- .../src/Plugin/WebFormElement/IPAddress.php | 79 +++++++++++++++++++ modules/wri_zoom/wri_zoom.install | 76 ++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 modules/wri_zoom/src/Plugin/WebFormElement/IPAddress.php diff --git a/modules/wri_zoom/src/Plugin/WebFormElement/IPAddress.php b/modules/wri_zoom/src/Plugin/WebFormElement/IPAddress.php new file mode 100644 index 000000000..632d4cd39 --- /dev/null +++ b/modules/wri_zoom/src/Plugin/WebFormElement/IPAddress.php @@ -0,0 +1,79 @@ + '', + ]; + } + + /** + * {@inheritdoc} + */ + public function getValue(array $element, WebformSubmissionInterface $webform_submission, array $options = []) { + // Fetch the current request. + $request = \Drupal::requestStack()->getCurrentRequest(); + + // Check if the webform has a remote address. + if ($this->getWebform()->hasRemoteAddr()) { + // Prefer 'X-Forwarded-For' header, but fall back to 'remote_addr' if it's not present. + $ip_address = $request->headers->has('X-Forwarded-For') + ? trim(explode(',', $request->headers->get('X-Forwarded-For'))[0]) + : $request->getClientIp(); + + // Save the IP address value to the webform submission. + $submission_data = $webform_submission->getData(); + $submission_data[$element['#webform_key']] = $ip_address; + $webform_submission->setData($submission_data); + + return $ip_address; + } + + // Return empty string if no IP address is available. + return ''; + } + + /** + * {@inheritdoc} + */ + public function isInput(array $element) { + // This is an input element since it captures data. + return TRUE; + } + + /** + * {@inheritdoc} + */ + public function preview() { + return ['#markup' => $this->t('Displays the IP Address.')]; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + // Custom settings for the IP address element can be added here. + $form = parent::buildConfigurationForm($form, $form_state); + return $form; + } +} diff --git a/modules/wri_zoom/wri_zoom.install b/modules/wri_zoom/wri_zoom.install index 41618747a..81e289326 100644 --- a/modules/wri_zoom/wri_zoom.install +++ b/modules/wri_zoom/wri_zoom.install @@ -5,6 +5,8 @@ * Update hooks for wri_zoom module. */ +use Drupal\webform\Entity\Webform; + /** * Enable the modules better_passwords and rename_admin_paths and pulls config. */ @@ -13,3 +15,77 @@ function wri_zoom_install() { \Drupal::service('distro_helper.updates')->installConfig('rename_admin_paths.settings', 'wri_admin', 'post-install', TRUE); \Drupal::service('distro_helper.updates')->installConfig('webform.webform_options.translations', 'wri_zoom', 'post-install', TRUE); } + +/** + * Update webform fields to the new ip_address plugin. + */ +function wri_zoom_update_10301() { + // Load all webforms. + $webforms = Webform::loadMultiple(); + + // If no webforms exist, log a message and return. + if (empty($webforms)) { + $message = 'No webforms found.'; + \Drupal::logger('wri_zoom')->notice($message); + print($message . PHP_EOL); + return; + } + + // Keep track of webforms that were updated. + $updated_webforms = []; + + // Loop through each webform and check if it needs updating. + foreach ($webforms as $webform_id => $webform) { + $elements = $webform->getElementsDecoded(); + + // Track if the webform was updated. + $updated = FALSE; + + // Loop through all elements of the webform. + foreach ($elements as &$element) { + if ( + isset($element['#default_value']) && + $element['#default_value'] === '[webform_submission:ip-address]' + ) { + // Update this element to use the custom ip_address plugin. + $element['#type'] = 'ip_address'; + // Remove the default value. + unset($element['#default_value']); + + $updated = TRUE; + } + } + + // If the webform was updated, save it and log the changes. + if ($updated) { + // Encode the updated elements back to the webform. + $webform->setElements($elements); + $webform->save(); + + // Store the updated webform information. + $updated_webforms[] = [ + 'id' => $webform_id, + 'name' => $webform->label(), + ]; + + // Log the webform ID and name. + \Drupal::logger('wri_zoom')->notice( + 'Updated webform: @id - @name', [ + '@id' => $webform_id, + '@name' => $webform->label(), + ] + ); + } + } + + // Output results to the console. + if (!empty($updated_webforms)) { + print('The following webforms were updated:' . PHP_EOL); + foreach ($updated_webforms as $webform_info) { + print('Webform ID: ' . $webform_info['id'] . ' - Name: ' . $webform_info['name'] . PHP_EOL); + } + } + else { + print('No webforms needed updating.' . PHP_EOL); + } +} From 021d39974745495bb55e7fbf587c3e89f98ee797 Mon Sep 17 00:00:00 2001 From: komejo Date: Thu, 12 Sep 2024 17:53:15 -0700 Subject: [PATCH 2/3] PHPCS --- modules/wri_zoom/src/Plugin/WebFormElement/IPAddress.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/wri_zoom/src/Plugin/WebFormElement/IPAddress.php b/modules/wri_zoom/src/Plugin/WebFormElement/IPAddress.php index 632d4cd39..d0a45e69f 100644 --- a/modules/wri_zoom/src/Plugin/WebFormElement/IPAddress.php +++ b/modules/wri_zoom/src/Plugin/WebFormElement/IPAddress.php @@ -2,9 +2,9 @@ namespace Drupal\wri_zoom\Plugin\WebformElement; +use Drupal\Core\Form\FormStateInterface; use Drupal\webform\Plugin\WebformElementBase; use Drupal\webform\WebformSubmissionInterface; -use Drupal\Core\Form\FormStateInterface; /** * Provides an 'ip_address' webform element. @@ -36,7 +36,8 @@ public function getValue(array $element, WebformSubmissionInterface $webform_sub // Check if the webform has a remote address. if ($this->getWebform()->hasRemoteAddr()) { - // Prefer 'X-Forwarded-For' header, but fall back to 'remote_addr' if it's not present. + // Prefer 'X-Forwarded-For' header, but fall + // back to 'remote_addr' if it's not present. $ip_address = $request->headers->has('X-Forwarded-For') ? trim(explode(',', $request->headers->get('X-Forwarded-For'))[0]) : $request->getClientIp(); @@ -76,4 +77,5 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta $form = parent::buildConfigurationForm($form, $form_state); return $form; } + } From 61411d3fffc7924a219eff7b56aa0fa73a0bc878 Mon Sep 17 00:00:00 2001 From: komejo Date: Mon, 16 Sep 2024 16:19:00 -0700 Subject: [PATCH 3/3] #314: update default to use new ip_address plugin --- .../wri_zoom/config/install/webform.webform.zoom_default.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/wri_zoom/config/install/webform.webform.zoom_default.yml b/modules/wri_zoom/config/install/webform.webform.zoom_default.yml index d408b1c94..894959bdf 100644 --- a/modules/wri_zoom/config/install/webform.webform.zoom_default.yml +++ b/modules/wri_zoom/config/install/webform.webform.zoom_default.yml @@ -59,9 +59,8 @@ elements: |- '#title': 'Webinar Date' '#default_value': '[webform_submission:source-entity:field_date_time:format:default]' ip_addr: - '#type': hidden + '#type': ip_address '#title': 'Ip Addr' - '#default_value': '[webform_submission:ip-address]' preferred_language: '#type': hidden '#title': 'Browser Language'