(1) first create a new module with a custom eav for your product content (this is just the InstallData.php
content):
<?php namespace <vendor>\<module_name>\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
/* assign object to class global variable for use in other class methods */
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'my_attribute',
[
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'my custom content',
'input' => 'textarea',
'class' => '',
'source' => '',
'global' => 1,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => 0,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'wysiwyg_enabled' => true, // if you want a wysiwyg editor
'unique' => false,
'apply_to' => 'simple,configurable,virtual,bundle,downloadable,grouped',
'group' => 'General'
]
);
$setup->endSetup();
}
}
(2) in the module extend catalog_product_view.xml
to insert your attribute:
[I added mine as a new product tab]
<referenceBlock name="product.info.details">
<block class="Magento\Catalog\Block\Product\View\Description" name="{SOME_NAME}" template="product/view/attribute.phtml" group="detailed_info">
<arguments>
<argument name="at_call" xsi:type="string">getMyAttribute</argument>
<argument name="at_code" xsi:type="string">my_attribute</argument>
<argument name="css_class" xsi:type="string">{some class if you like}</argument>
<argument name="at_label" xsi:type="string">none</argument>
<argument name="title" translate="true" xsi:type="string">My tab label</argument>
</arguments>
</block>
</referenceBlock>
(3) now you can go to your magento admin and insert custom content (even html) into the attribute content like for every other attribute
For adding image field, you can try using plugin and always try to avoid overwrite whole class.
Vendor/Module/etc/adminhtml/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\User\Block\User\Edit\Tab\Main">
<plugin name="sr_stackexchange_user_form" type="Vendor\Module\Plugin\Block\Adminhtml\User\Edit\Tab\Main" sortOrder="1"/>
</type>
</config>
Vendor/Module/Plugin/Block/Adminhtml/User/Edit/Tab/Main.php
namespace Vendor\Module\Plugin\Block\Adminhtml\User\Edit\Tab;
class Main
{
/**
* Get form HTML
*
* @return string
*/
public function aroundGetFormHtml(
\Magento\User\Block\User\Edit\Tab\Main $subject,
\Closure $proceed
)
{
$form = $subject->getForm();
if (is_object($form)) {
$fieldset = $form->addFieldset('admin_user_image', ['legend' => __('User Image')]);
$fieldset->addField(
'user_image',
'image',
[
'name' => 'user_image',
'label' => __('Image'),
'id' => 'user_image',
'title' => __('Image'),
'required' => false,
'note' => 'Allow image type: jpg, jpeg, png'
]
);
$subject->setForm($form);
}
return $proceed();
}
}
Clear cache.
Best Answer
Out of the box you cannot retrieve the block title.
What you'll have to do is:
First, create your own block class that extends
Magento\Cms\Block\Block
Optional: if you want the
getTitle
method to be available for every static block you can setup a preference by creating aetc/di.xml
with the following content:Then, change your code with the following:
Where
Vendor\Module\Block\Cms\Block
is the class you have created to rewrite the original class.Then create your
Vendor\Module\Block\Cms\Block
class with the following content: