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

4. Working with Locations

[=20 Add= ing a new Location to a Content item ] [=20 Hide/Unhide Loca= tion ] [=20 Deleting a locatio= n ] [=20 Se= tting a content item's main Location ]

Addin= g a new Location to a Content item

Full code

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, we need to get a new LocationCreateStruct. We will use it to set our new Loca= tion's properties. The new Location's parent ID is provided as a= parameter to Lo= cationService::newLocationCreateStruct.

In this example, we use the default values for the various LocationCreat= eStruct properties. We could of course have set custom values, like setting= the Location as hidden ($location->hidden =3D true), or changed the rem= oteId ($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 ContentI= nfo obtained above, together with our LocationCreateStruct. The method retu= rns the newly created Location Value Object.

Hide/Unhide Locati= on

Full code

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. You 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 Locat= ionService::unhideLocation(). Both expect the LocationInfo as their argumen= t, 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<= /h2>

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().

Sett= ing 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>

------=_Part_3087_118753194.1485851963887--