cancel
Showing results for 
Search instead for 
Did you mean: 

Issue Categories on search form magento 2.1

Issue Categories on search form magento 2.1

Hi Guys.
I need some help with add a custom module class inside search field phtml file.
First sorry I am new on magento 2 and I am trying to understand how it's works.

I installed a Theme and I would like to add a select box with categories on the left side of search field.

The theme already have a class with the funcion getStoreCategories() and I am trying to use it.

The theme has the overwrite Magento_Search folder and the file "form.mini.phtml".

Then I am trying to add this funcion on this file. The tree structure is like that:

app/code/Megnor/Category/Block/Html/Leftcategories.php

This file contain this functions:

 

<?php

namespace Megnor\Category\Block\Html;

class Leftcategories extends \Magento\Framework\View\Element\Template
{
protected $_categoryHelper;
protected $categoryFlatConfig;
protected $topMenu;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Helper\Category $categoryHelper,
\Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
\Magento\Theme\Block\Html\Topmenu $topMenu
) {
$this->_categoryHelper = $categoryHelper;
$this->categoryFlatConfig = $categoryFlatState;
$this->topMenu = $topMenu;
parent::__construct($context);
}
public function getHtml()
{
return $this->topMenu->getHtml();
}
/**
* Return categories helper
*/ 
public function getCategoryHelper()
{
return $this->_categoryHelper;
}/**
* Retrieve current store categories
*
* @param bool|string $sorted
* @param bool $asCollection
* @param bool $toLoad
* @return \Magento\Framework\Data\Tree\Node\Collection| 
\Magento\Catalog\Model\Resource\Category\Collection|array
*/ 
public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
{
return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
}
/**
* Retrieve child store categories
*
*/ 
public function getChildCategories($category)
{
if ($this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource()) {
$subcategories = (array)$category->getChildrenNodes();
} else {
$subcategories = $category->getChildren();
}
return $subcategories;
}
}

And this file I am trying to get this function:

app\design\frontend\Megnor\mag110228_2\Magento_Search\templates\form.mini.phtml

 

<?php
/** @var $helper \Magento\Search\Helper\Data */
/** @var $block \Megnor\Category\Block\Html */
$helper = $this->helper('Magento\Search\Helper\Data');
$helper_categories = $this->helper(' Megnor\Category\Helper\Data');
$categories = $helper_categories->getStoreCategories(true,false,true);
print_r($categories);
?>
<div class="block block-search">
<div class="block-search-inner">
<div class="block block-title"><strong><?php /* @escapeNotVerified */ echo __('Search'); ?></strong></div>
<div class="block block-content">
<form class="form minisearch" id="search_mini_form" action="<?php /* @escapeNotVerified */ echo $helper->getResultUrl() ?>" method="get">
<div class="field search">
<div class="box-categories">
<select name="cat">
<option value="all">Todos</option>
</select>
</div>
<label class="label" for="search" data-role="minisearch-label">
<span><?php /* @escapeNotVerified */ echo __('Search'); ?></span>
</label>
<div class="control">
<input id="search"
data-mage-init='{"quickSearch":{
"formSelector":"#search_mini_form",
"url":"<?php /* @escapeNotVerified */ echo $block->getUrl('search/ajax/suggest', ['_secure' => $block->getRequest()->isSecure()]); ?>",
"destinationSelector":"#search_autocomplete"}
}'
type="text"
name="<?php /* @escapeNotVerified */ echo $helper->getQueryParamName() ?>"
value="<?php /* @escapeNotVerified */ echo $helper->getEscapedQueryText() ?>"
placeholder="<?php /* @escapeNotVerified */ echo __('Search Your Products...'); ?>"
class="input-text"
maxlength="<?php /* @escapeNotVerified */ echo $helper->getMaxQueryLength();?>"
role="combobox"
aria-haspopup="false"
aria-autocomplete="both"
autocomplete="off"
aria-expanded= "true"/>
</div>
</div>
<div class="actions">
<button type="submit"
title="<?php echo $block->escapeHtml(__('Search')) ?>"
class="action search">
<span><?php /* @escapeNotVerified */ echo __('Search'); ?></span>
</button>
</div>
</form>
</div>
</div>
</div>

It is possible to do this? If so how can I do?

Thanks