ZF-5435: offsetSetStylesheet: not correct order in headLink view helper

Description

in the headlink view helper one can use the offsetSetStylesheet method to set certain indexes of the inside ArrayObject-type container,

but when you want to put out the stylesheets the order of output is not the one defined by index, but by insertion time order.

You can fix this by adding


$this->getContainer()->asort();

into the toString method of Zend/View/Helper/HeadLink but I don't know, if this is the best solution or if the issue lies a bit more under the surface (maybe the iterator of the ArrayObject doesn't work as expected.

One can help oneself by calling asort before calling the toString-method in the view script


$this->headStyle()->getContainer()->asort();
echo $this->headStyle();

The same should apply to headScript, headMeta and headStyle

Comments

Assigning to Ralph.

I confirm that this is still an issue in 1.7.2.

$this->view->headLink()->offsetSetStylesheet(1,$someStylesheet,$media); $this->view->headLink()->offsetSetStylesheet(10,$anotherStylesheet,$media); $this->view->headLink()->offsetSetStylesheet(20,$yetAnotherStylesheet,$media); $this->view->headLink()->offsetSetStylesheet(5,$insertedStylesheet,$media);

will render the style sheets to the output in order of insertion, not the offset order

Adding the line

$this->getContainer()->ksort();

(not asort() as previously stated)

prior to line 310 - foreach ($this as $item) {

fixes the problem

Sorry - should have given full script ref.

Script to amend is Zend_View_Helper_HeadLink.php

Affected function is

public function toString($indent = null)
{
    $indent = (null !== $indent)
            ? $this->getWhitespace($indent)
            : $this->getIndent();

    $items = array();
    $this->getContainer()->ksort(); //added by AK to fix bug ZF-5435
    foreach ($this as $item) {
        $items[] = $this->itemToString($item);
    }

    return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
}

This bug also affected the other view headerView Helpers so i rolled them all into one patch.

Fix with r13995 and move into 1.7 release branch with r13996