IP address fields
This module provides a field type for storing single IP addresses or IP ranges, supporting both IPv4 and IPv6. It allows for flexible input formats like CIDR notation and wildcards, and offers efficient storage and querying of IP data directly in the database for faster lookups.
Provides a field type for storing single IP addresses or IP ranges, in either IPv4 or IPv6. Values are stored as a [start, end] pair in indexed binary columns, so range-membership queries run natively in SQL against the field's index rather than in PHP.
Accepted input
A single text field accepts any of these forms and normalises them to an internal [start, end] pair:
- A single IP —
10.10.10.10or2001:db8::1 - A dash range —
10.10.10.0 - 10.10.12.255(whitespace ignored) - A partial IPv4 range (last octet only) —
10.10.10.10-20becomes10.10.10.10-10.10.10.20 - Wildcards, any number of asterisks —
10.10.*.*,2001:db8::*:* - CIDR notation —
10.10.10.0/24,2001:db8::/32
Invalid input is rejected inline by the widget, and as an HTTP 422 Unprocessable Entity response by the REST / JSON:API endpoints, with the offending input echoed back in the error message.
Per-field settings
- IP version — restrict to IPv4, IPv6, or allow both.
- Allow ranges — toggle whether the field accepts a range or only a single IP.
- Allowed range — optionally limit accepted values to a specific sub-range, configured independently per family.
Views integration
Field values are exposed to Views with native sort and filter handlers. The filter supports =, !=, <, <=, >, >=, BETWEEN, and NOT BETWEEN. Range-to-range comparisons are correct on both sides — e.g. "all entities whose stored range overlaps 10.0.0.0/8" is one filter operator, not a custom query.
Drupal 7 migration
The module ships a migrate field plugin that maps the D7 field_ipaddress field type onto the current storage. Old ip2long() BIGINT columns are converted to the current binary
representation; D7's convention of end = 0 meaning "single IP" is recognised and migrates cleanly.
For developers
A helper service, field_ipaddress.iptools, exposes the two query patterns the field is designed for without requiring callers to hand-roll the binary encoding:
$iptools = \Drupal::service('field_ipaddress.iptools'); // Entities whose stored range contains a single IP. $nids = $iptools->containsIp('node', 'field_ip', \Drupal::request()->getClientIp()); // Entities whose stored range overlaps a given range. $nids = $iptools->overlapsRange('node', 'field_ip', '10.0.0.0/24');
Both methods accept a string, an ip2long() integer, or a pre-parsed \Drupal\field_ipaddress\IpAddress object. Entity access checks are on by default.
Compatibility
Drupal 10, 11, and 12. On Drupal 10 / 11.0 / 11.1 the Views integration transparently falls back to the pre-11.2 procedural helper — no action required.