Message-ID: <1378964955.3694.1485854854118.JavaMail.confluence@ip-10-127-227-164> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_3693_693005329.1485854854118" ------=_Part_3693_693005329.1485854854118 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html Working with Locations

Working with Locations

=20
=20
=20
=20

Adding a n= ew Location to a Content item

We have seen earlier how you can create a Location for a newly created <= code>Content. It is of course also possible to add a new Location= to an existing Content.

=20
try
{
    $locationCreateStruct =3D $locationService->newLocationCreateStruct(=
 $parentLocationId );
    $contentInfo =3D $contentService->loadContentInfo( $contentId );
    $newLocation =3D $locationService->createLocation( $contentInfo, $lo=
cationCreateStruct );
    print_r( $newLocation );
}
// Content or location not found
catch ( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e )
{
    $output->writeln( $e->getMessage() );
}
// Permission denied
catch ( \eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e )
{
    $output->writeln( $e->getMessage() );
}
=20

This is the required code. As you can see, both the ContentService and t= he LocationService are involved. Errors are handled the usual way, by inter= cepting the Exceptions the used methods may throw.

=20
$locationCreateStruct =3D $locationService->newLocationCreateStr=
uct( $parentLocationId );
=20

Like we do when creating a new Content item, we need to get a new = LocationCreateStruct. We will use it to set our new = Location's properties. The new Location's parent ID is provided = as a parameter to LocationService::newLocationCreateStruct.

In this example, we use the default values for the various Locatio= nCreateStruct properties. We could of course have set custom values,= like setting the Location as hidden ($location->hidden =3D true), or ch= anged the remoteId ($location->remoteId =3D $myRemoteId).

=20
$contentInfo =3D $contentService->loadContentInfo( $contentId );=
=20

To add a Location to a Content item, we need to specify the Content, usi= ng a ContentInfo object. We load one using ContentService::loadContentInfo(), using the Content ID as t= he argument.

=20
$newLocation =3D $locationService->createLocation( $contentInfo,=
 $locationCreateStruct );
=20

We finally use LocationService::createLocation(), providing= the ContentInfo obtained above, together with our Locat= ionCreateStruct. The method returns the newly created Location Value= Object.

Hide/Unhide Location

We mentioned earlier that a Location's visibility could be set while cre= ating the Location, using the hidden property of the LocationCreateStruct. = Changing a Location's visibility may have a large impact in the Repository:= doing so will affect the Location's subtree visibility. For this reason, a= LocationUpdateStruct doesn't let you toggle this property. Yo= u need to use the LocationService to do so.

=20
$hiddenLocation =3D $locationService->hideLocation( $location );
$unhiddenLocation =3D $locationService->unhideLocation( $hiddenLocation =
);
=20

There are two methods for this: LocationService::hideLocation, and LocationService::unhideLocation(). Both expect the LocationInfo as their argument, and return the modified Location = Value Object.

The explanation above is valid for most Repository objects. Modification= of properties that affect other parts of the system will require that you = use a custom service method.

Deleting a Location

Deleting Locations can be done in two ways: delete, or trash. 

=20
$locationService->deleteLocation( $locationInfo );
=20

LocationService::deleteLocation() will perma= nently delete the Location, as well as all its descendants. Content th= at has only one Location will be permanently deleted as well. Those with mo= re than one won't be, as they are still referenced by at least one Location= .

=20
$trashService->trash( $locationInfo );
=20

TrashService::trash() will send the Location as well as = all its descendants to the Trash, where they can be found and restored unti= l the Trash is emptied. Content isn't affected at all, since it is still re= ferenced by the trash items.

The T= rashService can be used to list, restore and delete Locations th= at were previously sent to Trash using TrashService::trash().

Setting a= content item's main Location

This is done using the ContentService, by updating the ContentInfo with a ContentUpdateStruct that sets the = new main location:

=20
$repository =3D $this->getContainer()->get( 'ezpublish.api.re=
pository' );
$contentService =3D $repository->getContentService();
$contentInfo =3D $contentService->loadContentInfo( $contentId );

$contentUpdateStruct =3D $contentService->newContentMetadataUpdateStruct=
();
$contentUpdateStruct->mainLocationId =3D 123;

$contentService->updateContentMetadata( $contentInfo, $contentUpdateStru=
ct );
=20

Credits to Gareth Arnott for the snippet.<= /p>

=20
=20
=20
=20

In this topic:

=20
=20
=20
------=_Part_3693_693005329.1485854854118--