Skip to content

Section API

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

You can manage Sections by using the PHP API by using SectionService.

Section REST API

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

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
5
        $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
10
        $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.