Skip to main content

Managing Field Values via PHP

With the latest feature, you can now create and update field values directly from your PHP code. This allows for more dynamic control over the data without needing to interact with the back-office interface. Below is an explanation of how to use this feature effectively.

Why use PHP to manage field values?

Managing field values via PHP offers flexibility for developers who want to automate or programmatically control the data input process. Whether you're importing data from external sources, syncing data with third-party systems, or pre-populating fields, this functionality provides an efficient method to update field values.

Creating and Updating field values

To create or update the values of custom fields in your entities, follow the steps below. Ensure that the group structure is already defined; this method only handles values, not the structure of the groups.

  • Load the field group: To start, load the field group for which you want to create or update the values. Use the DataRetriever service to get access to FieldGroupUpdater.
/SomeClass.php
function someMethod()
{
$dataRetriever = DataRetrieverFacade::getInstance();

// Entity ID is optional; it's used to load existing data for the field group if needed.
$fieldGroup = $dataRetriever->getFieldGroupUpdater('field_group_slug', $idEntity);
}
  • Modify field values: Once the data is retrieved, you can modify the values as needed. Here is an example of updating a field value:
/SomeClass.php
function someMethod()
{
..
/**
* BASIC FIELDS
*/
$fieldGroup->getField('field_text_slug')->setValue('My value');
$fieldGroup->getField('field_boolean_slug')->setValue(true);
$fieldGroup->getField('field_choices')->setValue([1,2]);



/**
* RELATION FIELDS
*/
$fieldGroup->getField('field_product_slug')->setValue($idProduct);
$fieldGroup->getField('field_category_slug')->setValue($idProduct);



/**
* GROUP FIELD: Access subfields
*/
$fieldGroup->getField('field_group_slug')->getField('field_slug')->setValue('value');



/**
* REPEATER FIELD
*/
$repeater = $fieldGroup->getField('field_repeater_slug');

// Add new row
$row = $repeater->createRow();
$row->getField('row_field_slug')->setValue(true);
$repeater->addRow($subRow);

// Get rows
$rows = $repeater->getRows();

foreach ($rows as $k => $row) {
// Delete a row
$repeater->removeRow($key);

// Update a row
$row->getField('field_slug')->setValue('My value');
}



/**
* FLEXIBLE REPEATER FIELD
*/
$repeater = $fieldGroup->getField('field_flex_repeater_slug');

// Add new row
$row = $repeater->createRow('choice_slug');
$row->getField('row_field_slug')->setValue(true);
$repeater->addRow($subRow);

// Get rows
$rows = $repeater->getRows();

foreach ($rows as $k => $row) {
// Delete a row
$repeater->removeRow($key);

// Update a row
$row->getField('field_slug')->setValue('My value');
}
}
  • Validate and save values: After modifying the necessary values, you can check if your data is valid and save it using the appropriate method provided by the module.
/SomeClass.php
function someMethod()
{
..

// Boolean validation
$validation = $fieldGroup->validateFormData();

// Returns an array of errors or true if valid
$validation = $fieldGroup->validateFormData(true);

// Save the data for a specific ID; you can apply the same data to different entity IDs.
$fieldGroup->save($idEntity);
}

Use cases for PHP value management

  • Automated data imports: Integrate with third-party APIs or external data sources to populate your custom fields automatically.

  • Mass updates: Quickly update field values for multiple entities in bulk, improving efficiency.

  • Conditional value setting: Use conditional logic to set or modify values dynamically based on certain criteria, such as user input or external events.

Example Code:

Here’s a full example of creating and updating a custom field value programmatically:

/SomeClass.php
function someMethod()
{
$idCustomer = Context::getContext()->customer->id;
$dataRetriever = DataRetrieverFacade::getInstance();
$fieldGroup = $dataRetriever->getFieldGroupUpdater('field_group_slug');

$fieldGroup->getField('field_slug_text')->setValue('My value !');
$fieldGroup->getField('field_slug_color')->setValue('#ffffff');

$fieldGroup->save($idCustomer);
}