0

I've created programmaticaly a Widget. Then I've created the widget in the backend (widget 2 => Marketing).

Based on my user attribute, the widget should switch between 2 and 3 (which I have not created yet).

On my homepage, how to I load the widget based on this attribute ?

See my class :

use Magento\Customer\Model\Session;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Widget\Block\BlockInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Widget\Model\WidgetFactory;
use Portail\Customer\Helper\CustomerUserData;

class Marketing extends Template implements BlockInterface
{

    protected $_template = "widget/marketing.phtml";
    private $categoryRepository;
    private $customerSession;
    private $customerHelper;
    private $widgetCollection;

    public function __construct(
        Context $context,
        \Magento\Catalog\Model\CategoryRepository $categoryRepository,
        Session $customerSession,
        WidgetFactory $widgetFactory,
        array $data = []
    )
    {
        $this->categoryRepository = $categoryRepository;
        $this->customerSession = $customerSession;
        $this->customerHelper = new CustomerUserData();
        $this->widgetCollection = $widgetFactory;
        parent::__construct($context, $data);
    }

    protected function _construct()
    {
        parent::_construct();
        $this->setTemplate($this->_template);
    }


    /**
     * @return array
     */
    public function getAllData()
    {
        $widget = $this->getWidget();
        $data = [];
        $data['marketing'] = [
            'marketing_description' => $this->getData('marketing_description'),
            'logo_marketing' => $this->getData('logo_marketing'),
        ];
        $data['discover'] = [
            'image_bg_marketing' => $this->getData('image_bg_marketing'),
            'title' => $this->getData('title'),
            'content' => $this->getData('content'),
            'link' => $this->getData('link'),
        ];
        return $data;
    }

    public function getWidget() {
        $type = "Portail\Widget\Block\Widget\Marketing";
        $filters = ['type' => "$type", 'select_enterprise' => "XXX"];
        return $this->widgetCollection->create()->getWidgetsArray($filters);
    }
}

This is my widget layout :

<?php /** @var $block \Portail\Widget\Block\Widget\Marketing */ ?>
<?php $data = $block->getAllData();
?>
<div id="marketing" class="container-fluid">
    <div class="container">
        <div class="row">
            <div class="col-12 col-md-6">
                marketing
            </div>
            <div class="col-12 col-md-6">
                données
            </div>
        </div>
    </div>
</div>

and custom_home.xml

<container name="actuPortail" label="actuPortail" htmlTag="div" htmlId="actuPortail" before="containerRecette" htmlClass="container">
    <block class="Magento\Framework\View\Element\Template" template="Portail_Theme::marketing.phtml"/>
</container>

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.