3232use Pimcore \Bundle \StudioBackendBundle \DataObject \Util \Trait \ValidateObjectDataTrait ;
3333use Pimcore \Bundle \StudioBackendBundle \Element \Service \ElementSaveServiceInterface ;
3434use Pimcore \Bundle \StudioBackendBundle \Exception \Api \DatabaseException ;
35+ use Pimcore \Bundle \StudioBackendBundle \Exception \Api \ElementExistsException ;
3536use Pimcore \Bundle \StudioBackendBundle \Exception \Api \ElementSavingFailedException ;
3637use Pimcore \Bundle \StudioBackendBundle \Exception \Api \ForbiddenException ;
3738use Pimcore \Bundle \StudioBackendBundle \Exception \Api \InvalidElementTypeException ;
4748use Pimcore \Bundle \StudioBackendBundle \Util \Constant \ElementPermissions ;
4849use Pimcore \Bundle \StudioBackendBundle \Util \Constant \ElementTypes ;
4950use Pimcore \Bundle \StudioBackendBundle \Util \Constant \HttpResponseCodes ;
51+ use Pimcore \Bundle \StudioBackendBundle \Util \Constant \HttpResponseErrorKeys ;
5052use Pimcore \Bundle \StudioBackendBundle \Util \Trait \ElementProviderTrait ;
5153use Pimcore \Bundle \StudioBackendBundle \Util \Trait \UserPermissionTrait ;
5254use Pimcore \Model \DataObject \AbstractObject ;
@@ -89,6 +91,13 @@ public function __construct(
8991 ) {
9092 }
9193
94+ public function getDataObjectFullPath (
95+ string $ parentFullPath ,
96+ string $ key
97+ ): string {
98+ return str_ends_with ($ parentFullPath , '/ ' ) === true ? $ parentFullPath . $ key : $ parentFullPath . '/ ' . $ key ;
99+ }
100+
92101 /**
93102 * @throws DatabaseException
94103 * @throws ElementSavingFailedException
@@ -103,8 +112,15 @@ public function addDataObject(
103112 ): int {
104113 $ user = $ this ->securityService ->getCurrentUser ();
105114 $ parent = $ this ->getValidParent ($ user , $ parentId );
106- if ($ this ->dataObjectServiceResolver ->pathExists ($ parent ->getFullPath () . '/ ' . $ parameters ->getKey ())) {
107- throw new ElementSavingFailedException (null , 'Element with the same key and path already exists ' );
115+ $ fullPath = $ this ->getDataObjectFullPath (
116+ $ parent ->getFullPath (),
117+ $ parameters ->getKey ()
118+ );
119+ if ($ this ->dataObjectServiceResolver ->pathExists ($ fullPath )) {
120+ throw new ElementExistsException (
121+ $ fullPath ,
122+ HttpResponseErrorKeys::ELEMENT_EXISTS ->value
123+ );
108124 }
109125
110126 $ class = $ this ->getValidClass ($ this ->classDefinitionResolver , $ parameters ->getClassId ());
0 commit comments