/** * Loads the main location for $contentId. * * If the content does not have a location (yet), but has a Location draft, it is returned instead. * Location drafts do not have an id (it is set to null), and can be tested using the isDraft() method. * * If the content doesn't have a location nor a location draft, null is returned. * * @param mixed $contentId * * @return \eZ\Publish\API\Repository\Values\Content\Location|null */ public function loadMainLocation($contentId) { $location = null; $contentInfo = $this->contentService->loadContentInfo($contentId); // mainLocationId already exists, content has been published at least once. if ($contentInfo->mainLocationId) { $location = $this->locationService->loadLocation($contentInfo->mainLocationId); } elseif (!$contentInfo->published) { // New Content, never published, create a virtual location object. // In cases content is missing locations this will return empty array $parentLocations = $this->locationHandler->loadParentLocationsForDraftContent($contentInfo->id); if (empty($parentLocations)) { return null; } $location = new Location(array('contentInfo' => $contentInfo, 'status' => Location::STATUS_DRAFT, 'parentLocationId' => $parentLocations[0]->id, 'depth' => $parentLocations[0]->depth + 1, 'pathString' => $parentLocations[0]->pathString . '/x')); } return $location; }
/** * Loads the main location for $contentId * * If the content does not have a location (yet), but has a Location draft, it is returned instead. * Location drafts do not have an id (it is set to null), and can be tested using the isDraft() method. * * If the content doesn't have a location nor a location draft, null is returned. * * @param mixed $contentInfo * * @return \eZ\Publish\API\Repository\Values\Content\Location|null */ public function loadMainLocation($contentId) { $contentInfo = $this->contentService->loadContentInfo($contentId); // mainLocationId already exists, content has been published at least once. if ($contentInfo->mainLocationId) { $location = $this->locationService->loadLocation($contentInfo->mainLocationId); } else { // @todo In future releases this will be a full draft location when this feature // is implemented. Or it might return null when content does not have location, // but for now we can't detect that so we return a virtual draft location $parentLocations = $this->locationHandler->loadParentLocationsForDraftContent($contentInfo->id); if (count($parentLocations) === 0) { return null; } $location = new Location(array('contentInfo' => $contentInfo, 'status' => Location::STATUS_DRAFT, 'parentLocationId' => $parentLocations[0]->id, 'depth' => $parentLocations[0]->depth + 1)); } return $location; }