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

Issue #3452475: In the "ipaddress_pgsql" formatter and widget display the IP...

Issue #3452475: In the "ipaddress_pgsql" formatter and widget display the IP address with netmask prefix by default
parent 7eb60ddd
No related branches found
No related tags found
No related merge requests found
......@@ -138,17 +138,17 @@ final class IpAddressPostgreSqlFormatter extends FormatterBase implements Contai
*/
protected function viewValue($ipAddressString) {
// If a user has not selected any functions in the formatter settings,
// then return the initial IP address value.
// then return the result of the 'text' function. We use the 'text'
// because it enables to display the IP address with the netmask prefix even
// if the IP address was entered without it.
if ($this->getSetting('function') === '') {
return [
'#theme' => 'field__ipaddress',
'#value' => $ipAddressString,
];
$function = 'text';
}
else {
// As a user has selected a function in the formatter settings, then we
// create a query to the database and get the values of these function.
$function = $this->getSetting('function');
}
// As a user has selected a function in the formatter settings, then we
// create a query to the database and get the values of these function.
$function = $this->getSetting('function');
// We don't use the $function in the placeholders to prevent single quotes.
switch ($function) {
case 'abbrev':
......
......@@ -4,9 +4,13 @@ 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 Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'IP Address PostgreSQL' widget.
......@@ -19,15 +23,60 @@ use Drupal\Core\Form\FormStateInterface;
* }
* )
*/
final class IpAddressPostgreSqlWidget extends WidgetBase {
final class IpAddressPostgreSqlWidget 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')
);
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state): array {
// Check if the IP address was entered with netmask prefix and if not then
// get the representation with prefix by calling the 'text' function
// provided by PostgreSQL.
$ipAddress = $items[$delta]->value;
if ($ipAddress) {
$parts = explode('/', $ipAddress);
// If the IP address was stored without netmask prefix.
if (count($parts) == 1) {
// Get the IP address with the prefix format like this:
$query = $this->connection->query("SELECT text(inet :ipAddressString)", [
':ipAddressString' => $ipAddress,
]);
$ipAddress = $query->fetchField();
}
}
$element['value'] = $element + [
'#type' => 'textfield',
'#default_value' => $items[$delta]->value ?? NULL,
'#default_value' => $ipAddress,
];
return $element;
......@@ -43,6 +92,7 @@ final class IpAddressPostgreSqlWidget extends WidgetBase {
foreach ($values as &$item) {
$item['value'] = trim($item['value']);
}
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