Skip to main content
Version: Next

Using Data Services in Bundles

Other bundles (for example a portal) can read and write data object field values in exactly the shape the Studio Backend API uses. The data comes from the same data adapters, so a value read or written by your bundle looks the same as in Pimcore Studio.

Three services are supported for this. Type-hint their interfaces; they are autowired:

InterfacePurpose
Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterfaceRead field values (getDetailValue) and apply edited values to an object (updateEditableData)
Pimcore\Bundle\StudioBackendBundle\DataObject\Service\InheritanceServiceInterfaceFind out whether a field value is inherited from a parent object, and the inherited value
Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementSaveServiceInterfaceSave, publish or unpublish an element, or save a version, on behalf of a user

The FieldContextData value object (Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Model\FieldContextData) that appears in their signatures is supported as well.


The User​

In Pimcore Studio the services use the logged-in Studio user. A request of another bundle has no Studio session, so pass the Pimcore user whose permissions apply (for example the languages the user may see) explicitly:

MethodUser parameter
DataServiceInterface::getDetailValue()optional $user; also possible through FieldContextData (user: argument or withUser())
DataServiceInterface::updateEditableData()required $user, passed to every data adapter
InheritanceServiceInterface::getInheritanceData()optional $user
InheritanceServiceInterface::processFieldDefinition(), getFieldInheritanceData(), getOriginId()through FieldContextData
ElementSaveServiceInterface::save()required $user, set as the modifying user and checked for the publish/unpublish permission

Without a user, the services behave as in Studio and use the current Studio user.

caution

The services do not check whether the user may view or edit the object. Check the element permissions in your bundle before you call them.


Example​

<?php

namespace App\Service;

use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\InheritanceServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementSaveServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementSaveTasks;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\User;

final readonly class ProductEditor
{
public function __construct(
private DataServiceInterface $dataService,
private InheritanceServiceInterface $inheritanceService,
private ElementSaveServiceInterface $elementSaveService,
) {
}

public function read(Concrete $object, User $user): array
{
$values = [];
foreach ($object->getClass()->getFieldDefinitions() as $key => $fieldDefinition) {
$values[$key] = $this->dataService->getDetailValue(
$object,
$object->get($key),
$fieldDefinition,
user: $user
);
}

return [
'values' => $values,
'inheritance' => $this->inheritanceService->getInheritanceData(
$object,
$object->getClass()->getFieldDefinitions(),
true,
$user
),
];
}

/**
* @param array<string, mixed> $editableData field name => value in the shape getDetailValue() returns
*/
public function save(Concrete $object, array $editableData, User $user): void
{
$this->dataService->updateEditableData($object, $editableData, $user);
$this->elementSaveService->save($object, $user, ElementSaveTasks::VERSION->value);
}
}

The $task argument of save() takes the values of ElementSaveTasks: save, autoSave, version, publish and unpublish. Without a task the element is saved as it is.

updateEditableData() throws FieldValidationFailedException when a value fails validation and ElementSavingFailedException for other errors.