Skip to content

Managing content

Locations

You can manage Locations that hold content using LocationService.

Adding a new Location to a Content item

Every published Content item must have at least one Location. One Content item can have more that one Location, which means it is presented in more than one place in the content tree.

Creating a new Location, like creating content, requires using a struct, because a Location value object is read-only.

To add a new Location to existing content you need to create a LocationCreateStruct and pass it to the LocationService::createLocation method:

1
        $newLocation = $this->locationService->createLocation($contentInfo, $locationCreateStruct);

LocationCreateStruct must receive the parent Location ID. It sets the parentLocationId property of the new Location.

You can also provide other properties for the Location, otherwise they will be set to their defaults:

1
        $locationCreateStruct->hidden = true;

Changing the main Location

When a Content item has more that one Location, one Location is always considered the main one. You can change the main Location using ContentService, by updating the ContentInfo with a ContentUpdateStruct that sets the new main Location:

1
2
3
4
        $contentUpdateStruct = $this->contentService->newContentMetadataUpdateStruct();
        $contentUpdateStruct->mainLocationId = $locationId;

        $this->contentService->updateContentMetadata($contentInfo, $contentUpdateStruct);

Hiding and revealing Locations

To hide or reveal (unhide) a Location you need to make use of LocationService::hideLocation or LocationService::unhideLocation:

1
2
        $this->locationService->hideLocation($location);
        $this->locationService->unhideLocation($location);

See Location visibility for detailed information on the behavior of visible and hidden Locations.

Deleting a Location

You can remove a Location either by deleting it, or sending it to Trash.

Deleting makes use of LocationService::deleteLocation(). It permanently deletes the Location, together with its whole subtree.

Content which has only this one Location will be permanently deleted as well. Content which has more Locations will be still available in its other Locations. If you delete the main Location of a Content item that has more Locations, another Location will become the main one.

1
        $this->locationService->deleteLocation($location);

To send the Location and its subtree to Trash, use TrashService::trash. Items in Trash can be later restored, or deleted permanently.

1
        $this->trashService->trash($location);

Moving and copying a subtree

You can move a Location with its whole subtree using LocationService::moveSubtree:

1
2
3
        $targetLocation = $this->locationService->loadLocation($targetLocationId);
        $this->locationService->moveSubtree($sourceLocation, $targetLocation);
        $output->writeln('Location ' . $locationId . ' moved to ' . $targetLocationId . ' with its subtree.');

LocationService::copySubtree is used in the same way, but it copies the Location and its subtree instead of moving it.

Tip

To copy a subtree you can also make use of the built-in copy-subtree command: bin/console ibexa:copy-subtree <sourceLocationId> <targetLocationId>.

Note

Copy subtree limit only applies to operations in the Back Office. It is ignored when copying subtrees using the PHP API.

Trash

To empty the Trash (remove all Locations in Trash), use TrashService::emptyTrash, which takes no arguments.

You can recover an item from Trash using TrashService::recover. You must provide the method with the ID of the object in Trash. Trash Location is identical to the origin Location of the object.

1
            $this->trashService->recover($trashItem, $newParent);

The Content item will be restored under its previous Location. You can also provide a different Location to restore in as a second argument:

1
2
$newParent = $this->locationService->loadLocation($location);
$this->trashService->recover($trashItem, $newParent);

You can also search through Trash items and sort the results using several public PHP API search criteria and sort clauses that have been exposed for TrashService queries. For more information, see Searching in trash.

Content Types

Adding Content Types

To operate on Content Types, you need to make use of ContentTypeService.

Creating a new Content Type, like creating content, must happen with the use of a struct, because a Content Type value object is read-only. In this case you use ContentTypeCreateStruct.

A Content Type must have at least one name, in the main language, and at least one Field definition.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
        $contentTypeCreateStruct = $this->contentTypeService->newContentTypeCreateStruct($contentTypeIdentifier);
        $contentTypeCreateStruct->mainLanguageCode = 'eng-GB';
        $contentTypeCreateStruct->nameSchema = '<name>';

        $contentTypeCreateStruct->names = [
            'eng-GB' => $contentTypeIdentifier,
        ];

        $titleFieldCreateStruct = $this->contentTypeService->newFieldDefinitionCreateStruct('name', 'ezstring');
        $titleFieldCreateStruct->names = ['eng-GB' => 'Name'];
        $contentTypeCreateStruct->addFieldDefinition($titleFieldCreateStruct);

        $contentTypeDraft = $this->contentTypeService->createContentType(
            $contentTypeCreateStruct,
            [$contentTypeGroup]
        );

        $this->contentTypeService->publishContentTypeDraft($contentTypeDraft);
        $output->writeln("Content type '$contentTypeIdentifier' with ID $contentTypeDraft->id created");

