Pimcore\Model\Element\Service::getSaveCopyName PHP Method

getSaveCopyName() public static method

Returns a uniqe key for the element in the $target-Path (recursive)
public static getSaveCopyName ( string $type, string $sourceKey, Pimcore\Model\Element\ElementInterface $target ) : Pimcore\Model\Element\ElementInterface | string
$type string
$sourceKey string
$target Pimcore\Model\Element\ElementInterface
return Pimcore\Model\Element\ElementInterface | string
    public static function getSaveCopyName($type, $sourceKey, $target)
    {
        if (self::pathExists($target->getRealFullPath() . "/" . $sourceKey, $type)) {
            // only for assets: add the prefix _copy before the file extension (if exist) not after to that source.jpg will be source_copy.jpg and not source.jpg_copy
            if ($type == "asset" && ($fileExtension = File::getFileExtension($sourceKey))) {
                $sourceKey = str_replace("." . $fileExtension, "_copy." . $fileExtension, $sourceKey);
            } else {
                $sourceKey .= "_copy";
            }
            return self::getSaveCopyName($type, $sourceKey, $target);
        }
        return $sourceKey;
    }

Usage Example

Example #1
0
 /**
  * @param  AbstractObject $target
  * @param  AbstractObject $source
  * @return AbstractObject copied object
  */
 public function copyAsChild($target, $source)
 {
     //load properties
     $source->getProperties();
     //load all in case of lazy loading fields
     self::loadAllObjectFields($source);
     $new = clone $source;
     $new->o_id = null;
     $new->setChilds(null);
     $new->setKey(Element\Service::getSaveCopyName("object", $new->getKey(), $target));
     $new->setParentId($target->getId());
     $new->setUserOwner($this->_user->getId());
     $new->setUserModification($this->_user->getId());
     $new->setDao(null);
     $new->setLocked(false);
     $new->setCreationDate(time());
     $new->save();
     // $this->updateChilds($target, $new);
     return $new;
 }
All Usage Examples Of Pimcore\Model\Element\Service::getSaveCopyName