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' 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..d0a45e69f --- /dev/null +++ b/modules/wri_zoom/src/Plugin/WebFormElement/IPAddress.php @@ -0,0 +1,81 @@ + '', + ]; + } + + /** + * {@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); + } +}