Pimcore\Model\Document\PageSnippet::getElement PHP Method

getElement() public method

Get an element with the given key/name
public getElement ( string $name ) : Tag
$name string
return Tag
    public function getElement($name)
    {
        $elements = $this->getElements();
        if ($this->hasElement($name)) {
            return $elements[$name];
        } else {
            if (array_key_exists($name, $this->inheritedElements)) {
                return $this->inheritedElements[$name];
            }
            // check for content master document (inherit data)
            if ($contentMasterDocument = $this->getContentMasterDocument()) {
                if ($contentMasterDocument instanceof Document\PageSnippet) {
                    $inheritedElement = $contentMasterDocument->getElement($name);
                    if ($inheritedElement) {
                        $inheritedElement = clone $inheritedElement;
                        $inheritedElement->setInherited(true);
                        $this->inheritedElements[$name] = $inheritedElement;
                        return $inheritedElement;
                    }
                }
            }
        }
        return null;
    }

Usage Example

Beispiel #1
0
 /**
  * @param string $name
  * @return Model\Document\Tag
  */
 public function getElement($name)
 {
     // check if a persona is requested for this page, if yes deliver a different version of the element (prefixed)
     if ($this->getUsePersona()) {
         $personaName = $this->getPersonaElementName($name);
         if ($this->hasElement($personaName)) {
             $name = $personaName;
         } else {
             // if there's no dedicated content for this persona, inherit from the "original" content (unprefixed)
             // and mark it as inherited so it is clear in the ui that the content is not specific to the selected persona
             // replace all occurrences of the persona prefix, this is needed because of block-prefixes
             $inheritedName = str_replace($this->getPersonaElementPrefix(), "", $name);
             $inheritedElement = parent::getElement($inheritedName);
             if ($inheritedElement) {
                 $inheritedElement = clone $inheritedElement;
                 $inheritedElement->setResource(null);
                 $inheritedElement->setName($personaName);
                 $inheritedElement->setInherited(true);
                 $this->setElement($personaName, $inheritedElement);
                 return $inheritedElement;
             }
         }
     }
     // delegate to default
     return parent::getElement($name);
 }