cancel
Showing results for 
Search instead for 
Did you mean: 

How to unset cms block programmatically?

How to unset cms block programmatically?

My code:

<?php

$blockObj= $block->getLayout()->createBlock('Zero\DescBlock\Block\ActiveFilter');

$final_value = 'filter_block_'.$blockObj->getActiveFilter();

$check=array('NULL', '', 'filter_block_');  

if(!in_array( $final_value, $check ))

{
   
    echo $this->getLayout()
    ->createBlock('Magento\Cms\Block\Block')
    ->setBlockId($final_value)
    ->toHtml();
}
else{

    echo $this->getLayout()
    ->createBlock('Magento\Cms\Block\Block')
    ->setBlockId('category_desc_block')
    ->toHtml();
}
?>

How to unset block in else conditions?

2 REPLIES 2

Re: How to unset cms block programmatically?

Hi @Aveeva ,

 

Try below way to unset block from the layout. Use unsetElement() method to remove block.

$layout = $this->getLayout();
$block = $layout->getBlock('catalog.topnav'); // block name
$layout->unsetElement('catalog.topnav');

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!

Re: How to unset cms block programmatically?

Hi @Aveeva,

 

You can delete CMS static block programmatically using deleteById() function of Magento\Cms\Api\BlockRepositoryInterface interface.

 

use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
 
public function __construct(
    BlockRepositoryInterface $blockRepository
) {
    $this->blockRepository = $blockRepository;
}
 
/**
* @param int $blockId
* @return bool
*/
public function deleteCmsBlock(int $blockId)
{
    try {
        $result = $this->blockRepository->deleteById($blockId);
    } catch (LocalizedException $exception) {
        throw $exception->getMessage();
    }
    return $result;
}

Hope this can help you! Let me know if you need further assistance.

__________

If issue solved, Click Kudos & Accept as Solution.

LitExtension - #1 Shopping Cart Migration Expert

LitExtension helps store owners and agencies migrate all important data from one eCommerce platform to another accurately, securely and at the highest speed.

Visit website: http://litextension.com/