Skip to content

Object state API

Object states enable you to set a custom state to any content. States are grouped into Object state groups.

You can manage Object states by using the PHP API by using ObjectStateService.

Object state REST API

To learn how to manage Object states using the REST API, see REST API reference.

Getting Object state information

You can use the ObjectStateService to get information about Object state groups or Object states.

1
2
3
4
5
        $objectStateGroup = $this->objectStateService->loadObjectStateGroupByIdentifier('ez_lock');
        $objectState = $this->objectStateService->loadObjectStateByIdentifier($objectStateGroup, 'locked');

        $output->writeln($objectStateGroup->getName());
        $output->writeln($objectState->getName());

Creating Object states

To create an Object state group and add Object states to it, you need to make use of the ObjectStateService:

1
2
3
4
        $objectStateGroupStruct = $this->objectStateService->newObjectStateGroupCreateStruct($objectStateGroupIdentifier);
        $objectStateGroupStruct->defaultLanguageCode = 'eng-GB';
        $objectStateGroupStruct->names = ['eng-GB' => $objectStateGroupIdentifier];
        $newObjectStateGroup = $this->objectStateService->createObjectStateGroup($objectStateGroupStruct);

ObjectStateService::createObjectStateGroup takes as argument an ObjectStateGroupCreateStruct, in which you need to specify the identifier, default language and at least one name for the group.

To create an Object state inside a group, use ObjectStateService::newObjectStateCreateStruct and provide it with an ObjectStateCreateStruct:

1
2
3
4
            $stateStruct = $this->objectStateService->newObjectStateCreateStruct($objectStateIdentifier);
            $stateStruct->defaultLanguageCode = 'eng-GB';
            $stateStruct->names = ['eng-GB' => $objectStateIdentifier];
            $this->objectStateService->createObjectState($newObjectStateGroup, $stateStruct);

Assigning Object state

To assign an Object state to a content item, use ObjectStateService::setContentState. Provide it with a ContentInfo object of the content item, the Object state group and the Object state:

1
2
3
4
5
            $contentInfo = $this->contentService->loadContentInfo($contentId);
            $objectStateGroup = $this->objectStateService->loadObjectStateGroupByIdentifier($objectStateGroupIdentifier);
            $objectState = $this->objectStateService->loadObjectStateByIdentifier($objectStateGroup, $objectStateToAssign);

            $this->objectStateService->setContentState($contentInfo, $objectStateGroup, $objectState);