2

I am trying to add a field to the attribute information section. This should be an text field for multistore ( just like the manage titles section).

Magento version: 2.2.6

Here's what I got now:

  • I created a custom module.
  • A plugin: app\code\Custom\Module\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab\front.php
use Magento\Framework\DB\Ddl\Table;

class Front
{
    /**
     * @param Magento\Config\Model\Config\Source\Yesno $yesNo
     */
    protected $scopeConfig;

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig

    ) {
        $this->scopeConfig = $scopeConfig;
    }

    public function aroundGetFormHtml(
        \Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front $subject,
        \Closure $proceed
    )
    {

        $form = $subject->getForm();
        $fieldset = $form->getElement('front_fieldset');
        $fieldset->addField(
            'label_field',
            'text',
            [
                "type" => Table::TYPE_TEXT,
                'name' => 'label_field',
                'label' => __('Label field'),
                'title' => __('label_field'),
                'note' => __('label_field'),
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'translate' => 'label',
                "nullable" => true,
                'system' => 0,
            ]
        );

        return $proceed();
    }
}

And I also created A upgradeschema to create the field in the catalog_eav_attribute table;

class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
     * {@inheritdoc}
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        $installer->getConnection()->addColumn(
            $installer->getTable('catalog_eav_attribute'),
            'label_field',
            [
                'type' => Table::TYPE_TEXT,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                "nullable" => true,
                'default' => '',
                'comment' => 'label field',
                "system" => 0,
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            ]
        );
        $installer->endSetup();
    }
}

The field is created in attribute information -> Frontend properties aswell as the column in the db. Also the value is being saved but I am not able to see it in the backend. The endgoal is to save the attribute as text ( preffered for multistore ) and show the value in a phtml file. I seem to be stuck at retrieving the data in the backend.

This is my first time asking a question so if any more / less info is needed let me know.

0

For anyone else struggling with this, I got it to work.

Override: 'Magento/catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Front.php' to my custom module and added my fields in there.

Also used upgradeschema to create new columns inside the catalog_eav_attribute table.

I think there is a better solution doing this with UI components, but I still need to wrap my head arround that.

3
  • Can you provide your full code please? – mahmoudismail Aug 8 '19 at 8:31
  • Can you be a bit more specific? – Fabian Aug 8 '19 at 13:19
  • It was solved thank you dear :) – mahmoudismail Aug 8 '19 at 13:20

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.