Virtual Field

Overview #

A Virtual Field is a computed field whose value is processed using Twig, similar to Layout processing.

Field Type Parameters #

  1. Layout – Defines the Twig expression to compute the field value. Example: {{ price * count }} where price and count are field names.
  2. 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.
  3. 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 }}


What are your feelings

Leave a Reply

Updated on February 14, 2025