Skip to content

Taxonomy API

To manage taxonomies, use Ibexa\Contracts\Taxonomy\Service\TaxonomyServiceInterface.

Getting taxonomy entries

To get a single taxonomy entry, you can use TaxonomyServiceInterface::loadEntryById() and provide it with the numerical entry ID, or pass entry identifier and use TaxonomyServiceInterface::loadEntryByIdentifier():

1
2
3
        $entry = $this->taxonomyService->loadEntryByIdentifier('desks');

        $output->writeln($entry->name . ' with parent ' . $entry->parent->name);

You can also get a taxonomy entry from the ID of its underlying Content item, by using TaxonomyServiceInterface::loadEntryByContentId().

To get the root (main) entry of a given taxonomy, use TaxonomyServiceInterface::loadRootEntry() and provide it with the taxonomy name.

To get all entries in a taxonomy, use TaxonomyServiceInterface::loadAllEntries(), provide it with the taxonomy identifier, and optionally specify the limit of results and their offset. The default limit is 30.

1
        $allEntries = $this->taxonomyService->loadAllEntries('tags', 30, 0);

To get all children of a specific taxonomy entry, use TaxonomyServiceInterface::loadEntryChildren(), provide it with the entry object, and optionally specify the limit of results and their offset. The default limit is 30:

1
2
3
4
5
        $entryChildren = $this->taxonomyService->loadEntryChildren($entry, 10, 0);

        foreach ($entryChildren as $child) {
            $output->writeln($child->name);
        }

Managing taxonomy entries

You can move a taxonomy entry to a different parent by using TaxonomyServiceInterface::moveEntry(). Provide the method with two objects: the entry that you want to move and the new parent entry:

1
2
3
4
        $entryToMove = $this->taxonomyService->loadEntryByIdentifier('standing_desks');
        $newParent = $this->taxonomyService->loadEntryByIdentifier('desks');

        $this->taxonomyService->moveEntry($entryToMove, $newParent);

You can also move a taxonomy entry by passing its target sibling entry to TaxonomyServiceInterface::moveEntry(). The method takes as parameters the entry you want to move, the future sibling, and a position parameter, which is either TaxonomyServiceInterface::MOVE_POSITION_NEXT or TaxonomyServiceInterface::MOVE_POSITION_PREV:

1
2
        $sibling = $this->taxonomyService->loadEntryByIdentifier('school_desks');
        $this->taxonomyService->moveEntryRelativeToSibling($entryToMove, $sibling, TaxonomyServiceInterface::MOVE_POSITION_PREV);