Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 40141c83 authored by Nicolas's avatar Nicolas
Browse files

Premier commit du module

parents
No related branches found
No related tags found
No related merge requests found
# Created by https://www.gitignore.io/api/macos
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# End of https://www.gitignore.io/api/macos
<?php
namespace NicolasBejean\CategoryWidget\Block\Widget;
class CategoryWidget extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{
protected $_template = "widget/category_widget.phtml";
/*protected $_template;*/
/**
* Default value for products count that will be shown
*/
protected $_categoryHelper;
protected $categoryFlatConfig;
protected $topMenu;
protected $_categoryFactory;
protected $widgetTitle;
protected $widgetContent;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Catalog\Helper\Category $categoryHelper
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Helper\Category $categoryHelper,
\Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\Theme\Block\Html\Topmenu $topMenu
) {
$this->_categoryHelper = $categoryHelper;
$this->categoryFlatConfig = $categoryFlatState;
$this->topMenu = $topMenu;
$this->_categoryFactory = $categoryFactory;
parent::__construct($context);
}
/**
* Return categories helper
*/
public function getCategoryHelper()
{
return $this->_categoryHelper;
}
public function getCategoryModel($id)
{
$_category = $this->_categoryFactory->create();
$_category->load($id);
return $_category;
}
/**
* 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
*/
/**
* Récupère le titre du widget
*/
public function getWidgetTitle()
{
$widgetTitle = $this->getData('widget_title');
return $widgetTitle;
}
/**
* Récupère le contenu du widget
*/
public function getWidgetContent()
{
$widgetContent = $this->getData('widget_content');
return $widgetContent;
}
/**
* Récupère la sélection de catégories
*/
public function getCategorySelection()
{
$rootCat = $this->getData('category_selection');
$category = $this->_categoryFactory->create();
$collection = $category
->getCollection()
->addAttributeToSelect('image')
->addIdFilter($rootCat);
return $collection;
}
/**
* Récupère le template du widget
*/
public function getModel()
{
return $this->getData('model');
}
}
\ No newline at end of file
<?php
namespace NicolasBejean\CategoryWidget\Model\Config\Source;
use Magento\Framework\Option\ArrayInterface;
use Magento\Catalog\Helper\Category;
class CategoryList implements ArrayInterface
{
protected $_categoryHelper;
public function __construct(\Magento\Catalog\Helper\Category $catalogCategory)
{
$this->_categoryHelper = $catalogCategory;
}
/*
* Return categories helper
*/
public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
{
return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
}
/*
* Option getter
* @return array
*/
public function toOptionArray()
{
$arr = $this->toArray();
$ret = [];
foreach ($arr as $key => $value)
{
$ret[] = [
'value' => $key,
'label' => $value
];
}
return $ret;
}
/*
* Get options in "key-value" format
* @return array
*/
public function toArray()
{
$categories = $this->getStoreCategories(true,false,true);
$catagoryList = array();
foreach ($categories as $category){
$catagoryList[$category->getEntityId()] = __($category->getName());
}
return $catagoryList;
}
}
?>
\ No newline at end of file
# CATEGORY WIDGET
Permet d'afficher une catégorie ou une liste de catégories avec leur image.
## To Do
- Il faut pouvoir éviter d'avoir un foreach si il n'y a qu'une seule catégorie
- Il faut pouvoir afficher n fois la même catégorie
- Pouvoir sélectionner un template de rendu > Traité mais c'est pas fait correctement
## Variables
- widget_title (setWidgetTitle) : Optionnel : Titre H2 du widget
- widget_content (setWidgetContent) : Optionnel : Contenu texte servant à insérer une description dans le widget
- category_selection (setCategorySelection): Obligatoire : ID de la catégorie à afficher. Pour en afficher plusieurs 3,6
- model (setModel) : Obligatoire : Permet de sélectionner le template de rendu
## Exemples d'intégration
Dans un fichier layout (XML) :
```xml
<block class="NicolasBejean\CategoryWidget\Block\Widget\CategoryWidget">
<arguments>
<argument name="category_selection" xsi:type="string">3</argument>
<argument name="model" xsi:type="string">widget/category_widget_img_background.phtml</argument>
</arguments>
</block>
```
Dans un fichier de template (PHTML) :
```php
<?php
echo $this ->getLayout()
->createBlock("NicolasBejean\CategoryWidget\Block\Widget\CategoryWidget")
->setCategorySelection("6")
->setModel("widget/category_widget.phtml")
->toHtml();
?>
```
Dans le backoffice :
```html
{{widget type="NicolasBejean\CategoryWidget\Block\Widget\CategoryWidget" widget_title="titre" widget_content="tag" category_selection="3" model="widget/category_widget_img_background.phtml"}}
```
## Structure LESS
Le fichier LESS doit être créé dans le dossier Category_Widget/web/css/source/_categoryWidget
```less
.category-widget {
.content-heading {
h2.title {}
p.content {}
}
.categories {
a.category {
img {}
span {}
}
}
}
```
## Source
https://github.com/Programmingatkstark/Magento-2-Category-with-image-widget
\ No newline at end of file
{
"name": "NicolasBejean/CategoryWidget",
"description": "Afficher une liste de catégories avec leur photo",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0"
},
"type": "magento2-module",
"version": "1.0.0",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
]
}
}
\ No newline at end of file
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="NicolasBejean_CategoryWidget" setup_version="1.0.0" />
</config>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
<widget id="nicolasbejean_categorywidget" class="NicolasBejean\CategoryWidget\Block\Widget\CategoryWidget">
<label translate="true">widget_label</label>
<description translate="true">widget_description</description>
<parameters>
<parameter name="widget_title" xsi:type="text" visible="true" sort_order="1">
<label translate="true">title</label>
</parameter>
<parameter name="widget_content" xsi:type="text" visible="true" sort_order="2">
<label translate="true">content</label>
</parameter>
<parameter name="category_selection" xsi:type="multiselect" visible="true" sort_order="3" source_model="NicolasBejean\CategoryWidget\Model\Config\Source\CategoryList">
<label translate="true">category_selection</label>
<description translate="true">category_selection_description</description>
</parameter>
<parameter name="model" xsi:type="select" visible="true">
<label translate="true">template</label>
<options>
<option name="default" value="widget/category_widget.phtml" selected="true">
<label translate="true">default_template</label>
</option>
<option name="img_background" value="widget/category_widget_img_background.phtml">
<label translate="true">other_template</label>
</option>
</options>
</parameter>
</parameters>
</widget>
</widgets>
"widget_label", "Widget"
"widget_description", "CategoryWidget Description"
"widget_title", "Widget Title"
"widget_content", "Widget Content"
"category_selection", "Select categories"
"category_selection_description", "Each categories must have an image"
"template", "Template"
"default_template", "Default Template"
"other_template", "Template with backgrounded image"
"no_model", "WidgetCategory : Please define a model."
\ No newline at end of file
"widget_label", "Widget : Liste de catégories avec leur image"
"widget_description", "Description du widget CategoryWidget"
"title", "Titre du widget"
"content", "Contenu du widget"
"category_selection", "Sélectionner les catégories"
"category_selection_description", "Chaque catégorie doit avoir une image"
"template", "Modèle"
"default_template", "Modèle par défaut"
"other_template", "Modèle avec les images en arrière-plan du lien"
"no_model", "WidgetCategory : Veuillez définir un modèle."
\ No newline at end of file
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'NicolasBejean_CategoryWidget',
__DIR__
);
\ No newline at end of file
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head />
</page>
\ No newline at end of file
<?php
$categories = $this->getCategorySelection();
$categoryHelper = $this->getCategoryHelper();
?>
<div class="widget block block-static-block category-widget">
<div class="block-content">
<?php if($this->getModel() == "widget/category_widget.phtml") { ?>
<?php if($this->getWidgetTitle() != null && $this->getWidgetContent() != null): ?>
<div class="content-heading">
<?php if($this->getWidgetTitle() != null): ?>
<h2 class="title">
<?php echo $this->getWidgetTitle(); ?>
</h2>
<?php endif; ?>
<?php if($this->getWidgetContent() != null): ?>
<p class="content">
<?php echo $this->getWidgetContent(); ?>
</p>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if($categories != null): ?>
<div class="categories">
<?php
foreach($categories as $category):
$cat = $this->getCategoryModel($category->getId());
if($cat->getImageUrl() != null): ?>
<a class="category" href="<?php echo $cat->getUrl(); ?>">
<img src="<?php echo $cat->getImageUrl(); ?>" class="img-responsive category_image" alt="<?php echo $cat->getName(); ?>" />
<span><?php echo $cat->getName(); ?></span>
</a>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php } elseif ($this->getModel("widget/category_widget_img_background.phtml")) { ?>
<?php if($categories != null): ?>
<div class="categories">
<?php
foreach($categories as $category):
$cat = $this->getCategoryModel($category->getId());
if($cat->getImageUrl() != null): ?>
<a class="category img-background" href="<?php echo $cat->getUrl(); ?>" style="background-image: url(<?php echo $cat->getImageUrl(); ?>)">
<span><?php echo $cat->getName(); ?></span>
</a>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php } else { ?>
<p class="message global demo">
<?php echo __('no_model'); ?>
</p>
<?php } ?>
</div>
</div>
<?php
$categories = $this->getCategorySelection();
$categoryHelper = $this->getCategoryHelper();
?>
<div class="widget block block-static-block category-widget">
<div class="block-content">
<?php if($categories != null): ?>
<div class="categories">
<?php
foreach($categories as $category):
$cat = $this->getCategoryModel($category->getId());
$image = $cat->getImageUrl();
if($image != null): ?>
<a class="category" href="<?php echo $cat->getUrl(); ?>" style="background-image: url(\"<?php echo $image; ?>\")">
<span><?php echo $cat->getName(); ?></span>
</a>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</div>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment