Pimcore\Model\Document\Tag::buildTagName PHP Méthode

buildTagName() public static méthode

public static buildTagName ( $type, $name, null $document = null ) : string
$type
$name
$document null
Résultat string
    public static function buildTagName($type, $name, $document = null)
    {
        if (!preg_match("@^[a-zA-Z0-9\\-_]+\$@", $name)) {
            throw new \Exception("Only valid CSS class selectors are allowed as the name for an editable (which is basically [a-zA-Z0-9\\-_]+), your name was: " . $name);
        }
        // check for persona content
        if ($document && $document instanceof Document\Page && $document->getUsePersona()) {
            $name = $document->getPersonaElementName($name);
        }
        // @todo add document-id to registry key | for example for embeded snippets
        // set suffixes if the tag is inside a block
        if (\Zend_Registry::isRegistered("pimcore_tag_block_current")) {
            $blocks = \Zend_Registry::get("pimcore_tag_block_current");
            $numeration = \Zend_Registry::get("pimcore_tag_block_numeration");
            if (is_array($blocks) and count($blocks) > 0) {
                if ($type == "block") {
                    $tmpBlocks = $blocks;
                    $tmpNumeration = $numeration;
                    array_pop($tmpBlocks);
                    array_pop($tmpNumeration);
                    $tmpName = $name;
                    if (is_array($tmpBlocks)) {
                        $tmpName = $name . implode("_", $tmpBlocks) . implode("_", $tmpNumeration);
                    }
                    if ($blocks[count($blocks) - 1] == $tmpName) {
                        array_pop($blocks);
                        array_pop($numeration);
                    }
                }
                $name = $name . implode("_", $blocks) . implode("_", $numeration);
            }
        }
        if (strlen($name) > 750) {
            throw new \Exception("Composite name is longer than 750 characters - use shorter names for your editables or reduce amount of nesting levels. Name is: " . $name);
        }
        return $name;
    }

Usage Example

Exemple #1
0
 /**
  * @param $type
  * @param $realName
  * @param array $options
  * @return Model\Document\Tag
  */
 public function tag($type, $realName, $options = array())
 {
     $type = strtolower($type);
     $document = $this->document;
     $name = Model\Document\Tag::buildTagName($type, $realName, $document);
     try {
         if ($document instanceof Model\Document\PageSnippet) {
             $tag = $document->getElement($name);
             if ($tag instanceof Model\Document\Tag && $tag->getType() == $type) {
                 // call the load() method if it exists to reinitialize the data (eg. from serializing, ...)
                 if (method_exists($tag, "load")) {
                     $tag->load();
                 }
                 // set view & controller, editmode
                 $tag->setController($this->controller);
                 $tag->setView($this);
                 $tag->setEditmode($this->editmode);
                 $tag->setOptions($options);
             } else {
                 $tag = Model\Document\Tag::factory($type, $name, $document->getId(), $options, $this->controller, $this, $this->editmode);
                 $document->setElement($name, $tag);
             }
             // set the real name of this editable, without the prefixes and suffixes from blocks and areablocks
             $tag->setRealName($realName);
         }
         return $tag;
     } catch (\Exception $e) {
         \Logger::warning($e);
     }
 }