Skip to main content
Added additional option for catching already existing blocks
Source Link

We can try:

$cmsBlock = $this->blockInterfaceFactory->create();

$copyrightBlock = $cmsBlock->load('copy-right','identifier');
//And then check

if (!$copyrightBlock->getId()) {
 ......
}

Take a look: vendor/magento/module-cms/Setup/InstallData.php

$footerLinksBlock = $this->createPage()->load('footer_links', 'identifier');

[EDIT]

Seem that load method will be deprecated in the future.

[EDIT - Regarding load method being marked deprecated]

The Magento\Cms\Model\BlockRepository::save() method does throw an \Exception (type: Magento\Framework\Exception\CouldNotSaveException), so you could wrap it in a try/catch to skip over blocks that already exist.

try {
    $this->blockRepository->save($cmsBlock);
} catch (\Exception $e) {
    // Do nothing, block likely already exists
}

We can try:

$cmsBlock = $this->blockInterfaceFactory->create();

$copyrightBlock = $cmsBlock->load('copy-right','identifier');
//And then check

if (!$copyrightBlock->getId()) {
 ......
}

Take a look: vendor/magento/module-cms/Setup/InstallData.php

$footerLinksBlock = $this->createPage()->load('footer_links', 'identifier');

[EDIT]

Seem that load method will be deprecated in the future.

We can try:

$cmsBlock = $this->blockInterfaceFactory->create();

$copyrightBlock = $cmsBlock->load('copy-right','identifier');
//And then check

if (!$copyrightBlock->getId()) {
 ......
}

Take a look: vendor/magento/module-cms/Setup/InstallData.php

$footerLinksBlock = $this->createPage()->load('footer_links', 'identifier');

[EDIT]

Seem that load method will be deprecated in the future.

[EDIT - Regarding load method being marked deprecated]

The Magento\Cms\Model\BlockRepository::save() method does throw an \Exception (type: Magento\Framework\Exception\CouldNotSaveException), so you could wrap it in a try/catch to skip over blocks that already exist.

try {
    $this->blockRepository->save($cmsBlock);
} catch (\Exception $e) {
    // Do nothing, block likely already exists
}
added 1 character in body
Source Link
Khoa TruongDinh
  • 32.3k
  • 11
  • 90
  • 156

We can try:

$cmsBlock = $this->blockInterfaceFactory->create();

$copyrightBlock = $cmsBlock->load('copy-right','identifier');
//And then check

if (!$copyrightBlock->getId()) {
 ......
}

Take a look: vendor/magento/module-cms/Setup/InstallData.php

$footerLinksBlock = $this->createPage()->load('footer_links', 'identifier');

[EDIT]

Seem that load method will be deprecated in the future.

We can try:

$cmsBlock = $this->blockInterfaceFactory->create();

$copyrightBlock = $cmsBlock->load('copy-right','identifier');
//And then check

if ($copyrightBlock->getId()) {
 ......
}

Take a look: vendor/magento/module-cms/Setup/InstallData.php

$footerLinksBlock = $this->createPage()->load('footer_links', 'identifier');

[EDIT]

Seem that load method will be deprecated in the future.

We can try:

$cmsBlock = $this->blockInterfaceFactory->create();

$copyrightBlock = $cmsBlock->load('copy-right','identifier');
//And then check

if (!$copyrightBlock->getId()) {
 ......
}

Take a look: vendor/magento/module-cms/Setup/InstallData.php

$footerLinksBlock = $this->createPage()->load('footer_links', 'identifier');

[EDIT]

Seem that load method will be deprecated in the future.

Source Link
Khoa TruongDinh
  • 32.3k
  • 11
  • 90
  • 156

We can try:

$cmsBlock = $this->blockInterfaceFactory->create();

$copyrightBlock = $cmsBlock->load('copy-right','identifier');
//And then check

if ($copyrightBlock->getId()) {
 ......
}

Take a look: vendor/magento/module-cms/Setup/InstallData.php

$footerLinksBlock = $this->createPage()->load('footer_links', 'identifier');

[EDIT]

Seem that load method will be deprecated in the future.