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

getValidKey() public static method

public static getValidKey ( $key, null $type ) : mixed | string
$key
$type null
return mixed | string
    public static function getValidKey($key, $type)
    {
        $results = \Pimcore::getEventManager()->trigger("system.service.preGetValidKey", null, ["key" => $key, "type" => $type]);
        if ($results->count()) {
            $key = $results->last();
        }
        $key = \Pimcore\Tool\Transliteration::toASCII($key);
        if ($type == "document") {
            // no spaces for documents / clean URLs
            $key = preg_replace('/[^a-zA-Z0-9\\-\\.~_]+/', '-', $key);
        } else {
            // assets & objects including spaces
            $key = preg_replace('/[^a-zA-Z0-9\\-\\.~_ ]+/', '-', $key);
        }
        if ($type == "asset") {
            // keys shouldn't start with a "." (=hidden file) *nix operating systems
            // keys shouldn't end with a "." - Windows issue: filesystem API trims automatically . at the end of a folder name (no warning ... et al)
            $key = trim($key, ". ");
        } else {
            $key = trim($key);
            $key = ltrim($key, ".");
        }
        return $key;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * @param string $name
  * @return $this|void
  * @throws DAV\Exception\Forbidden
  * @throws \Exception
  */
 public function setName($name)
 {
     if ($this->asset->isAllowed("rename")) {
         $user = AdminTool::getCurrentUser();
         $this->asset->setUserModification($user->getId());
         $this->asset->setFilename(Element\Service::getValidKey($name), "asset");
         $this->asset->save();
     } else {
         throw new DAV\Exception\Forbidden();
     }
     return $this;
 }
All Usage Examples Of Pimcore\Model\Element\Service::getValidKey