Extension Advanced Content Manager 2 pour Magento
Pour Magento 1
Pour Magento 2

Add Content type & Content programmatically

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

In the example below, we explain how to add a ContentType in your code.

First of all, initialize your Magento 2 module structure: http://devdocs.magento.com/guides/v2.1/extension-dev-guide/build/build.html
And don't forget to add Blackbird_ContentManager to your module dependencies.

Now you need to inject these following objects into 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',
];