Skip to content

Commit

Permalink
Add features from N98 layout helper (#1051)
Browse files Browse the repository at this point in the history
  • Loading branch information
sreichel committed Mar 12, 2021
1 parent 89a7f2b commit 57563fc
Showing 1 changed file with 83 additions and 15 deletions.
98 changes: 83 additions & 15 deletions app/code/core/Mage/Page/Block/Html/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ protected function _construct()
*
* @param string $name
* @param string $params
* @param string $referenceName
* @param bool $before
* @return $this
*/
public function addCss($name, $params = "")
public function addCss($name, $params = "", $referenceName = "*", $before = null)
{
$this->addItem('skin_css', $name, $params);
$this->addItem('skin_css', $name, $params, null, null, $referenceName, $before);
return $this;
}

Expand All @@ -66,11 +68,13 @@ public function addCss($name, $params = "")
*
* @param string $name
* @param string $params
* @param string $referenceName
* @param bool $before
* @return $this
*/
public function addJs($name, $params = "")
public function addJs($name, $params = "", $referenceName = "*", $before = null)
{
$this->addItem('js', $name, $params);
$this->addItem('js', $name, $params, null, null, $referenceName, $before);
return $this;
}

Expand All @@ -79,11 +83,13 @@ public function addJs($name, $params = "")
*
* @param string $name
* @param string $params
* @param string $referenceName
* @param bool $before
* @return $this
*/
public function addCssIe($name, $params = "")
public function addCssIe($name, $params = "", $referenceName = "*", $before = null)
{
$this->addItem('skin_css', $name, $params, 'IE');
$this->addItem('skin_css', $name, $params, 'IE', null, $referenceName, $before);
return $this;
}

Expand All @@ -92,11 +98,13 @@ public function addCssIe($name, $params = "")
*
* @param string $name
* @param string $params
* @param string $referenceName
* @param bool $before
* @return $this
*/
public function addJsIe($name, $params = "")
public function addJsIe($name, $params = "", $referenceName = "*", $before = null)
{
$this->addItem('js', $name, $params, 'IE');
$this->addItem('js', $name, $params, 'IE', null, $referenceName, $before);
return $this;
}

Expand Down Expand Up @@ -128,20 +136,41 @@ public function addLinkRel($rel, $href)
* @param string $params
* @param string $if
* @param string $cond
* @param string $referenceName name of the item to insert the element before. If name is not found, insert at the end, * has special meaning (before all / before all)
* @param bool $before If true insert before the $referenceName instead of after
* @return $this
*/
public function addItem($type, $name, $params = null, $if = null, $cond = null)
public function addItem($type, $name, $params = null, $if = null, $cond = null, $referenceName = "*", $before = false)
{
if ($type==='skin_css' && empty($params)) {
// allow skipping of parameters in the layout XML files via empty-string
if ($params === '') {
$params = null;
}
if ($if === '') {
$if = null;
}
if ($cond === '') {
$cond = null;
}

if ($type === 'skin_css' && empty($params)) {
$params = 'media="all"';
}
$this->_data['items'][$type.'/'.$name] = array(
'type' => $type,
'name' => $name,
$this->_data['items'][$type . '/' . $name] = array(
'type' => $type,
'name' => $name,
'params' => $params,
'if' => $if,
'cond' => $cond,
'if' => $if,
'cond' => $cond,
);

// that is the standard behaviour
if ($referenceName === '*' && $before === false) {
return $this;
}

$this->_sortItems($referenceName, $before, $type);

return $this;
}

Expand Down Expand Up @@ -536,4 +565,43 @@ protected function _isFile($filename)
}
return is_file($filename);
}

/**
* @param string $referenceName
* @param string $before
* @param string $type
*/
protected function _sortItems($referenceName, $before, $type)
{
$items = $this->_data['items'];

// get newly inserted item so we do not have to reproduce the functionality of the parent
end($items);
$newKey = key($items);
$newVal = array_pop($items);

$newItems = array();

if ($referenceName === '*' && $before === true) {
$newItems[$newKey] = $newVal;
}

$referenceName = $type . '/' . $referenceName;
foreach ($items as $key => $value) {
if ($key === $referenceName && $before === true) {
$newItems[$newKey] = $newVal;
}

$newItems[$key] = $value;

if ($key === $referenceName && $before === false) {
$newItems[$newKey] = $newVal;
}
}

// replace items only if the reference was found (otherwise insert as last item)
if (isset($newItems[$newKey])) {
$this->_data['items'] = $newItems;
}
}
}

0 comments on commit 57563fc

Please sign in to comment.