Skip to content

Commit

Permalink
Ticket 600: Adding link tags dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
blackcoder87 committed Apr 20, 2019
1 parent 69b1ff8 commit 997d138
Show file tree
Hide file tree
Showing 3 changed files with 332 additions and 1 deletion.
54 changes: 54 additions & 0 deletions application/libraries/Ilch/Layout/Frontend.php
Expand Up @@ -138,6 +138,53 @@ public function getMetaTagString($key)
}
}

/**
* Get the link tag as string.
*
* @param $key
* @return string
*/
public function getLinkTagString($key)
{
$linkTagModel = $this->get('linkTags')[$key];

// If the rel attribute is absent, has no keywords, or if none of the keywords used are allowed according
// to the definitions in this specification, then the element does not create any links.
// The href attribute must be present and must contain a valid non-empty URL potentially surrounded by spaces.
// If the href attribute is absent, then the element does not define a link.
if (empty($linkTagModel->getRel()) || empty($linkTagModel->getHref())) {
return "";
}

$linkTagString = sprintf('<link rel="%s" href="%s"', $this->escape($linkTagModel->getRel()), $this->escape($linkTagModel->getHref()));

if ($linkTagModel->getCrossorigin()) {
$linkTagString = $linkTagString . sprintf(' crossorigin="%s"', $this->escape($linkTagModel->getCrossorigin()));
}

if ($linkTagModel->getHreflang()) {
$linkTagString = $linkTagString . sprintf(' hreflang="%s"', $this->escape($linkTagModel->getHreflang()));
}

if ($linkTagModel->getSizes()) {
$linkTagString = $linkTagString . sprintf(' sizes="%s"', $this->escape($linkTagModel->getSizes()));
}

if ($linkTagModel->getType()) {
$linkTagString = $linkTagString . sprintf(' type="%s"', $this->escape($linkTagModel->getType()));
}

if ($linkTagModel->getMedia()) {
$linkTagString = $linkTagString . sprintf(' media="%s"', $this->escape($linkTagModel->getMedia()));
}

if ($linkTagModel->getTitle()) {
$linkTagString = $linkTagString . sprintf(' title="%s"', $this->escape($linkTagModel->getTitle()));
}

return $linkTagString . '>';
}

/**
* Get key from config.
*
Expand Down Expand Up @@ -211,6 +258,13 @@ public function getHeader()
}
}

if (is_array($this->get('linkTags'))) {
foreach ($this->get('linkTags') as $key => $linkTag) {
$html .= '
'.$this->getLinkTagString($key);
}
}

$html .= '
<link rel="apple-touch-icon" href="'.$this->getBaseUrl($this->escape($this->getAppleIcon())).'">
<link href="'.$this->getVendorUrl('fortawesome/font-awesome/css/all.min.css').'" rel="stylesheet">
Expand Down
277 changes: 277 additions & 0 deletions application/libraries/Ilch/Layout/Helper/LinkTag/Model.php
@@ -0,0 +1,277 @@
<?php
/**
* @copyright Ilch 2.0
* @package ilch
*/

namespace Ilch\Layout\Helper\LinkTag;

use Ilch\Layout\Base as Layout;

