phpimagezend-frameworksitemap

Image Sitemap with Zend_Navigation


I generate a Sitemap using Zend_Navigation and i wanted to add images to this sitemap, now i got no Idea how to get this done, i use following (working) code to generate the sitemap

foreach($sitemapItems as $item)
    {
        $newSite = new Zend_Navigation_Page_Uri();
        $newSite->uri = 'http://' . $_SERVER['HTTP_HOST'] . $item->getSpeakingUrl();
        $newSite->lastmod = $item->getUpdatedAt();
        $newSite->changefreq = 'weekly';

        $this->_navigation->addPage($newSite);
    }

    $this->view->navigation($this->_navigation);
    $this->view->navigation()->sitemap()->setFormatOutput(true);

The Output is as follows:

<?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        <url>
            <loc>http://test.dev/pictures/site-28.html</loc>
            <lastmod>2010-03-11T17:47:30+01:00</lastmod>
            <changefreq>weekly</changefreq>
         </url>
         ....

I need the following Output inside the Url part

<image:image>
    <image:loc>http://example.com/image.jpg</image:loc>
</image:image> 

i tried to just set

$newSite->image = URI

but it didnt work also i tried to add custom attribute via

$newSite->__set('image', array('loc' => URI));

Does anyone know if what i want is even possible ? i cant find anything in the docs or web...

Thanks for your time, Dominik


Solution

  • Oki so first what you need to do is extend Zend_Navigation_Page_Uri and add you're image var to it smth like below :

        class Mylib_NavPageUriImage extends Zend_Navigation_Page_Uri
    {
        protected $_image = null;
    
        public function setImage($image)
        {
            if (null !== $image && !is_string($image)) {
                require_once 'Zend/Navigation/Exception.php';
                throw new Zend_Navigation_Exception(
                        'Invalid argument: $image must be a string or null');
            }
    
            $this->_image = $image;
            return $this;
        }
    
        public function getImage()
        {
            return $this->_image;
        }
    
        public function toArray()
        {
            return array_merge(
                parent::toArray(),
                array(
                    'image' => $this->getImage()
                ));
        }
    }
    

    Add this class to library/Mylib/NavPageUriImage.php.

    To make it usable you need to register the namespace ( i like to register my namespaces at bootstrap but it can be done from app.ini too ) so in you're bootstrap class add the following :

    function _initNamespace()
        {
            $autoloader = Zend_Loader_Autoloader::getInstance();
            $autoloader->registerNamespace('Mylib_');
        }
    

    Then in you're controller you can now use :

    $newSite = new Mylib_NavPageUriImage();
    $newSite->uri = 'http://' . $_SERVER['HTTP_HOST'] . $item->getSpeakingUrl();
    $newSite->lastmod = $item->getUpdatedAt();
    $newSite->changefreq = 'weekly';
    $newSite->image = 'some image';
    

    THE FOLLOWING IS NOT RECOMENDED , YOU NEED TO EXTEND YOU'RE OWN NAVIGATION HELPER AND USE THAT ( i just don't have the time to play with it now ) ALLSO ADD YOU'RE OWN imageValidator

    And then in library/zend/view/helper/navigation/sitemap.php add the following lines ( under the add priority element if statement , mine ends at 443 so i added this at 444 ) :

    // add 'image' element if a valid image is set in page
    if (isset($page->image)) {
        $image = $page->image;
            $imgDom = $dom->createElementNS(self::SITEMAP_NS, 'image:image');
            $imgDom->appendChild($dom->createElementNS(self::SITEMAP_NS, 'image:loc', $image));
        $urlNode->appendChild($imgDom);
    }