Pimcore\Model\Document\Tag\Link::frontend PHP Method

frontend() public method

See also: Document\Tag\TagInterface::frontend
public frontend ( ) : string
return string
    public function frontend()
    {
        $url = $this->getHref();
        if (strlen($url) > 0) {
            // add attributes to link
            $attribs = [];
            if (is_array($this->options)) {
                foreach ($this->options as $key => $value) {
                    if (is_string($value) || is_numeric($value)) {
                        $attribs[] = $key . '="' . $value . '"';
                    }
                }
            }
            // add attributes to link
            $allowedAttributes = ["charset", "coords", "hreflang", "name", "rel", "rev", "shape", "target", "accesskey", "class", "dir", "id", "lang", "style", "tabindex", "title", "xml:lang", "onblur", "onclick", "ondblclick", "onfocus", "onmousedown", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onkeydown", "onkeypress", "onkeyup"];
            $defaultAttributes = [];
            if (!is_array($this->options)) {
                $this->options = [];
            }
            if (!is_array($this->data)) {
                $this->data = [];
            }
            $availableAttribs = array_merge($defaultAttributes, $this->data, $this->options);
            foreach ($availableAttribs as $key => $value) {
                if ((is_string($value) || is_numeric($value)) && in_array($key, $allowedAttributes)) {
                    if (!empty($value)) {
                        $attribs[] = $key . '="' . $value . '"';
                    }
                }
            }
            $attribs = array_unique($attribs);
            if (array_key_exists("attributes", $this->data) && !empty($this->data["attributes"])) {
                $attribs[] = $this->data["attributes"];
            }
            return '<a href="' . $url . '" ' . implode(" ", $attribs) . '>' . htmlspecialchars($this->data["text"]) . '</a>';
        }
        return "";
    }

Usage Example

Example #1
0
 /**
  * Automatically inject bootstrap classes to the link
  * @return string
  */
 public function frontend()
 {
     $html = parent::frontend();
     $matches = array();
     if (preg_match('/^<a.*?class=[\'"](.*?)[\'"]/i', $html, $matches)) {
         $classes = explode(' ', $matches[1]);
     } else {
         $classes = array();
     }
     // base button class
     if (!in_array('btn', $classes)) {
         $classes[] = 'btn';
     }
     // button style
     if (isset($this->data['buttonStyle'])) {
         $styleClass = 'btn-' . strtolower($this->data['buttonStyle']);
         if (!in_array($styleClass, $classes)) {
             $classes[] = $styleClass;
         }
     }
     // button size
     if (isset($this->data['buttonSize']) && array_key_exists($this->data['buttonSize'], $this->sizes)) {
         $sizeClass = $this->sizes[$this->data['buttonSize']];
         if (!empty($sizeClass) && !in_array($sizeClass, $classes)) {
             $classes[] = $sizeClass;
         }
     }
     // button active
     if (isset($this->data['buttonActive']) && (bool) $this->data['buttonActive'] && !in_array('active', $classes)) {
         $classes[] = 'active';
     }
     // button disabled
     if (isset($this->data['buttonDisabled']) && (bool) $this->data['buttonDisabled'] && !in_array('disabled', $classes)) {
         $classes[] = 'disabled';
     }
     // make the replacement
     $classAttr = implode(' ', $classes);
     if (empty($matches)) {
         $html = str_replace('<a ', '<a class="' . $classAttr . '" ', $html);
     } else {
         $html = preg_replace('/^<a(.*?)class=[\'"](.*?)[\'"]/i', "<a\$1class=\"{$classAttr}\"", $html);
     }
     return $html;
 }