How to filter CMS Page(Block) content in template file using magento 2?

Sometimes we have defined a native static variable in Static Block in Magento 2 and we need to call that block in template file at that time static variable will not be interpreted,
Like Custom variable {{config path=”web/unsecure/base_url”}} in static block. using below method content will be displayed the dynamic value of static block variables.
We need to use, \Magento\Cms\Model\Template\FilterProvider class to filter static content in Blocks.

<?php
public function __construct(
    \Magento\Cms\Model\Template\FilterProvider $filter
) {
    $this->filter = $filter;
}
/*
* Static block $content
*/
public function getContentFromStaticBlock($content)
{
    return $this->filter->getBlockFilter()->filter($content);
}

Call from the template file,

$content = "YOUR_STATIC_BLOCK_CONTENT";
echo $this->getContentFromStaticBlock($content);

The result will be displayed filterable static variable value with your content.

How to create custom tabs in product page magento 2?

Let’s create a product tab in Magento 2 product page using the simple module. We want to display short content for a product using product tabs.

Example, We want to display short description info in product tabs.
Start with the Simple module in Magento 2, create registration.php file to register our module.

Path, app/code/Rbj/ProductTabs/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Rbj_ProductTabs',
    __DIR__
);

File path, app/code/Rbj/ProductTabs/etc/module.xml

<?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="Rbj_ProductTabs" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

We have passed dependency of Catalog module in sequence tag of our module.
Now We want to create catalog_product_view.xml file for display our custom tabs of the short description.
Create a layout file,
Path, app/code/Rbj/ProductTabs/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.info.details">
        	<!-- For short description tab -->
            <block class="Rbj\ProductTabs\Block\ExtraInfo" name="product.short.description" template="Rbj_ProductTabs::short-descripiton.phtml" group="detailed_info">
                <arguments>
                    <argument name="title" translate="true" xsi:type="string">Extra Info</argument>
                </arguments>
            </block>
   		</referenceBlock>
   	</body>
</page>

We have keep our tab name is Extra Info if you want to change tabs title you can replace Extra info with your title in argument tag.  In the above layout file, We must need to pass group=”detailed_info”  in the block class.

Create template file for call short description content in tabs using template, Path, app/code/Rbj/ProductTabs/view/frontend/templates/short-descripiton.phtml

<?php $extraInfo = $block->getTabsContent(); ?>

<div class="short">
    <?php echo $extraInfo; ?>
</div>

We need to create ExtraInfo.php file to check if a product has short description we need to display tabs otherwise we don’t need to display empty tabs for the short description.

Path, app/code/Rbj/ProductTabs/Block/ExtraInfo.php

<?php

namespace Rbj\ProductTabs\Block;

class ExtraInfo extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
         array $data = []
    ) {
        $this->_coreRegistry = $context->getRegistry();
        parent::__construct($context, $data);
    }

    /**
     * Retrieve current product object
     *
     * @return \Magento\Catalog\Model\Product
     */
    public function getProduct()
    {
        if (!$this->hasData('product')) {
            $this->setData('product', $this->_coreRegistry->registry('product'));
        }
        return $this->getData('product');
    }

    /**
     * @return string
     */
    public function getTabsContent()
    {
        return $this->getProduct()->getShortDescription();
    }

    /**
     * @return return HTML
     */
    protected function _toHtml()
    {
        if(!empty($this->getTabsContent())) {
            return parent::_toHtml();
        }

        return false;
    }
}

Now run command to activate our module,

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush

You can see new tabs on the product page in the tabs section below the image section. Your new tab will be displayed if your product has the short description otherwise tabs will not display in product page.
You can add new attribute details using the above way.

 

How to create modal popup in magento 2 using Magento_Ui/js/modal/modal?

How to create a Popup Modal using Javascript in Magento 2?

Create a .phtml template file with the given content to make Modal popup using Magento 2,

<div class="main-block">
    <div class="content">
    	<a href="javascript:void(0)" id="chart"><?php echo __('Chart size');?></a>
    </div>
    <!-- Your Popup content with main div display none -->
    <div id="popup-chart" style="display:none;">
        YOUR POPUP CONTENT GOES HERE
    </div>
</div>
<script>
require(
        [
            'jquery',
            'Magento_Ui/js/modal/modal',
            'domReady!'
        ],
        function($, modal) {
            var options = {
                type: 'popup',
                responsive: true,
                innerScroll: true,
                buttons: []
            };

            modal(options, $('#modal-form'));
        }
);
</script>

In the above template file, we can show a popup based on clicking on the chart size. When click on the chart link defined in our DOM element popup modal will be displayed.

Call Magento_Ui/js/modal/modal js file as a dependency.

There are many default options are available for modal.js file.

Different types of options you can provide as per your requirement in above options object.

List of default options for a modal popup, if you want to override the default value set options value in your custom template in the options object.

    type: 'popup',
    title: '',
    subTitle: '',
    modalClass: '',
    focus: '[data-role="closeBtn"]',
    autoOpen: false,
    clickableOverlay: true,
    popupTpl: popupTpl,
    slideTpl: slideTpl,
    customTpl: customTpl,
    modalVisibleClass: '_show',
    parentModalClass: '_has-modal',
    innerScrollClass: '_inner-scroll',
    responsive: false,
    innerScroll: false,
    modalTitle: '[data-role="title"]',
    modalSubTitle: '[data-role="subTitle"]',
    modalBlock: '[data-role="modal"]',
    modalCloseBtn: '[data-role="closeBtn"]',
    modalContent: '[data-role="content"]',
    modalAction: '[data-role="action"]',
    focusableScope: '[data-role="focusable-scope"]',
    focusableStart: '[data-role="focusable-start"]',
    focusableEnd: '[data-role="focusable-end"]',
    appendTo: 'body',
    wrapperClass: 'modals-wrapper',
    overlayClass: 'modals-overlay',
    responsiveClass: 'modal-slide',
    trigger: '',
    modalLeftMargin: 45,
    closeText: $.mage.__('Close'),
    buttons: [{
        text: $.mage.__('Ok'),
        class: '',
        attr: {},

        /**
         * Default action on button click
         */
        click: function (event) {
            this.closeModal(event);
        }
    }],