Skip to content

Commit

Permalink
Merge pull request #319 from wri/WRI-314--zoom-webform
Browse files Browse the repository at this point in the history
#314: Add new IP Address element, update hook.
  • Loading branch information
komejo authored Sep 16, 2024
2 parents 5f52091 + 61411d3 commit 14202d9
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
81 changes: 81 additions & 0 deletions modules/wri_zoom/src/Plugin/WebFormElement/IPAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Drupal\wri_zoom\Plugin\WebformElement;

use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformElementBase;
use Drupal\webform\WebformSubmissionInterface;

/**
* Provides an 'ip_address' webform element.
*
* @WebformElement(
* id = "ip_address",
* label = @Translation("IP Address"),
* description = @Translation("Provides a webform element for capturing IP Address."),
* category = @Translation("Custom"),
* )
*/
class IPAddress extends WebformElementBase {

/**
* {@inheritdoc}
*/
protected function defineDefaultProperties() {
return parent::defineDefaultProperties() + [
'ip_address' => '',
];
}

/**
* {@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;
}

}
76 changes: 76 additions & 0 deletions modules/wri_zoom/wri_zoom.install
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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);
}
}

0 comments on commit 14202d9

Please sign in to comment.