Skip to content

Commit

Permalink
EZP-29518: ContentType option "Default field for sorting children" ha…
Browse files Browse the repository at this point in the history
…s no effect on new created objects (#2428)

* Fixed ContentType 'Default field for sorting children'

* Fixed broken test and CS

* Fixed not using FQCN for method parameters in PHP doc and added missing failsafe.

* Fixed 'Cannot use isset() on the result of an expression' error

* Added missing empty line before return statement.
  • Loading branch information
mateuszbieniek authored and Łukasz Serwatka committed Sep 25, 2018
1 parent b4417cd commit 73d0544
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 16 deletions.
18 changes: 12 additions & 6 deletions eZ/Publish/Core/REST/Client/LocationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use eZ\Publish\Core\REST\Common\Input\Dispatcher;
use eZ\Publish\Core\REST\Common\Output\Visitor;
use eZ\Publish\Core\REST\Common\Message;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;

/**
* Location service, used for complex subtree operations.
Expand Down Expand Up @@ -79,16 +80,21 @@ public function setSession($id)
* Instantiates a new location create class.
*
* @param mixed $parentLocationId the parent under which the new location should be created
* @param eZ\Publish\API\Repository\Values\ContentType\ContentType|null $contentType
*
* @return \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct
*/
public function newLocationCreateStruct($parentLocationId)
public function newLocationCreateStruct($parentLocationId, ContentType $contentType = null)
{
return new LocationCreateStruct(
array(
'parentLocationId' => $parentLocationId,
)
);
$properties = [
'parentLocationId' => $parentLocationId,
];
if ($contentType) {
$properties['sortField'] = $contentType->defaultSortField;
$properties['sortOrder'] = $contentType->defaultSortOrder;
}

return new LocationCreateStruct($properties);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion eZ/Publish/Core/REST/Server/Controller/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,12 @@ protected function parseContentRequest(Request $request)
protected function doCreateContent(Request $request, RestContentCreateStruct $contentCreate)
{
try {
$contentCreateStruct = $contentCreate->contentCreateStruct;
$contentCreate->locationCreateStruct->sortField = $contentCreateStruct->contentType->defaultSortField;
$contentCreate->locationCreateStruct->sortOrder = $contentCreateStruct->contentType->defaultSortOrder;

$content = $this->repository->getContentService()->createContent(
$contentCreate->contentCreateStruct,
$contentCreateStruct,
array($contentCreate->locationCreateStruct)
);
} catch (ContentValidationException $e) {
Expand Down
9 changes: 9 additions & 0 deletions eZ/Publish/Core/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use eZ\Publish\API\Repository\ContentService as ContentServiceInterface;
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
use eZ\Publish\API\Repository\Values\Content\Language;
use eZ\Publish\Core\Repository\Values\Content\Location;
use eZ\Publish\SPI\Persistence\Handler;
use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct as APIContentUpdateStruct;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
Expand Down Expand Up @@ -875,6 +876,14 @@ protected function buildSPILocationCreateStructs(array $locationCreateStructs)
);
}

if (!array_key_exists($locationCreateStruct->sortField, Location::SORT_FIELD_MAP)) {
$locationCreateStruct->sortField = Location::SORT_FIELD_NAME;
}

if (!array_key_exists($locationCreateStruct->sortOrder, Location::SORT_ORDER_MAP)) {
$locationCreateStruct->sortOrder = Location::SORT_ORDER_ASC;
}

$parentLocationIdSet[$locationCreateStruct->parentLocationId] = true;
$parentLocation = $this->repository->getLocationService()->loadLocation(
$locationCreateStruct->parentLocationId
Expand Down
18 changes: 12 additions & 6 deletions eZ/Publish/Core/Repository/LocationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Exception;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;

/**
* Location service, used for complex subtree operations.
Expand Down Expand Up @@ -722,16 +723,21 @@ public function deleteLocation(APILocation $location)
* Instantiates a new location create class.
*
* @param mixed $parentLocationId the parent under which the new location should be created
* @param eZ\Publish\API\Repository\Values\ContentType\ContentType|null $contentType
*
* @return \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct
*/
public function newLocationCreateStruct($parentLocationId)
public function newLocationCreateStruct($parentLocationId, ContentType $contentType = null)
{
return new LocationCreateStruct(
array(
'parentLocationId' => $parentLocationId,
)
);
$properties = [
'parentLocationId' => $parentLocationId,
];
if ($contentType) {
$properties['sortField'] = $contentType->defaultSortField;
$properties['sortOrder'] = $contentType->defaultSortOrder;
}

return new LocationCreateStruct($properties);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions eZ/Publish/Core/SignalSlot/LocationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use eZ\Publish\Core\SignalSlot\Signal\LocationService\UnhideLocationSignal;
use eZ\Publish\Core\SignalSlot\Signal\LocationService\MoveSubtreeSignal;
use eZ\Publish\Core\SignalSlot\Signal\LocationService\DeleteLocationSignal;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;

/**
* LocationService class.
Expand Down Expand Up @@ -350,12 +351,13 @@ public function deleteLocation(Location $location)
* Instantiates a new location create class.
*
* @param mixed $parentLocationId the parent under which the new location should be created
* @param eZ\Publish\API\Repository\Values\ContentType\ContentType|null $contentType
*
* @return \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct
*/
public function newLocationCreateStruct($parentLocationId)
public function newLocationCreateStruct($parentLocationId, ContentType $contentType = null)
{
return $this->service->newLocationCreateStruct($parentLocationId);
return $this->service->newLocationCreateStruct($parentLocationId, $contentType);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion eZ/Publish/Core/SignalSlot/Tests/LocationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function serviceProvider()
),
array(
'newLocationCreateStruct',
array($rootId),
array($rootId, null),
$locationCreateStruct,
0,
),
Expand Down

0 comments on commit 73d0544

Please sign in to comment.