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

Issue #3455768: Implement the generateSampleValue() method so that "Devel...

Issue #3455768: Implement the generateSampleValue() method so that "Devel generate" can generate valid field values
parent 0640992c
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ use Drupal\Core\Field\FieldItemBase; ...@@ -9,6 +9,7 @@ use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition; use Drupal\Core\TypedData\DataDefinition;
use IPTools\Network;
/** /**
* Plugin implementation of the 'IP Address MySQL' field type. * Plugin implementation of the 'IP Address MySQL' field type.
...@@ -156,9 +157,53 @@ final class IpAddressMySqlField extends FieldItemBase { ...@@ -156,9 +157,53 @@ final class IpAddressMySqlField extends FieldItemBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @todo Inject the service when the following issue are resolved
* https://www.drupal.org/node/2053415
* https://www.drupal.org/project/drupal/issues/3294266
*/ */
public static function generateSampleValue(FieldDefinitionInterface $field_definition): array { public static function generateSampleValue(FieldDefinitionInterface $field_definition): array {
$values['value'] = '192.168.0.1/24'; $settings = $field_definition->getSettings();
// IPv4.
if ($settings['family'] == 4) {
// Number of bytes in IP address.
$bites = 4;
$prefixMaxLength = 32;
}
else {
// IPv6.
// Number of bytes in IP address.
$bites = 16;
$prefixMaxLength = 128;
}
// Generate an IP Address in the text format.
$ipAddres = inet_ntop(random_bytes($bites));
$prefixLength = mt_rand(1, $prefixMaxLength);
$networkMask = (string) Network::parse("$ipAddres/$prefixLength")->getNetmask();
$values['network_mask'] = inet_pton($networkMask);
if ($settings['cidr_format']) {
$cidr = Network::parse("$ipAddres/$prefixLength")->CIDR;
$ipAddres = explode('/', $cidr)[0];
}
$values['ip_address'] = inet_pton($ipAddres);
if ($settings['default_gateway_enabled']) {
/** @var \IPTools\Range $hosts */
$hosts = Network::parse("$ipAddres/$prefixLength")->hosts;
// For simplicity, we will randomly select either the first or last value
// from the range of valid values.
if (mt_rand(0, 1) == 0) {
$defaultGateway = (string) $hosts->getFirstIP();
}
else {
$defaultGateway = (string) $hosts->getLastIP();
}
$values['default_gateway'] = inet_pton($defaultGateway);
}
return $values; return $values;
} }
......
...@@ -9,6 +9,7 @@ use Drupal\Core\Field\FieldItemBase; ...@@ -9,6 +9,7 @@ use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition; use Drupal\Core\TypedData\DataDefinition;
use IPTools\Network;
/** /**
* Plugin implementation of the 'IP Address PostgreSQL' field type. * Plugin implementation of the 'IP Address PostgreSQL' field type.
...@@ -154,7 +155,44 @@ final class IpAddressPostgreSqlField extends FieldItemBase { ...@@ -154,7 +155,44 @@ final class IpAddressPostgreSqlField extends FieldItemBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function generateSampleValue(FieldDefinitionInterface $field_definition): array { public static function generateSampleValue(FieldDefinitionInterface $field_definition): array {
$values['value'] = '192.168.0.1/24'; $settings = $field_definition->getSettings();
// IPv4.
if ($settings['family'] == 4) {
// Number of bytes in IP address.
$bites = 4;
$prefixMaxLength = 32;
}
else {
// IPv6.
// Number of bytes in IP address.
$bites = 16;
$prefixMaxLength = 128;
}
$ipAddres = inet_ntop(random_bytes($bites));
$prefixLength = mt_rand(1, $prefixMaxLength);
$value = "$ipAddres/$prefixLength";
if ($settings['cidr_format']) {
$value = (string) Network::parse($value)->CIDR;
}
$values['value'] = $value;
if ($settings['default_gateway_enabled']) {
/** @var \IPTools\Range $hosts */
$hosts = Network::parse($value)->hosts;
// For simplicity, we will randomly select either the first or last value
// from the range of valid values.
if (mt_rand(0, 1) == 0) {
$defaultGateway = (string) $hosts->getFirstIP();
}
else {
$defaultGateway = (string) $hosts->getLastIP();
}
$values['default_gateway'] = $defaultGateway;
}
return $values; 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