Advanced Content Manager 2
For Magento 1
For Magento 2

Add ContentType in the setup data file

Maybe you need for your project to add some ContentType programmatically. Actually you can add ContentType, Content and ContentList programmatically, anywhere you want in your code.


In the example below, we explain how to add a ContentType while installing/upgrading your module.


Firstly, initialize you Magento 2 module structure: http://devdocs.magento.com/guides/v2.1/extension-dev-guide/build/build.html


Think to add Blackbird_ContentManager to your module dependencies.


Then add one of these classes, regarding what you need:


First install of the module:


<MyVendor>\<MyModule>\Setup\InstallData

Must implements \Magento\Framework\Setup\InstallDataInterface


Upgrade of the module:


<MyVendor>\<MyModule>\Setup\UpgradeData

Must implements \Magento\Framework\Setup\UpgradeDataInterface


For the version management, please see the references mentioned at <Magento2Root>/setup/src/Magento/Setup


Now you need to inject these following objects in your class:


If you want to create a ContentType:


\Blackbird\ContentManager\Api\Data\ContentTypeInterfaceFactory

\Blackbird\ContentManager\Api\Data\ContentType\CustomFieldsetInterfacefactory

          // Create and initialize the ContentType
          $contentType = $this->_contentTypeFactory->create()
              ->setData($data)
              ->save();
          // Add some CustomFields
          $customFieldset = $this->_customFieldsetFactory->create()
              ->setData(['title' => 'General', 'sort_order' => 1, 'ct_id' => $contentType->getCtId()])
              ->save();
          foreach ($customFields as $customField) {
              $customField['fieldset_id'] = $customFieldset->getId();
              $contentType->addCustomField($customField);
          }
          $contentType->saveCustomFields();

Where $data and $customFields are:


  // The fully list of data is not available yet
  $data = [
              'title' => 'My New ContentType',
              'identifier' => 'my_new_contenttype',
              'default_status' => 0,
              'description' => 'My New Content Type description',
              'default_url' => 'Add ContentType in the setup data file'
              'breadcrumb' => null,
              'sitemap_enable' => 0,
              'sitemap_frequency' => always,
              'sitemap_priority' => 0,
              'search_enabled' => 0,
  ];
  // See the fully list of CustomField types in the documentation page
  $customFields = [
                  [
                      'type' => 'field',
                      'is_require' => 1,
                      'sort_order' => 0,
                      'identifier' => 'my_new_contenttype_label',
                      'title' => 'Label',
                      'show_in_grid' => 0,
                      'note' => 'Explain what this custom field is used for.',
                      'default_value' => 'My Label',
                      'max_characters' => 0
                  ],
                  [
                      'type' => image,
                      'is_require' => 1,
                      'sort_order' => 1,
                      'identifier' => 'my_new_contenttype_image',
                      'title' => 'Image',
                      'show_in_grid' => 1,
                      'note' => 'Explain what this custom field is used for.',
                      'crop' => 0,
                      'crop_w' => 0,
                      'crop_h' => 0,
                      'keep_aspect_ratio'' => 0,
                      'file_path' => 0,
                      'img_alt' => 1,
                      'img_title' => 0,
                      'img_url' => 0,
                      'file_extension' => 0,
                  ]
  ];

If you want to create a Content:

\Blackbird\ContentManager\Api\Data\ContentInterfaceFactory

  // Create a new Content
  $this->_contentFactory->create()
                      ->setData($contentData)
                      ->save();

Where $contentData is an associative array. Each key of the array should match an identifier of a CustomField of the ContentType.

The required data are:


  $contentData = [
              'ct_id' => $contentType->getId(),
              'status' => 1,
              'store_id' => 0,
              'title' => 'My Content Title',
              'url_key' => 'my-content-url-key',
  ];