0

My code looks like this:

/**
 * @var \Magento\Cms\Model\BlockFactory
 */
private $_blockFactory;

...

$this->_blockFactory
        ->setStoreId(0)
        ->load('some-identifier', 'identifier')
        ->setData([
            'title' => 'Some Title',
            'identifier' => 'some-identifier',
            'is_active' => 1,
            'stores' => [0],
            'content' => 'Some Content...',
        ])->save();

Everytime I run this code, it creates a new static block with the same identifier.


Any idea?

0

Don't use factory for existing blocks. You should use the repository: \Magento\Cms\Api\BlockRepositoryInterface

<?php

use Magento\Framework\App\Bootstrap;
require './app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

$searchCriteriaBuilder = $obj->create('Magento\Framework\Api\SearchCriteriaBuilder');
/** @var \Magento\Cms\Api\BlockRepositoryInterface $blockRepository */
$blockRepository = $obj->create('Magento\Cms\Api\BlockRepositoryInterface');
$searchCriteria = $searchCriteriaBuilder->addFilter(
    'identifier',
    'home-page-block',
    'eq'
)->create();
/** @var \Magento\Cms\Model\Block $item */
$items = $blockRepository->getList($searchCriteria)->getItems();
reset($items);
$item = $items[key($items)];
echo $item->getTitle();
$item->setTitle($item->getTitle() . '_1');
$blockRepository->save($item);
echo $item->getTitle();

This works for me

UPDATE In case if you need "insert or update if exists" use this code:

<?php

use Magento\Framework\App\Bootstrap;
require './app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

/** @var \Magento\Cms\Api\BlockRepositoryInterface $blockRepository */
$blockRepository = $obj->create(Magento\Cms\Api\BlockRepositoryInterface::class);
/** @var \Magento\Cms\Model\BlockFactory $blockFactory */
$blockFactory = $obj->create(Magento\Cms\Model\BlockFactory::class);
$searchCriteriaBuilder = $obj->create(Magento\Framework\Api\SearchCriteriaBuilder::class);

$searchCriteria = $searchCriteriaBuilder->addFilter(
    'identifier',
    'home-page-block',
    'eq'
)->create();
/** @var \Magento\Cms\Model\Block $item */
$items = $blockRepository->getList($searchCriteria)->getItems();
if (count($items)) {
    reset($items);
    $block = $items[key($items)];
} else {
    $block = $blockFactory->create();
}
$block->setTitle($item->getTitle() . '_1');
$blockRepository->save($item);
  • Will it also work if the block doesn't exist yet? – Pini Apr 11 '18 at 12:33
  • Of course not, you asked about update code. – Stepan Furman Apr 11 '18 at 14:59
  • I updated my answer for you. Now it inserts new if not exists, and update if exists – Stepan Furman Apr 11 '18 at 15:10
  • You are welcome. Please let me know if it's not fit for you target. – Stepan Furman Apr 11 '18 at 15:34
  • @Pini how is it going along? – Stepan Furman Apr 12 '18 at 20:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.