Skip to content

Instantly share code, notes, and snippets.

@denistrator
Created October 25, 2019 13:27
Show Gist options
  • Save denistrator/fae4d4a0741290a08a493fa9c4fd4d6e to your computer and use it in GitHub Desktop.
Save denistrator/fae4d4a0741290a08a493fa9c4fd4d6e to your computer and use it in GitHub Desktop.
Custom Cms Block Product Tab
<?php
/**
* Copyright
*/
namespace Vendor\Module\Block;
use Magento\Cms\Model\ResourceModel\Block\CollectionFactory;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template;
use Magento\Store\Model\StoreManagerInterface;
class CustomProductInfoTab extends Template
{
const CUSTOM_PRODUCT_INFO_TAB_CMS_BLOCK_ID = 'product_info_custom_tab';
/**
* @var StoreManagerInterface $storeManager
*/
protected $storeManager;
/**
* @var CollectionFactory
*/
protected $entityCollectionFactory;
public function __construct(
Template\Context $context,
StoreManagerInterface $storeManager,
CollectionFactory $collectionFactory,
array $data = []
) {
$this->storeManager = $storeManager;
$this->entityCollectionFactory = $collectionFactory;
parent::__construct($context, $data);
}
/**
* Object data getter
*
* If $key is not defined will return all the data as an array.
* Otherwise it will return value of the element specified by $key.
* It is possible to use keys like a/b/c for access nested array data
*
* If $index is specified it will assume that attribute data is an array
* and retrieve corresponding member. If data is the string - it will be explode
* by new line character and converted to array.
*
* @param string $key
* @param string|int $index
* @return mixed
*/
public function getData($key = '', $index = null)
{
if ($key === 'title' && $customCmsBlockTitle = $this->getCustomCmsBlockTitle()) {
return $customCmsBlockTitle;
}
return parent::getData($key, $index);
}
/**
* @return string|null
*/
public function getCustomCmsBlockTitle()
{
$block = $this->getCustomCmsBlock();
if (!$block || !$block->getData('is_active')) {
return null;
}
return $block->getData('title');
}
/**
* @return DataObject|null
*/
public function getCustomCmsBlock()
{
$entityCollection = $this
->entityCollectionFactory->create()
->addFieldToFilter('identifier', self::CUSTOM_PRODUCT_INFO_TAB_CMS_BLOCK_ID)
->addStoreFilter($this->getStoreId());
return new DataObject($entityCollection->getData()[0]);
}
/**
* @return int
*/
public function getStoreId()
{
$storeId = 0;
try {
$storeId = $this->storeManager->getStore()->getId();
} catch (NoSuchEntityException $e) {
}
return $storeId;
}
}
<?xml version="1.0"?>
<!--
/**
* Copyright
-->
<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">
<block name="product.info.custom.tab" class="Vendor\Module\Block\CustomProductInfoTab"
template="Magento_Theme::html/container.phtml" group="detailed_info" after="-">
<block class="Magento\Cms\Block\Block" name="product.info.custom.tab.cms.block">
<arguments>
<argument name="block_id" xsi:type="string">product_info_custom_tab</argument>
</arguments>
</block>
</block>
</referenceBlock>
</body>
</page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment