From dde077877bd231cf94df7162e06ac80d8c338af5 Mon Sep 17 00:00:00 2001 From: gggeek Date: Sun, 15 Oct 2017 19:11:46 +0100 Subject: [PATCH] Allow setting content main location --- Core/Executor/ContentManager.php | 28 +++++++++++++++++++++++ Core/Executor/LocationManager.php | 25 +++++++++++++++++++- Core/Matcher/LocationMatcher.php | 1 + Resources/doc/DSL/Contents.yml | 1 + Resources/doc/DSL/Locations.yml | 2 ++ Tests/dsl/good/UnitTestOK005_location.yml | 9 ++++++++ WHATSNEW.md | 2 ++ 7 files changed, 67 insertions(+), 1 deletion(-) diff --git a/Core/Executor/ContentManager.php b/Core/Executor/ContentManager.php index beec36b0..be42cbe0 100644 --- a/Core/Executor/ContentManager.php +++ b/Core/Executor/ContentManager.php @@ -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; @@ -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) { @@ -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; } @@ -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 * diff --git a/Core/Executor/LocationManager.php b/Core/Executor/LocationManager.php index bcc126a6..e8fbc639 100644 --- a/Core/Executor/LocationManager.php +++ b/Core/Executor/LocationManager.php @@ -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); @@ -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; } } @@ -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; } @@ -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 diff --git a/Core/Matcher/LocationMatcher.php b/Core/Matcher/LocationMatcher.php index 6693e0e6..8bfcb31f 100644 --- a/Core/Matcher/LocationMatcher.php +++ b/Core/Matcher/LocationMatcher.php @@ -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) { diff --git a/Resources/doc/DSL/Contents.yml b/Resources/doc/DSL/Contents.yml index f24c3386..12d9b3fc 100644 --- a/Resources/doc/DSL/Contents.yml +++ b/Resources/doc/DSL/Contents.yml @@ -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 diff --git a/Resources/doc/DSL/Locations.yml b/Resources/doc/DSL/Locations.yml index fdeceb0e..d7f2cb2d 100644 --- a/Resources/doc/DSL/Locations.yml +++ b/Resources/doc/DSL/Locations.yml @@ -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 @@ -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 diff --git a/Tests/dsl/good/UnitTestOK005_location.yml b/Tests/dsl/good/UnitTestOK005_location.yml index af8ecffb..be606e2e 100644 --- a/Tests/dsl/good/UnitTestOK005_location.yml +++ b/Tests/dsl/good/UnitTestOK005_location.yml @@ -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: @@ -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 @@ -255,6 +263,7 @@ parent_location: reference:kmb_test_005_4_loc priority: 1998 is_hidden: false + is_main: true sort_order: ASC references: - diff --git a/WHATSNEW.md b/WHATSNEW.md index 64d8c068..f7daa1b4 100644 --- a/WHATSNEW.md +++ b/WHATSNEW.md @@ -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 =============