I'm following the exercises of "Magento2 development cookbook". In the part of "Adding a block of new products" (previously to that all works fine). I have:
app/code/Packt/HelloWorld/view/frontend/layout/helloworld_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<title>Helloworld Landingspage</title>
</head>
<body>
<remove name="wishlist_sidebar" /> <!-- THIS DOESN'T WORKS -->
<referenceContainer name="content">
<block class="Packt\HelloWorld\Block\Landingspage" name="landingsblock" template="Packt_HelloWorld::landingspage.phtml" /> <!-- THIS WORKS -->
<block class="Packt\HelloWorld\Block\Newproducts" name="newproducts" template="Packt_HelloWorld::newproducts.phtml" /><!-- THIS DOESN'T WORKS -->
</referenceContainer>
</body>
app/code/Packt/HelloWorld/Block/Newproducts.php
<?php
namespace Packt\HelloWorld\Block;
use Magento\Framework\View\Element\Template;
class Newproducts extends Template
{
private $_productCollectionFactory;
protected $_logger;
public function __construct(
Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
array $data = [])
{
parent::__construct($context, $data);
$this->_logger = $context->getLogger();
$this->_productCollectionFactory = $productCollectionFactory;
}
public function getProducts() {
$collection = $this->_productCollectionFactory->create();
$this->_logger->info('YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY');
//$this->_logger->info();
$collection
->addAttributeToSelect('*')
->setOrder('created_at')
->setPageSize(5);
return $collection;
}
}
app/code/Packt/HelloWorld/view/frontend/templates/newproducts.php
<h2>New Products</h2>
<ul>
<?php foreach ($block->getProducts() as $product): ?>
<li><?php echo $product->getName() ?></li>
<?php endforeach; ?>
</ul>
New Products?var/view_preprocessed?