You can specify more details of the Field definition in the create struct, for example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
        $titleFieldCreateStruct = $this->contentTypeService->newFieldDefinitionCreateStruct('name', 'ezstring');
        $titleFieldCreateStruct->names = ['eng-GB' => 'Name'];
        $titleFieldCreateStruct->descriptions = ['eng-GB' => 'The name'];
        $titleFieldCreateStruct->fieldGroup = 'content';
        $titleFieldCreateStruct->position = 10;
        $titleFieldCreateStruct->isTranslatable = true;
        $titleFieldCreateStruct->isRequired = true;
        $titleFieldCreateStruct->isSearchable = true;

        $contentTypeCreateStruct->addFieldDefinition($titleFieldCreateStruct);

Copying Content Types

To copy a Content Type, use ContentTypeService::copyContentType:

1
            $copyDraft = $this->contentTypeService->createContentTypeDraft($copy);

The copy will automatically be given an identifier based on the original Content Type identifier and the copy's ID, for example: copy_of_folder_21.

To change the identifier of the copy, use a ContentTypeUpdateStruct:

1
2
3
4
5
6
            $copyUpdateStruct = $this->contentTypeService->newContentTypeUpdateStruct();
            $copyUpdateStruct->identifier = $copyIdentifier;
            $copyUpdateStruct->names = ['eng-GB' => $copyIdentifier];
            $this->contentTypeService->updateContentTypeDraft($copyDraft, $copyUpdateStruct);
            $this->contentTypeService->publishContentTypeDraft($copyDraft);
            $output->writeln('Copy of the new CT created with identifier ' . $copyIdentifier);

Calendar events

You can handle the calendar using CalendarServiceInterface (EzSystems\EzPlatformCalendar\Calendar\CalendarServiceInterface).

Getting events

To get a list of events for a specified time period, use the CalendarServiceInterface::getEvents method. You need to provide the method with an EventQuery, which takes a date range and a count as the minimum of parameters:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
        $dateFrom = new \DateTimeImmutable('2021-12-01T10:00:00+00:00');
        $dateTo = new \DateTimeImmutable('2021-12-31T10:0:00+00:00');
        $dateRange = new Calendar\DateRange($dateFrom, $dateTo);

        $eventQuery = new Calendar\EventQuery($dateRange, 10);

        $eventList = $this->calendarService->getEvents($eventQuery);

        foreach ($eventList as $event) {
            $output->writeln($event->getName() . '; date: ' . $event->getDateTime()->format('T Y-m-d H:i:s'));
        }

You can also get the first and last event in the list by using the first() and last() methods of an EventCollection (EzSystems\EzPlatformCalendar\Calendar\EventCollection) respectively:

1
2
        $eventCollection = $eventList->getEvents();
        $output->writeln('First event: ' . $eventCollection->first()->getName() . '; date: ' . $eventCollection->first()->getDateTime()->format('T Y-m-d H:i:s'));

You can process the events in a collection using the find(Closure $predicate), filter(Closure $predicate), map(Closure $callback) or slice(int $offset, ?int $length = null) methods of EventCollection, for example:

1
2
3
        $newCollection = $eventCollection->slice(3, 5);
        foreach ($newCollection as $event) {
            $output->writeln('New collection: ' . $event->getName() . '; date: ' . $event->getDateTime()->format('T Y-m-d H:i:s'));

Performing calendar actions

You can perform a calendar action (e.g. reschedule or unschedule calendar events) using the CalendarServiceInterface::executeAction() method. You must pass an EzSystems\EzPlatformCalendar\Calendar\EventAction\EventActionContext instance as argument. EventActionContext defines events on which the action is performed, as well as action-specific parameters e.g. a new date:

1
2
        $newDate = new \DateTimeImmutable('2021-12-06T13:00:00+00:00');
        $context = new RescheduleEventActionContext($eventCollection, $newDate);
1
$context = new UnscheduleEventActionContext($eventCollection);