Overview #
A Virtual Field is a computed field whose value is processed using Twig, similar to Layout processing.
Field Type Parameters #
- Layout – Defines the Twig expression to compute the field value. Example:
{{ price * count }}
whereprice
andcount
are field names. Value Storage
(optional) – Determines whether the computed value should be stored for future use in search or order by operations. Options:- virtual – Default. The value is computed on request and not stored.
- storedstring – Stores the computed value as a string.
- storedintegersigned – Stores the computed value as a signed integer.
- storedintegerunsigned – Stores the computed value as an unsigned integer.
Length
(optional) – Specifies the maximum number of characters for Stored String values.- Default: 255
- Min: 1
- Max: 4096
Examples: #
Concatenate First Name and Last Name
{{ (firstname ~ " " ~ lastname) }}
Multiply Price by Count
{{ price * count }}
Generate a Random Number (1000–9999)
{{ random(1000,9999) }}
Create a Unique Project Code
Concatenates "PRJ-"
with the first 10 characters of the MD5 hash of the current date:
PRJ-{{ (now|date('Y-m-d H:i:s.u')|md5)|slice(0,10) }}
Calculate the number of days between two dates
More about date formats: https://twig.symfony.com/doc/3.x/filters/date.html
{% set lastdate = date_field2("Y-m-d")|date('U') %}{% set firstdate = date_field1("Y-m-d")|date('U') %}{{ (lastdate - firstdate) // 86400 }}
Dividing by 86400
(seconds in a day) gives the number of days.
Basic usage #
Access the computed value:
{{ FieldName }}
Leave a Reply
You must be logged in to post a comment.