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);