Skip to content
Snippets Groups Projects
Commit 4f9fd073 authored by Andrey Vitushkin's avatar Andrey Vitushkin
Browse files

Issue #3446907: Make the "ipaddress_mysql" widget work with values stored as a...

Issue #3446907: Make the "ipaddress_mysql" widget work with values stored as a MySQL VARBINARY(16) type
parent a480d679
No related branches found
No related tags found
No related merge requests found
......@@ -4,14 +4,11 @@ declare(strict_types=1);
namespace Drupal\field_ipaddress_pgsql\Plugin\Field\FieldWidget;
use Drupal\Core\Database\Connection;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\field_ipaddress_pgsql\Plugin\Validation\Constraint\MySqlFormatConstraintValidator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use IPTools\Network;
/**
* Plugin implementation of the 'IP Address PostgreSQL' widget.
......@@ -24,43 +21,24 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* }
* )
*/
final class IpAddressMySqlWidget extends WidgetBase implements ContainerFactoryPluginInterface {
/**
* Constructs the plugin instance.
*/
public function __construct(
$plugin_id,
$plugin_definition,
FieldDefinitionInterface $field_definition,
array $settings,
array $third_party_settings,
private readonly Connection $connection,
) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['third_party_settings'],
$container->get('database')
);
}
final class IpAddressMySqlWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state): array {
if ($items[$delta]->ip_address && $items[$delta]->network_mask) {
$ipAddress = inet_ntop($items[$delta]->ip_address);
$networkMask = inet_ntop($items[$delta]->network_mask);
$defaultValue = Network::parse("$ipAddress $networkMask")->getCIDR();
}
else {
$defaultValue = NULL;
}
$element['value'] = $element + [
'#type' => 'textfield',
'#default_value' => $items[$delta]->value ?? NULL,
'#default_value' => $defaultValue,
'#element_validate' => [[$this, 'validateIpAddress']],
];
......@@ -100,7 +78,10 @@ final class IpAddressMySqlWidget extends WidgetBase implements ContainerFactoryP
}
/**
* Separate the IP address and network mask and convert them to int values.
* Separate the IP address and network mask and convert them to binary string.
*
* We convert them to binary string so that they can be stored in a column of
* the VARBINARY(16) type.
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
// This method is called despite errors in the validateIpAddress() method.
......@@ -109,9 +90,10 @@ final class IpAddressMySqlWidget extends WidgetBase implements ContainerFactoryP
if (!$form_state->getErrors()) {
foreach ($values as &$item) {
$value = trim($item['value']);
$parts = explode('/', $value);
$item['ip_address'] = ip2long($parts[0]);
$item['network_mask'] = intval($parts[1]);
$ipAddress = (string) Network::parse($value)->getIP();
$networkMask = (string) Network::parse($value)->getNetmask();
$item['ip_address'] = inet_pton($ipAddress);
$item['network_mask'] = inet_pton($networkMask);
}
}
return $values;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment