1

I am trying to overwrite the following Bluefoot content block found in vendor/gene/bluefoot/model/setup/data/pagebuilder_blocks_core.json

{
  "identifier": "button_item",
  "name": "Button",
  "content_type": "block",
  "description": "Single Button",
  "url_key_prefix": null,
  "preview_field": "link_text",
  "renderer": "core_default",
  "item_view_template": "core_button_item",
  "list_template": null,
  "list_item_template": null,
  "item_layout_update_xml": null,
  "list_layout_update_xml": null,
  "singular_name": "Button",
  "plural_name": null,
  "include_in_sitemap": "0",
  "searchable": "0",
  "icon_class": "fa fa-mouse-pointer",
  "color": "#5284bd",
  "show_in_page_builder": "0",
  "sort_order": "0",
  "group": "general",
  "attribute_data": {
    "attributes": [
      "link_text",
      "link_url",
      "css_classes"
    ],
    "groups": [
      {
        "attribute_group_name": "General",
        "sort_order": "1",
        "default_id": "0",
        "attributes": [
          {
            "attribute_code": "link_text",
            "sort_order": "1"
          },
          {
            "attribute_code": "link_url",
            "sort_order": "2"
          }
        ]
      },
      {
        "attribute_group_name": "Advanced",
        "sort_order": "2",
        "default_id": "0",
        "attributes": [
          {
            "attribute_code": "css_classes",
            "sort_order": "1"
          }
        ]
      }
    ]
  }
}

Here is the new json I am trying to add to the DB during install of my module.

{
"content_blocks": [
  {
    "identifier": "button_item",
    "name": "Button",
    "content_type": "block",
    "description": "Single Button",
    "url_key_prefix": null,
    "preview_field": "link_text",
    "renderer": "core_default",
    "item_view_template": "core_button_item",
    "list_template": null,
    "list_item_template": null,
    "item_layout_update_xml": null,
    "list_layout_update_xml": null,
    "singular_name": "Button",
    "plural_name": null,
    "include_in_sitemap": "0",
    "searchable": "0",
    "icon_class": "fa fa-mouse-pointer",
    "color": "#A55A55",
    "show_in_page_builder": "0",
    "sort_order": "0",
    "group": "general",
    "attribute_data": {
      "attributes": [
        "link_text",
        "link_url",
        "button_type",
        "css_classes"
      ],
      "groups": [
        {
          "c": "General",
          "sort_order": "1",
          "default_id": "0",
          "attributes": [
            {
              "attribute_code": "link_text",
              "sort_order": "1"
            },
            {
              "attribute_code": "link_url",
              "sort_order": "2"
            },
            {
              "attribute_code": "button_type",
              "sort_order": "3"
            }
          ]
        },
        {
          "attribute_group_name": "Advanced",
          "sort_order": "2",
          "default_id": "0",
          "attributes": [
            {
              "attribute_code": "css_classes",
              "sort_order": "1"
            }
          ]
        }
      ]
    }
  }
],
"attributes": [
  {
    "attribute_code": "button_type",
    "attribute_model": null,
    "backend_model": null,
    "backend_type": "varchar",
    "backend_table": null,
    "frontend_model": null,
    "frontend_input": "select",
    "frontend_label": ["Button Type"],
    "frontend_class": null,
    "source_model": "eav\/entity_attribute_source_table",
    "is_required": "1",
    "is_user_defined": "1",
    "is_unique": "0",
    "note": null,
    "is_global": "0",
    "is_wysiwyg_enabled": "0",
    "is_visible": "1",
    "content_scope": "0",
    "frontend_input_renderer": null,
    "widget": null,
    "data_model": null,
    "template": null,
    "list_template": null,
    "additional_data": [],
    "entity_allowed_block_type": false,
    "option": {
      "value": {
        "option_0": ["primary"],
        "option_1": ["secondary"],
        "option_2": ["alt"],
        "option_3": ["action"]
      },
      "order": {
        "option_0": "",
        "option_1": "",
        "option_2": "",
        "option_3": ""
      },
      "delete": {
        "option_0": "",
        "option_1": "",
        "option_2": "",
        "option_3": ""
      }
    }
  }
]
}

The new button_type is being inserted into the db correctly, but the json that I want to overwrite the button_item is not working.

I have this install script where I am installing this json file, and I believe I need to delete the old button_item content block from the DB first in order for the new one to work. Here is the install script.

app/code/Vendor/Module/Setup/InstallData.php    

<?php

namespace vendor\module\Setup;

use Gene\BlueFoot\Model\Attribute\ContentBlock as ContentBlock;
use Gene\BlueFoot\Model\Attribute\ContentBlockRepository;
use Gene\BlueFoot\Model\Installer\File as InstallerFile;
use Magento\Framework\Filesystem\Io\File as IoFile;
use Magento\Framework\Module\Dir\Reader;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * Class to install data for list builder BlueFoot block
 */
class InstallData implements InstallDataInterface
{
    /**
     * @var Reader
     */
    protected $moduleReader;

    /**
     * @var IoFile
     */
    protected $ioFile;

    /**
     * @var InstallerFile
     */
    protected $fileInstaller;

    /**
     * @var Bluefoot content block repository manager
     */
    protected $contentBlockRepository;

    /**
     * @var
     */
    protected $attributeFactory;

    protected $contentBlockInterface;

    /**
     * InstallData constructor.
     *
     * @param Reader $moduleReader
     * @param IoFile $ioFile
     * @param InstallerFile $fileInstaller
     */
    public function __construct(
        Reader $moduleReader,
        IoFile $ioFile,
        InstallerFile $fileInstaller,
        ContentBlockRepository $contentBlockRepository
    ) {
        $this->moduleReader = $moduleReader;
        $this->ioFile = $ioFile;
        $this->fileInstaller = $fileInstaller;
        $this->contentBlockRepository = $contentBlockRepository;
    }

