Skip to content

Commit

Permalink
#314: Add new IP Address element, update hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
komejo committed Sep 13, 2024
1 parent 56fe0e2 commit aa491fe
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
79 changes: 79 additions & 0 deletions modules/wri_zoom/src/Plugin/WebFormElement/IPAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Drupal\wri_zoom\Plugin\WebformElement;

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

/**
* 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 aa491fe

Please sign in to comment.