Dynamic Rows offer the ability to add rows to multiple pages using rule based relations (instead of adding a row to each page manually). These rules are based on common properties between those pages. An example of such property would be the category. Let's take a deeper look.
Note
The Dynamic Row functionality is available since Graphcommerce 7.0.0
Let's say we want to inject a Dynamic Row which displays a discount banner for all socks with animal prints with a price lower than €7.50. We want to place it after the product specs:
discount-banner-row
discount-banner
.
AFTER
product-specs
Text
and Number
components nested in
an AND
component.The result is as follows:
The Dynamic Row should now be injected on all the pages whose properties match the conditions.
The magic of the Dynamic Row functionality, is all in the HygrahPageContent()
function. This function takes 4 arguments.
To inject a dynamic row, go to Hygraph and add a Dynamic Row entry. You will see the following fields:
Placement has a few key behaviours.
The conditions are matched against the properties of the pages you want to
inject into. As explained in 'How does it work?' we can give an object of
properties to a page. You can add a custom object to the HygraphPageContent()
in your own NextJS pages. You can then use the conditions field in Hygraph to
match those properties.
The conditions field works as follows:
We can match against string or number values using the Text and Number components.
Text and Number components contain a property and a value field. This way we can match values against the page properties. If there is a match the Dynamic Row will be injected.
The property can be accessed with javascript dot notation. If the object contains an array of objects, you don't have to use bracket notation. Let's say categories is an array of category objects which hold the category name:
{
categories [
{
name: "Animals",
},
{
name: "Landscapes",
},
]
}
In this case, the property field can be accessed using categories.name
syntax.
Number components contain an additional field called operator. Here we can match if a number is equal to, greater than or lower than.
We can implement logic with the And & Or components. These components allow nesting of Text and Number components.