    /**
     * Installs data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $buttonItemBlock = $this->contentBlockRepository->getByIdentifier('button_item');
        $this->contentBlockRepository->delete($buttonItemBlock);

        //Install the blocks and attributes
        $this->installData($setup);

        $setup->endSetup();
    }

    /**
     * Install blocks and attributes from specified JSON file
     *
     * @param ModuleDataSetupInterface $setup
     */
    protected function installData(ModuleDataSetupInterface $setup)
    {
        $file = $this->moduleReader->getModuleDir(false, 'Vendor_Module') . DIRECTORY_SEPARATOR . 'Setup' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'install-blocks-attributes.json';

        if ($this->ioFile->fileExists($file)) {
            $this->fileInstaller->install($file, $setup);
        }
    }
}

When I run this I get this error

PHP Fatal error:  Uncaught TypeError: Argument 1 passed to Gene\BlueFoot\Model\Attribute\ContentBlockRepository::delete() must be an instance of Gene\BlueFoot\Api\Data\ContentBlockInterface, instance of Gene\BlueFoot\Model\Attribute\ContentBlock\Interceptor given
1

I solved this by just adding the new attribute type and then adding it to the attribute set of the button_item.

Here is the install code.

<?php

namespace Vendor\Module\Setup;

use Gene\BlueFoot\Setup\EntitySetupFactory;
use Gene\BlueFoot\Model\Installer\File as InstallerFile;
use Gene\BlueFoot\Model\Attribute\ContentBlockRepository;
use Gene\BlueFoot\Api\Data\EntityInterface;
use Magento\Framework\Filesystem\Io\File as IoFile;
use Magento\Framework\Module\Dir\Reader;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * Class to install data for list builder BlueFoot block
 */
class InstallData implements InstallDataInterface
{
    /**
     * @var Reader
     */
    protected $moduleReader;

    /**
     * @var IoFile
     */
    protected $ioFile;

    /**
     * @var InstallerFile
     */
    protected $fileInstaller;

    /**
     * @var ContentBlockRepository
     */
    protected $contentBlockRepository;

    /**
     * @var EntitySetupFactory
     */
    protected $entitiySetupFactory;

    /**
     * InstallData constructor.
     *
     * @param Reader $moduleReader
     * @param IoFile $ioFile
     * @param InstallerFile $fileInstaller
     */
    public function __construct(
        Reader $moduleReader,
        IoFile $ioFile,
        InstallerFile $fileInstaller,
        ContentBlockRepository $contentBlockRepository,
        EntitySetupFactory $entitySetupFactory
    ) { 
        $this->moduleReader = $moduleReader;
        $this->ioFile = $ioFile;
        $this->fileInstaller = $fileInstaller;
        $this->contentBlockRepository = $contentBlockRepository;
        $this->entitiySetupFactory = $entitySetupFactory;
    }

    /**
     * Installs data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        //Install the new blocks and attributes
        $this->installData($setup);

        //Grab the block element button_item so that we know what group to add the button_type to
        $buttonItemBlock = $this->contentBlockRepository->getByIdentifier('button_item');
        $buttonItemBlockData = $buttonItemBlock->getData();

        //Create an eav setup object
        $eavSetup = $this->entitiySetupFactory->create(['setup' => $setup]);
        //Get the button_item group id
        $attrGroupID = $eavSetup->getDefaultAttributeGroupId(EntityInterface::ENTITY, $buttonItemBlockData['attribute_set_id']);
        //Get the button_type information
        $eavAttr = $eavSetup->getAttribute(EntityInterface::ENTITY, 'button_type');
        //Add it to the button_item set
        $eavSetup->addAttributeToSet(
            $eavAttr['entity_type_id'],
            $buttonItemBlockData['attribute_set_id'],
            $attrGroupID,
            $eavAttr['attribute_id'],
            3
        );

        $setup->endSetup();
    }

    /**
     * Install blocks and attributes from specified JSON file
     *
     * @param ModuleDataSetupInterface $setup
     */
    protected function installData(ModuleDataSetupInterface $setup)
    {
        $file = $this->moduleReader->getModuleDir(false, 'Vendor_Module') . DIRECTORY_SEPARATOR . 'Setup' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'install-blocks-attributes.json';

        if ($this->ioFile->fileExists($file)) {
            $this->fileInstaller->install($file, $setup);
        }
    }
}

And my new config file

{
"attributes": [
  {
    "attribute_code": "button_type",
    "attribute_model": null,
    "backend_model": null,
    "backend_type": "varchar",
    "backend_table": null,
    "frontend_model": null,
    "frontend_input": "select",
    "frontend_label": ["Button Type"],
    "frontend_class": null,
    "source_model": "eav\/entity_attribute_source_table",
    "is_required": "1",
    "is_user_defined": "1",
    "is_unique": "0",
    "note": null,
    "is_global": "0",
    "is_wysiwyg_enabled": "0",
    "is_visible": "1",
    "content_scope": "0",
    "frontend_input_renderer": null,
    "widget": null,
    "data_model": null,
    "template": null,
    "list_template": null,
    "additional_data": [],
    "entity_allowed_block_type": false,
    "option": {
      "value": {
        "option_0": ["primary"],
        "option_1": ["secondary"],
        "option_2": ["alt"],
        "option_3": ["action"]
      },
      "order": {
        "option_0": "",
        "option_1": "",
        "option_2": "",
        "option_3": ""
      },
      "delete": {
        "option_0": "",
        "option_1": "",
        "option_2": "",
        "option_3": ""
      }
    }
  }
]
}

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.