Skip to content

Repository API

Sections

Sections enable you to divide content into groups which can later be used e.g. as basis for permissions.

Creating Sections

To create a new Section, you need to make use of the SectionCreateStruct and pass it to the SectionService::createSection method:

1
2
3
4
        $sectionCreateStruct = $this->sectionService->newSectionCreateStruct();
        $sectionCreateStruct->name = $sectionName;
        $sectionCreateStruct->identifier = $sectionIdentifier;
        $this->sectionService->createSection($sectionCreateStruct);

Getting Section information

You can use SectionService to retrieve Section information such as whether it is in use:

1
2
3
4
        $output->writeln((
            $this->sectionService->isSectionUsed($section)
            ? 'This section is in use.'
            : 'This section is not in use.'

Listing content in a Section

To list Content items assigned to a Section you need to make a query for content belonging to this section, by applying the SearchService. You can also use the query to get the total number of assigned Content items:

1
2
3
4
5
6
7
8
9
        $query = new LocationQuery();
        $query->filter = new Criterion\SectionId([
            $section->id,
        ]);

        $result = $this->searchService->findContentInfo($query);

        foreach ($result->searchHits as $searchResult) {
            $output->writeln('* ' . $searchResult->valueObject->name);

Assigning Section to content

To assign content to a Section, use the SectionService::assignSection method. You need to provide it with the ContentInfo object of the Content item, and the Section object:

1
2
3
        $section = $this->sectionService->loadSectionByIdentifier($sectionIdentifier);
        $contentInfo = $this->contentService->loadContentInfo($contentId);
        $this->sectionService->assignSection($contentInfo, $section);

Note that assigning a Section to content does not automatically assign it to the Content item's children.

Object states

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

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->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
            $objectStateGroup = $this->objectStateService->loadObjectStateGroupByIdentifier($objectStateGroupIdentifier);
            $objectState = $this->objectStateService->loadObjectStateByIdentifier($objectStateGroup, $objectStateToAssign);

            $this->objectStateService->setContentState($contentInfo, $objectStateGroup, $objectState);
            $output->writeln('Content ' . $contentInfo->name . ' assigned state ' . $objectState->getName());

Workflow

Getting workflow information

To get information about a specific workflow for a Content item, use WorkflowServiceInterface::loadWorkflowMetadataForContent:

1
2
3
4
        $workflowMetadata = $this->workflowService->loadWorkflowMetadataForContent($content, $workflowName);

        foreach ($workflowMetadata->markings as $marking) {
            $output->writeln($content->getName() . ' is in stage ' . $marking->name . ' in workflow ' . $workflowMetadata->workflow->getName());

Tip

marking, a term from Symfony Workflow, refers to a state in a workflow.

To get a list of all workflows that can be used for a given Content item, use WorkflowRegistry:

1
        $supportedWorkflows = $this->workflowRegistry->getSupportedWorkflows($content);

Applying workflow transitions

To place a Content item in a workflow, use WorkflowService::start:

1
        $this->workflowService->start($content, $workflowName);

To apply a transition to a Content item, use Workflow::apply. Additionally, you can check if the transition is possible for the given object using WorkflowService::can:

1
2
3
4
        if ($this->workflowService->can($workflowMetadata, $transitionName)) {
            $workflow = $this->workflowRegistry->getWorkflow($workflowName);
            $workflow->apply($workflowMetadata->content, $transitionName, ['Please review']);
    }

Tip

\EzSystems\EzPlatformWorkflow\Value\WorkflowMetadata object contains all information about a workflow, such as ID, name, transitions and current stage. \EzSystems\EzPlatformWorkflow\Value\WorkflowMetadata::$workflow gives you direct access to native Symfony Workflow object.

Bookmarks

BookmarkService enables you to read, add and remove bookmarks from content.

To view a list of all bookmarks, use BookmarkService::loadBookmarks:

1
2
3
4
5
6
7
        $bookmarkList = $this->bookmarkService->loadBookmarks();

        $output->writeln('Total bookmarks: ' . $bookmarkList->totalCount);

        foreach ($bookmarkList->items as $bookmark) {
            $output->writeln($bookmark->getContentInfo()->name);
        }

You can add a bookmark to a Content item by providing its Location object to the BookmarkService::createBookmark method:

1
2
3
        $location = $this->locationService->loadLocation($locationId);

        $this->bookmarkService->createBookmark($location);

You can remove a bookmark from a Location with BookmarkService::deleteBookmark:

1
            $this->bookmarkService->deleteBookmark($location);

Languages

Getting language information

To get a list of all languages in the system use LanguageService::loadLanguages:

1
2
3
4
5
        $languageList = $this->languageService->loadLanguages();

        foreach ($languageList as $language) {
            $output->writeln($language->languageCode . ': ' . $language->name);
        }

Creating a language

To create a new language, you need to create a LanguageCreateStruct and provide it with the language code and language name. Then, use LanguageService::createLanguage and pass the LanguageCreateStruct to it:

1
2
3
4
        $languageCreateStruct = $this->languageService->newLanguageCreateStruct();
        $languageCreateStruct->languageCode = 'pol-PL';
        $languageCreateStruct->name = 'Polish';
        $this->languageService->createLanguage($languageCreateStruct);