Adding a Subpart

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
swpierce
Posts: 53
Joined: 01 Nov 2015, 13:19

Adding a Subpart

Post by swpierce » 15 Nov 2015, 03:36

I'm wanting to display subcategories as part of the catalog list. Reading through another post, it seems like the best way to do it is to add a subpart, so this is what I did:

In my Laravel project, I created a directory structure as such:

Code: Select all

<root>/ext/subcategories/client/html/src/Client/Html/Catalog/List/Subcategories
<root>/ext/subcategories/client/html/layouts/catalog/list
In the first directory, I put Default.php with all the skeleton code and the following class declaration and _setViewParams (as per the other post I read):

Code: Select all

class Client_Html_Catalog_List_Subcategories_Default
    extends Client_Html_Abstract
    implements Client_Html_Common_Client_Factory_Interface
{

Code: Select all

protected function _setViewParams( MW_View_Interface $view, array &$tags = array(), &$expire = null ) {
        if ( !isset( $this->_cache ) ) {
            $this->addMetaItem( $itemOrItemList, 'catalog', $this->_expire, $this->_tags );
            $this->_addMetaList( $prodid, 'catalog', $this->_expire );
            
            $controller = Controller_Frontend_Factory::createController( $this->_getContext(), 'catalog' );
            $view->subcategoriesList = $controller->getCatalogTree( $view->param( 'f_catid' ), array( 'text', 'media' ), MW_Tree_Manager_Abstract::LEVEL_LIST );
            
            $this->_cache = $view;
        }
        
        $expire = $this->_expires( $this->_expire, $expire );
        $tags = array_merge( $tags, $this->_tags );
        
        return $this->_cache;
    }
In the layouts directory tree, I put subcategories-header-default.html and subcategories-body-default.html

In the shop.php file, I put this configuration:

Code: Select all

'catalog' => array(
     'list' => array(
          'subparts' => [''head', 'quote', 'promo', 'subcategories', 'pagination', 'items', 'pagination' ],
     ),
),
Cleared the cache and reloaded the page but I don't see anything. I'm guessing it's not working due to my inexperience with Laravel but I'm not sure what I'm doing wrong...

User avatar
aimeos
Administrator
Posts: 3097
Joined: 01 Jan 1970, 00:00

Re: Adding a Subpart

Post by aimeos » 15 Nov 2015, 10:52

swpierce wrote:

Code: Select all

protected function _setViewParams( MW_View_Interface $view, array &$tags = array(), &$expire = null ) {
        if ( !isset( $this->_cache ) ) {
            $this->addMetaItem( $itemOrItemList, 'catalog', $this->_expire, $this->_tags );
            $this->_addMetaList( $prodid, 'catalog', $this->_expire );
            
            $controller = Controller_Frontend_Factory::createController( $this->_getContext(), 'catalog' );
            $view->subcategoriesList = $controller->getCatalogTree( $view->param( 'f_catid' ), array( 'text', 'media' ), MW_Tree_Manager_Abstract::LEVEL_LIST );
            
            $this->_cache = $view;
        }
        
        $expire = $this->_expires( $this->_expire, $expire );
        $tags = array_merge( $tags, $this->_tags );
        
        return $this->_cache;
    }
What do you expect as output? At the moment, you assign a catalog item with it's direct children to your view and they contain category texts and pictures (if there are any). You should leave out the "meta" things at the moment to make it easier as the code is currently invalid.
swpierce wrote: In the layouts directory tree, I put subcategories-header-default.html and subcategories-body-default.html
The easiest way to see what's in your view is to do a

Code: Select all

print_r( $this->subcategoriesList );
swpierce wrote: Cleared the cache and reloaded the page but I don't see anything. I'm guessing it's not working due to my inexperience with Laravel but I'm not sure what I'm doing wrong...
If you didn't disable the cache during development (https://github.com/aimeos/aimeos-laravel#hints), you have to run

Code: Select all

php artisan aimeos:cache
after each change.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

swpierce
Posts: 53
Joined: 01 Nov 2015, 13:19

Re: Adding a Subpart

Post by swpierce » 15 Nov 2015, 14:02

Right. In the subcategories-body-default.html file, I basically do the print_r statement like you suggested. I never see it. I have also tried just putting some static HTML in the view instead - that never gets displayed either. Fairly sure the subpart isn't getting called. Thought it might be because I put it in the wrong directory or configured it wrong.

EDIT: I also tried copying my code into the aimeos-core directory tree (even though I know it shouldn't go there) but it still doesn't work.

Here is the full code of the Default.php:

Code: Select all

<?php
class Client_Html_Catalog_List_Subcategories_Default
    extends Client_Html_Abstract
    implements Client_Html_Common_Client_Factory_Interface
{
    private $_subPartPath = 'client/html/catalog/list/sbcategories/default/subparts';
    private $_subPartNames = array();
    private $_cache;
    
    public function getBody( $uid = '', array &$tags = array(), &$expire = null ) {
        $view = $this->_setViewParams($this->getView(), $tags, $expire );
        
        $html = '';
        foreach( $this->_getSubClients() as $subclient ) {
            $html .= $subclient->setView( $view )->getBody( $uid, $tags, $expire );
        }
        $view->subcategoriesBody = $html;
        
        $tplconf = 'client/html/catalog/list/subcategories/default/template-body';
        $default = 'catalog/list/subcategories-body-default.html';
        
        return $view->render( $this->_getTemplate( $tplconf, $default ) );
    }
    
    public function getHeader( $uid = '', array &$tags = array(), &$expire = null ) {
        $view = $this->_setViewParams( $this->getView(), $tags, $expire );
        
        $html = '';
        foreach( $this->_getSubClients() as $subclient ) {
            $html .= $subclient->setView( $view )->getHeader( $uid, $tags, $expire );
        }
        $view->subcategoriesHeader = $html;
        
        $tplconf = 'client/html/catalog/list/subcategories/default/template-header';
        $default = 'catalog/list/subcategories-header-default.html';
        
        return $view->render( $this->_getTemplate( $tplconf, $default ) );
    }
    
    public function getSubClient( $type, $name = null ) {
        return $this->_createSubClient( 'catalog/list/subcategories/' . $type, $name );
    }
    
    public function _getSubClientNames() {
        return $this->_getContext()->getConfig()->get( $this->_subPartPath, $this->_subPartNames );
    }
    
    protected function _setViewParams( MW_View_Interface $view, array &$tags = array(), &$expire = null ) {
            $controller = Controller_Frontend_Factory::createController( $this->_getContext(), 'catalog' );
            $view->subcategoriesList = $controller->getCatalogTree( $view->param( 'f_catid' ), array( 'text', 'media' ), MW_Tree_Manager_Abstract::LEVEL_LIST );
        return $view;
    }
}

?>
Last edited by swpierce on 15 Nov 2015, 14:18, edited 1 time in total.

User avatar
aimeos
Administrator
Posts: 3097
Joined: 01 Jan 1970, 00:00

Re: Adding a Subpart

Post by aimeos » 15 Nov 2015, 14:06

swpierce wrote:Right. In the subcategories-body-default.html file, I basically do the print_r statement like you suggested. I never see it. I have also tried just putting some static HTML in the view instead - that never gets displayed either. Fairly sure the subpart isn't getting called. Thought it might be because I put it in the wrong directory or configured it wrong.
How does your getBody() method look like? There's the location of the template specified. For a working example this may help:
https://aimeos.org/docs/Developers/Html ... Body.28.29
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

swpierce
Posts: 53
Joined: 01 Nov 2015, 13:19

Re: Adding a Subpart

Post by swpierce » 15 Nov 2015, 15:01

Here is my getBody() method:

Code: Select all

public function getBody( $uid = '', array &$tags = array(), &$expire = null ) {
        $view = $this->_setViewParams($this->getView(), $tags, $expire );
        
        $html = '';
        foreach( $this->_getSubClients() as $subclient ) {
            $html .= $subclient->setView( $view )->getBody( $uid, $tags, $expire );
        }
        $view->subcategoriesBody = $html;
        
        $tplconf = 'client/html/catalog/list/subcategories/default/template-body';
        $default = 'catalog/list/subcategories-body-default.html';
        
        return $view->render( $this->_getTemplate( $tplconf, $default ) );
    }
I put the full code of the Default.php file in my previous post.

I do not believe my subpart is getting used at all. I intentionally added some bad code to Default.php but nothing happens - site works when it should break if it is calling my subpart. I have content cache disabled and I ran aimeos:cache anyway just in case.

User avatar
aimeos
Administrator
Posts: 3097
Joined: 01 Jan 1970, 00:00

Re: Adding a Subpart

Post by aimeos » 15 Nov 2015, 15:12

swpierce wrote: In the shop.php file, I put this configuration:

Code: Select all

'catalog' => array(
     'list' => array(
          'subparts' => [''head', 'quote', 'promo', 'subcategories', 'pagination', 'items', 'pagination' ],
     ),
),
If it's not found, most likely this is at the wrong place ("default" is missing in your case). It must be in

Code: Select all

return array(
  ...
  'client' => array(
    'html' => array(
      ...
      'catalog' => array(
        ...
        'list' =>array(
          'default' => array(
            'subparts' => [''head', 'quote', 'promo', 'subcategories', 'pagination', 'items', 'pagination' ],
          ),
        ),
        ...
      ),
      ...
    ),
  ),
  ...
);
Here's the documentation for the config setting: https://aimeos.org/docs/Configuration/C ... t/subparts
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

swpierce
Posts: 53
Joined: 01 Nov 2015, 13:19

Re: Adding a Subpart

Post by swpierce » 15 Nov 2015, 15:23

I knew it was something simple I was overlooking! Thanks!

One last question on this topic:

If I put the code in the ext directory tree, I get:

Code: Select all

Class "Client_Html_Catalog_List_Subcategories_Default" not available
If I put the code in the aimeos-core directory tree, it works.

Shouldn't I avoid putting it in the aimeos-core tree to prevent issues during upgrades? If so, where should I put the code if the ext directory tree is the wrong place. OR, if the ext directory is the right place, is there something I should do to get the code to load from there?

Thanks again for all the help!

User avatar
aimeos
Administrator
Posts: 3097
Joined: 01 Jan 1970, 00:00

Re: Adding a Subpart

Post by aimeos » 15 Nov 2015, 15:36

I'm almost sure this is an autoloader issue. Maybe it helps to run

Code: Select all

composer dump-autoload
in your application directory.

If not, you may try to add the "client/html/src" directory to the "autoload"/"classmap" section of your composer.json file.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

swpierce
Posts: 53
Joined: 01 Nov 2015, 13:19

Re: Adding a Subpart

Post by swpierce » 15 Nov 2015, 16:37

Adding 'ext' to the autoload classmap worked in getting the class to load. but if I leave stuff in the ext directory tree, it is unable to find the templates:

Code: Select all

Template "catalog/list/subcategories-body-default.html" not available

User avatar
aimeos
Administrator
Posts: 3097
Joined: 01 Jan 1970, 00:00

Re: Adding a Subpart

Post by aimeos » 15 Nov 2015, 16:52

The template directory is configured in the manifest.php file of your extension and it should contain these lines by default:

Code: Select all

'custom' => array(
	...
	'client/html' => array(
		'client/html/layouts',
	),
	...
),
Your template must then be in .../ext/client/html/layouts/catalog/list/subcategories-body-default.html
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply