Introduction to Functions
To address the need for dynamic content, Ector has introduced functions
, which are executed at runtime and allow for the inclusion of dynamic content within the theme configuration.
A function
consists of two main parts: its declaration in the configuration and its counterpart in the code.
Declaration of Use
To indicate in the configuration that a field contains a function
, simply insert the keyword function
as the property key and the function to be executed as the value. Here's an example from the base configuration, within IndexController -> carousel_product_1
:
{
"IndexController": {
"carousel_product_1": {
"title": "Featured Products",
"description": "Discover our featured products",
"function": "idToProduct(product_ids)",
"product_ids": [1, 2, 3, 4, 5]
}
}
}
In this configuration, we have defined that carousel_product_1
should execute a function
called idToProduct
and pass product_ids
as a parameter. Our product_ids
is an array of product IDs that we want to display in the carousel.
Code Counterpart
To create an executable function
from the configuration, you need to extend the hookActionEctorConfiguratorRegisterBlocks
hook and register the function within the existing blocks.
public function hookActionEctorConfiguratorRegisterBlocks($params)
{
$params["registeredBlocks"]["yourFunctionName"] = YourFunctionClass::class;
}
We recommend creating a dedicated module to register functions, so as not to compromise the main module files.
Creating the Function
Creating a function is simple: just create a class that extends BlockFunctionAbstract
and implements the build
method.
Here is an example of the idToProduct
function:
<?php
namespace Dgcal\EctorConfigurator\Content\BlockFunction;
class IdToProduct extends BlockFunctionAbstract
{
const IDENTIFIER = 'id_to_product_*';
public function getIdentifier()
{
return self::IDENTIFIER;
}
public function isCacheable()
{
return false;
}
public function build()
{
$assembler = $this->getAssembler();
$presenterFactory = $this->getPresenterFactory();
$productPresenter = $presenterFactory->getPresenter();
$content = $this->getContent();
foreach ($content[$this->getPropKey()] as $k => $id) {
$product = new \Product($id);
if (!$product->id) {
unset($content[$this->getPropKey()][$k]);
continue;
}
$assembledProduct = $assembler->assembleProduct(['id_product' => $id]);
$productArray = $productPresenter->present(
$presenterFactory->getPresentationSettings(),
$assembledProduct,
$this->getContext()->language
);
$content[$this->getPropKey()][$k] = $productArray;
}
$this->setContent($content);
}
}
The function receives the configuration content via the getContent
method and the name of the property containing the data to be processed via getPropKey
. In this case, it takes the product IDs and transforms them into an array of complete products, which are then passed back to the configuration.
Existing Functions
Ector provides several ready-to-use functions that allow you to quickly and easily insert dynamic content:
idToProduct
: Transforms product IDs into an array of complete products.idToCategory
: Transforms category IDs into an array of complete categories.idToCategoryWithSubs
: Transforms category IDs into an array of complete categories, including subcategories.idToBrand
: Transforms brand IDs into an array of complete manufacturers.bestseller
: Transforms product IDs into an array of bestseller products with essential information.
With this system, the extension options are endless, and common data passing and transformation logic can be reused without having to rewrite everything from scratch.