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

Issue #3451882: Rename classes

parent 23453ace
No related branches found
No related tags found
No related merge requests found
<?php
declare(strict_types=1);
namespace Drupal\field_ipaddress_pgsql\Plugin\Field\FieldFormatter;
use Drupal\Core\Database\Connection;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'IP Address PostgreSQL cidr' formatter.
*
* @FieldFormatter(
* id = "ipaddress_cidr",
* label = @Translation("Default"),
* field_types = {
* "ipaddress_cidr",
* }
* )
*/
final class IpAddressCidrFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* Constructs the plugin instance.
*/
public function __construct(
$plugin_id,
$plugin_definition,
FieldDefinitionInterface $field_definition,
array $settings,
$label,
$view_mode,
array $third_party_settings,
private readonly Connection $connection,
) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $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['label'],
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('database')
);
}
/**
* {@inheritdoc}
*/
public static function defaultSettings(): array {
return [
'function' => '',
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state): array {
$form = parent::settingsForm($form, $form_state);
$form['link_to_functions'] = [
'#type' => 'link',
'#title' => $this->t('IP Address Function'),
'#url' => Url::fromUri('https://www.postgresql.org/docs/16/functions-net.html'),
'#attributes' => [
'target' => '_blank',
],
'#prefix' => '<div>',
'#sufix' => '</div>',
];
$options = [
'abbrev' => 'abbrev',
'broadcast' => 'broadcast',
'family' => 'family',
'host' => 'host',
'hostmask' => 'hostmask',
'masklen' => 'masklen',
'netmask' => 'netmask',
'network' => 'network',
'text' => 'text',
];
$form['function'] = [
'#type' => 'select',
'#options' => $options,
'#default_value' => $this->getSetting('function'),
'#empty_value' => '',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function settingsSummary(): array {
return [
$this->t('IP Address function to display additional information about the IP address.'),
];
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode): array {
$element = [];
foreach ($items as $delta => $item) {
$element[$delta] = $this->viewValue($item->value);
}
return $element;
}
/**
* Generate the output appropriate for one field item.
*
* @param string $ipAddressString
* The IP address in the "cidr" representation as a string.
*
* @return array
* The textual output generated as a render array.
*/
protected function viewValue($ipAddressString) {
// If a user has not selected any functions in the formatter settings,
// then return the initial IP address value.
if ($this->getSetting('function') === '') {
return [
'#theme' => 'field__ipaddress_pgsql',
'#value' => $ipAddressString,
];
}
// 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':
$select = "SELECT abbrev(cidr :ipAddressString)";
break;
case 'broadcast':
$select = "SELECT broadcast(cidr :ipAddressString)";
break;
case 'family':
$select = "SELECT family(cidr :ipAddressString)";
break;
case 'host':
$select = "SELECT host(cidr :ipAddressString)";
break;
case 'hostmask':
$select = "SELECT hostmask(cidr :ipAddressString)";
break;
case 'masklen':
$select = "SELECT masklen(cidr :ipAddressString)";
break;
case 'netmask':
$select = "SELECT netmask(cidr :ipAddressString)";
break;
case 'network':
$select = "SELECT network(cidr :ipAddressString)";
break;
case 'text':
$select = "SELECT text(cidr :ipAddressString)";
}
$query = $this->connection->query($select, [':ipAddressString' => $ipAddressString]);
$result = $query->fetchField();
return [
'#theme' => 'field__ipaddress_pgsql',
'#value' => $result,
];
}
}
......@@ -14,17 +14,17 @@ use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'IP Address PostgreSQL inet' formatter.
* Plugin implementation of the 'IP Address PostgreSQL' formatter.
*
* @FieldFormatter(
* id = "ipaddress_inet",
* id = "ipaddress_pgsql",
* label = @Translation("Default"),
* field_types = {
* "ipaddress_inet",
* "ipaddress_pgsql"
* }
* )
*/
final class IpAddressInetFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
final class IpAddressPostgreSqlFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* Constructs the plugin instance.
......@@ -124,7 +124,6 @@ final class IpAddressInetFormatter extends FormatterBase implements ContainerFac
foreach ($items as $delta => $item) {
$element[$delta] = $this->viewValue($item->value);
}
return $element;
}
......@@ -142,7 +141,7 @@ final class IpAddressInetFormatter extends FormatterBase implements ContainerFac
// then return the initial IP address value.
if ($this->getSetting('function') === '') {
return [
'#theme' => 'field__ipaddress_pgsql',
'#theme' => 'field__ipaddress',
'#value' => $ipAddressString,
];
}
......@@ -192,7 +191,7 @@ final class IpAddressInetFormatter extends FormatterBase implements ContainerFac
$result = $query->fetchField();
return [
'#theme' => 'field__ipaddress_pgsql',
'#theme' => 'field__ipaddress',
'#value' => $result,
];
......
<?php
declare(strict_types=1);
namespace Drupal\field_ipaddress_pgsql\Plugin\Validation\Constraint;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the cidr format validation constraint.
*/
final class CidrFormatConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* Constructs the object.
*/
public function __construct(
private readonly Connection $connection,
) {}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): self {
return new self(
$container->get('database'),
);
}
/**
* {@inheritdoc}
*/
public function validate(mixed $value, Constraint $constraint): void {
$value = trim($value->value);
try {
$this->connection->query('SELECT cidr(:ipAddressString)', [':ipAddressString' => $value]);
}
catch (\Exception $e) {
$this->context->addViolation($constraint->message);
}
}
}
......@@ -7,20 +7,20 @@ namespace Drupal\field_ipaddress_pgsql\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* Provides a inet format validation constraint.
* Provides a PostgreSQL format validation constraint.
*
* @Constraint(
* id = "InetFormat",
* label = @Translation("inet format validation", context = "Validation"),
* id = "PostgreSqlFormat",
* label = @Translation("PostgreSQL format validation", context = "Validation"),
* )
*/
final class InetFormatConstraint extends Constraint {
final class PostgreSqlFormatConstraint extends Constraint {
/**
* Message for when the value isn't in the proper format.
*
* @var string
*/
public string $message = 'The value is in the wrong format for the inet type. See <a href="https://www.postgresql.org/docs/16/datatype-net-types.html#DATATYPE-INET" target="_blank">the description of the inet type</a>.';
public string $message = 'Invalid IP address format.';
}
......@@ -11,9 +11,9 @@ use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the inet format validation constraint.
* Validates the PostgreSQL format validation constraint.
*/
final class InetFormatConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
final class PostgreSqlFormatConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* Constructs the object.
......
<?php
namespace Drupal\field_ipaddress_pgsql\Plugin\views\filter;
use Drupal\views\Plugin\views\filter\StringFilter;
/**
* A filter that uses the "IP Address Operators" provided by the PostgreSQL.
*
* @see https://www.postgresql.org/docs/16/functions-net.html#FUNCTIONS-NET
*
* @ingroup views_filter_handlers
*
* @ViewsFilter("ipaddress_cidr")
*/
class IpAddressCidrFilter extends StringFilter {
/**
* Get the operators.
*/
public function operators() {
$operators = [
'=' => [
'title' => $this->t('Is equal to?'),
'short' => $this->t('='),
'values' => 1,
],
'<<' => [
'title' => $this->t('Is subnet strictly contained by subnet?'),
'short' => $this->t('<<'),
'values' => 1,
],
'<<=' => [
'title' => $this->t('Is subnet contained by or equal to subnet?'),
'short' => $this->t('<<='),
'values' => 1,
],
'>>' => [
'title' => $this->t('Does subnet strictly contain subnet?'),
'short' => $this->t('>>'),
'values' => 1,
],
'>>=' => [
'title' => $this->t('Does subnet contain or equal subnet?'),
'short' => $this->t('>>='),
'values' => 1,
],
'&&' => [
'title' => $this->t('Does either subnet contain or equal the other?'),
'short' => $this->t('&&'),
'values' => 1,
],
];
return $operators;
}
/**
* Build strings from the operators() for 'select' options.
*/
public function operatorOptions($which = 'title') {
$options = [];
foreach ($this->operators() as $id => $info) {
$options[$id] = $info[$which];
}
return $options;
}
/**
* Add this filter to the query.
*
* Due to the nature of fapi, the value and the operator have an unintended
* level of indirection. You will find them in $this->operator
* and $this->value respectively.
*/
public function query() {
$this->ensureMyTable();
$field = "$this->tableAlias.$this->realField";
$operator = $this->operator;
$exp = "$field $operator cidr(:filterValue)";
$this->query->addWhereExpression($this->options['group'], $exp, [
':filterValue' => $this->value,
]);
}
}
......@@ -11,9 +11,9 @@ use Drupal\views\Plugin\views\filter\StringFilter;
*
* @ingroup views_filter_handlers
*
* @ViewsFilter("ipaddress_inet")
* @ViewsFilter("ipaddress_pgsql")
*/
class IpAddressInetFilter extends StringFilter {
class IpAddressPgSqlFilter extends StringFilter {
/**
* Get the operators.
......
<?php
namespace Drupal\field_ipaddress_pgsql\Plugin\views\sort;
use Drupal\views\Plugin\views\sort\SortPluginBase;
/**
* Sort handler for the "IP Address PostgreSQL cidr" field type.
*
* @ViewsSort("ipaddress_cidr")
*/
class IpAddressCidrSort extends SortPluginBase {
/**
* Called to add the sort to a query.
*/
public function query() {
$this->ensureMyTable();
$this->query->addOrderBy(NULL, "cidr($this->tableAlias.$this->realField)", $this->options['order'], $this->realField);
}
}
......@@ -7,9 +7,9 @@ use Drupal\views\Plugin\views\sort\SortPluginBase;
/**
* Sort handler for the "IP Address PostgreSQL inet" field type.
*
* @ViewsSort("ipaddress_inet")
* @ViewsSort("ipaddress_pgsql")
*/
class IpAddressInetSort extends SortPluginBase {
class IpAddressPgSqlSort extends SortPluginBase {
/**
* Called to add the sort to a query.
......
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