Skip to content

Commit

Permalink
Allow setting content main location
Browse files Browse the repository at this point in the history
  • Loading branch information
gggeek committed Oct 15, 2017
1 parent be3b9c9 commit dde0778
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 1 deletion.
28 changes: 28 additions & 0 deletions Core/Executor/ContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct;
use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct;
Expand Down Expand Up @@ -220,6 +221,10 @@ protected function update($step)
throw new \Exception("Can not execute Content update because multiple contents match, and a references section is specified in the dsl. References can be set when only 1 content matches");
}

if (count($contentCollection) > 1 && isset($step->dsl['main_location'])) {
throw new \Exception("Can not execute Content update because multiple contents match, and a main_location section is specified in the dsl. References can be set when only 1 content matches");
}

$contentType = array();

foreach ($contentCollection as $key => $content) {
Expand Down Expand Up @@ -286,6 +291,10 @@ protected function update($step)
$this->setObjectStates($content, $step->dsl['object_states']);
}

if (isset($step->dsl['main_location'])) {
$this->setMainLocation($content, $step->dsl['main_location']);

}
$contentCollection[$key] = $content;
}

Expand Down Expand Up @@ -648,6 +657,25 @@ protected function setObjectStates(Content $content, array $stateKeys)
}
}

protected function setMainLocation(Content $content, $locationId)
{
$locationId = $this->referenceResolver->resolveReference($locationId);
if (is_int($locationId) || ctype_digit($locationId)) {
$location = $this->repository->getLocationService()->loadLocation($locationId);
} else {
$location = $this->repository->getLocationService()->loadLocationByRemoteId($locationId);
}

if ($location->contentInfo->id != $content->id) {
throw new \Exception("Can not set main location {$location->id} to content {$content->id} as it belongs to another object");
}

$contentService = $this->repository->getContentService();
$contentMetaDataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
$contentMetaDataUpdateStruct->mainLocationId = $location->id;
$contentService->updateContentMetadata($location->contentInfo, $contentMetaDataUpdateStruct);
}

/**
* Create the field value from the migration definition hash
*
Expand Down
25 changes: 24 additions & 1 deletion Core/Executor/LocationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ protected function create($step)
$parentLocationIds = array($parentLocationIds);
}

if (isset($step->dsl['is_main']) && count($parentLocationIds) > 1) {
throw new \Exception('Can not set more than one new location as main.');
}

// resolve references and remote ids
foreach ($parentLocationIds as $id => $parentLocationId) {
$parentLocationId = $this->referenceResolver->resolveReference($parentLocationId);
Expand Down Expand Up @@ -81,7 +85,13 @@ protected function create($step)
$locationCreateStruct->sortField = $this->getSortField($step->dsl['sort_field']);
}

$locations[] = $locationService->createLocation($contentInfo, $locationCreateStruct);
$location = $locationService->createLocation($contentInfo, $locationCreateStruct);

if (isset($step->dsl['is_main'])) {
$this->setMainLocation($location);
}

$locations[] = $location;
}
}

Expand Down Expand Up @@ -183,6 +193,11 @@ protected function update($step)
$locationService->swapLocation($location, $locationToSwap);
}

// make the location the main one
if (isset($step->dsl['is_main'])) {
$this->setMainLocation($location);
}

$locationCollection[$key] = $location;
}

Expand Down Expand Up @@ -387,6 +402,14 @@ protected function matchContents($action, $step)
return $this->contentMatcher->matchContent($match);
}

protected function setMainLocation(Location $location)
{
$contentService = $this->repository->getContentService();
$contentMetaDataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
$contentMetaDataUpdateStruct->mainLocationId = $location->id;
$contentService->updateContentMetadata($location->contentInfo, $contentMetaDataUpdateStruct);
}

/**
* @param $newValue
* @param null $currentValue
Expand Down
1 change: 1 addition & 0 deletions Core/Matcher/LocationMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ protected function getQueryCriterion($key, $values)
return new Query\Criterion\Location\Depth(self::$operatorsMap[$operator], $match);

case self::MATCH_IS_MAIN_LOCATION:
case 'is_main':
/// @todo error/warning if there is more than 1 value...
$value = reset($values);
if ($value) {
Expand Down
1 change: 1 addition & 0 deletions Resources/doc/DSL/Contents.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
priority: 0 # Optional. Set the priority of the main location
publication_date: zzz # Optional, set content publication date (if integer: timstamp is assumed. if string, see: http://php.net/manual/en/datetime.formats.php)
remote_id: custom_remote_id # Optional, will set a custom remote id
main_location: int|string # Optional, will set the main location. If passed a string, the location remote_id is assumed
section: x # string|int Optional section id or identifier
sort_field: x # Optional. See ManageLocation.yml for details
sort_order: ASC|DESC # Optional
Expand Down
2 changes: 2 additions & 0 deletions Resources/doc/DSL/Locations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
parent_location_remote_id: # the remote location id(s) of the parent(s) of contents we want to add a location to
parent_location: y # The Location ID of the parent where the new location should be placed. When a non numeric string is used, it is assumed to be a location remote id
is_hidden: true|false # Optional
is_main: true # Optional: make this the main location. NB: will *not* unset this location from being main if set to false
priority: x # Optional
sort_field: x # Optional. Possible values for sort_field are:
# - published
Expand Down Expand Up @@ -96,6 +97,7 @@
parent_location: x # Optional The parent location to move the subtree to. Cannot be set at the same time than swap_with_location
# When a non numeric string is used, it is assumed to be a location remote id
is_hidden: true|false # Optional, Set the visibility of the location
is_main: true # Optional: make this the main location. NB: will *not* unset this location from being main if set to false
priority: x # Optional, will be updated if set
sort_field: x # Optional
sort_order: ASC|DESC # Optional
Expand Down
9 changes: 9 additions & 0 deletions Tests/dsl/good/UnitTestOK005_location.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
parent_location_id: reference:kmb_test_005_2_loc
priority: 1999
is_hidden: true
is_main: true
sort_field: path
sort_order: DESC
match:
Expand Down Expand Up @@ -191,6 +192,13 @@
identifier: kmb_test_005_3_loc_3_sort_order
attribute: sort_order

-
type: content
mode: update
match:
location_id: reference:kmb_test_005_3_loc
main_location: reference:kmb_test_005_3_loc_3

-
type: assert
target: reference
Expand Down Expand Up @@ -255,6 +263,7 @@
parent_location: reference:kmb_test_005_4_loc
priority: 1998
is_hidden: false
is_main: true
sort_order: ASC
references:
-
Expand Down
2 changes: 2 additions & 0 deletions WHATSNEW.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Version 4.2
* New: references are now resolved in `validator-configuration` settings for content type definitions. This is useful
eg. when using the eZTags v3 bundle

* New: allow to set content main location


Version 4.1.1
=============
Expand Down

0 comments on commit dde0778

Please sign in to comment.