/**
* A model for a link tag.
*
* @see https://www.w3.org/TR/html50/document-metadata.html#the-link-element
*/
class Model
{
/**
* The href of the link tag.
* Address of the hyperlink
* Must be present and must contain a valid non-empty URL potentially surrounded by spaces.
*
* @see https://www.w3.org/TR/html50/document-metadata.html#attr-link-href
* @var string
*/
protected $href;

/**
* The crossorigin of the link tag.
* How the element handles crossorigin requests
*
* @see https://www.w3.org/TR/html50/document-metadata.html#attr-link-crossorigin
* @var string
*/
protected $crossorigin;

/**
* The rel of the link tag.
* Relationship between the document containing the hyperlink and the destination resource
* A link element must have a rel attribute.
*
* @see https://www.w3.org/TR/html50/document-metadata.html#attr-link-rel
* @var string
*/
protected $rel;

/**
* The media of the link tag.
* Applicable media
*
* @see https://www.w3.org/TR/html50/document-metadata.html#attr-link-media
* @var string
*/
protected $media;

/**
* The hreflang of the link tag.
* Language of the linked resource
*
* @see https://www.w3.org/TR/html50/document-metadata.html#attr-link-hreflang
* @var string
*/
protected $hreflang;

/**
* The type of the link tag.
* Hint for the type of the referenced resource
* The type attribute gives the MIME type of the linked resource. It is purely advisory.
* The value must be a valid MIME type.
*
* @see https://www.w3.org/TR/html50/document-metadata.html#attr-link-type
* @var string
*/
protected $type;

/**
* The sizes of the link tag.
* Sizes of the icons (for rel="icon")
*
* @see https://www.w3.org/TR/html50/links.html#attr-link-sizes
* @var string
*/
protected $sizes;

/**
* The title of the link tag.
* title attribute has special semantics on this element: Title of the link; alternative style sheet set name.
*
* @see https://www.w3.org/TR/html50/document-metadata.html#attr-link-title
* @var string
*/
protected $title;

/**
* Set href for the link tag.
*
* @param string $href
* @return Model
*/
public function setHref($href)
{
$this->href = $href;
return $this;
}

/**
* Get href for the link tag.
* @return string
*/
public function getHref()
{
return $this->href;
}

/**
* Set crossorigin for the link tag.
* Example: anonymous, use-credentials, ...
*
* @param string $crossorigin
* @return Model
*/
public function setCrossorigin($crossorigin)
{
$this->crossorigin = $crossorigin;
return $this;
}

/**
* Get crossorigin for the link tag.
*
* @return string
*/
public function getCrossorigin()
{
return $this->crossorigin;
}

/**
* Set rel for the link tag.
* Example: alternate, stylesheet, ...
*
* @param string $rel
* @return Model
*/
public function setRel($rel)
{
$this->rel = $rel;
return $this;
}

/**
* Get rel for the link tag.
*
* @return string
*/
public function getRel()
{
return $this->rel;
}

/**
* Set the media for the link tag.
* The value must be a valid media query.
* Example: print
*
* @param string $media
* @return Model
*/
public function setMedia($media)
{
$this->media = $media;
return $this;
}

/**
* Get the media for the link tag.
*
* @return string
*/
public function getMedia()
{
return $this->media;
}

/**
* Set hreflang for the link tag.
* The value must be a valid BCP 47 language tag.
* Example: en, fr, ...
*
* @param string $hreflang
* @return Model
*/
public function setHreflang($hreflang)
{
$this->hreflang = $hreflang;
return $this;
}

/**
* Get hreflang for the link tag.
*
* @return string
*/
public function getHreflang()
{
return $this->hreflang;
}

/**
* Set the type for the link tag.
* The value must be a valid MIME type.
* Example: text/html, application/pdf, ...
*
* @param string $type
* @return Model
*/
public function setType($type)
{
$this->type = $type;
return $this;
}

/**
* Get the type for the link tag.
*
* @return string
*/
public function getType()
{
return $this->type;
}

/**
* Set sizes for the link tag.
* Example: 16x16, 32x32, any, ...
*
* @param string $sizes
* @return Model
*/
public function setSizes($sizes)
{
$this->sizes = $sizes;
return $this;
}

/**
* Get sizes for the link tag.
*
* @return string
*/
public function getSizes()
{
return $this->sizes;
}

/**
* Set title for the link tag.
*
* @param string $title
* @return Model
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}

/**
* Get title for the link tag.
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
2 changes: 1 addition & 1 deletion application/libraries/Ilch/Layout/Helper/MetaTag/Model.php
Expand Up @@ -146,4 +146,4 @@ public function setCharset($charset)

return $this;
}
}
}

0 comments on commit 997d138

Please sign in to comment.