- Documentation >
- Content management >
- Content management API >
- Object state API
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.
You can use the ObjectStateService
to get information about object state groups or object states.
| $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
:
| $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
:
| $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:
| $contentInfo = $this->contentService->loadContentInfo($contentId);
$objectStateGroup = $this->objectStateService->loadObjectStateGroupByIdentifier($objectStateGroupIdentifier);
$objectState = $this->objectStateService->loadObjectStateByIdentifier($objectStateGroup, $objectStateToAssign);
$this->objectStateService->setContentState($contentInfo, $objectStateGroup, $objectState);
|