0

Here I need to add a Banner attribute in Magento 2. enter image description here

Here, On banner edit page I need to add an attribute, but I have no idea how to achieve it. Can I get some help? Thank you in advance.

2
  • are you using mageplaza banner slider module ?do you want to add text attribute (column) ? – Pawan Dec 23 '20 at 6:05
  • Thank you @Pawan for the response, Yes am using Mageplaza banner slider. I need a Dropdown attribute. – Praveen Negimani Dec 23 '20 at 6:35
0

It is always better to keep our customization in custom module, So I am creating a module which will:

  • Add a custom column in table mageplaza_bannerslider_banner to hold value
  • Add custom value to existing form
  • Add source model to keep option values

app/code/Pawan/MageBanner/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Pawan_MageBanner',
    __DIR__
);

app/code/Pawan/MageBanner/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Pawan_MageBanner" setup_version="1.0.0" >
        <sequence>
          <module name="Mageplaza_BannerSlider"/>
        </sequence>
    </module>
</config>

app/code/Pawan/MageBanner/Setup/InstallSchema.php

<?php

namespace Pawan\MageBanner\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class InstallSchema implements InstallSchemaInterface
{


public function install(SchemaSetupInterface $setup, ModuleContextInterface    $context)
  {
    $installer = $setup;

    $installer->startSetup();

    $eavTable = $installer->getTable('mageplaza_bannerslider_banner');

    $columns = [
        'custom' => [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            'nullable' => true,
            'comment' => 'Custom',
        ],
    ];

    $connection = $installer->getConnection();
    foreach ($columns as $name => $definition) {
        $connection->addColumn($eavTable, $name, $definition);
    }

    $installer->endSetup();
}
}

app/code/Pawan/MageBanner/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="Mageplaza\BannerSlider\Block\Adminhtml\Banner\Edit\Tab\Banner">
        <plugin name="pawan_banner_form" type="Pawan\MageBanner\Plugin\Block\Adminhtml\Banner\Edit\Tab\Banner" sortOrder="1"/>
    </type>
</config>

app/code/Pawan/MageBanner/Plugin/Block/Adminhtml/Banner/Edit/Tab/Banner.php

<?php
namespace Pawan\MageBanner\Plugin\Block\Adminhtml\Banner\Edit\Tab;
use Magento\Framework\Registry;
use Pawan\MageBanner\Model\Config\Source\Custom;

class Banner
{
    protected $registry;
    /**
     * Custom options
     *
     * @var Custom
     */
    protected $customOptions;
        
    public function __construct(
        \Magento\Framework\Registry $registry,
        Custom $customOptions
        )
    {        
        $this->registry = $registry;
        $this->customOptions = $customOptions;
    }
      
    public function aroundGetFormHtml(
        \Mageplaza\BannerSlider\Block\Adminhtml\Banner\Edit\Tab\Banner $subject,
        \Closure $proceed
    )
    {
        $banner = $this->registry->registry('mpbannerslider_banner');

        $form = $subject->getForm();
        if (is_object($form)) {

        $fieldset = $form->getElement('base_fieldset');

        $customBanner = $fieldset->addField('custom', 'select', [
            'name'  => 'custom',
            'label' => __('Custom Text'),
            'title' => __('Custom Text'),
            'values' => $this->customOptions->toOptionArray()
        ]);

        
            if($banner->getData()){
                $form->addValues($banner->getData());
            }
            $subject->setForm($form);
        }

        return $proceed();
    }
}

app/code/Pawan/MageBanner/Model/Config/Source/Custom.php

<?php
namespace Pawan\MageBanner\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;


class Custom implements ArrayInterface
{
    const SOME   = '0';
    const ANOTHER = '1';

    /**
     * to option array
     *
     * @return array
     */
    public function toOptionArray()
    {
        $options = [
            [
                'value' => self::SOME,
                'label' => __('Some value')
            ],
            [
                'value' => self::ANOTHER,
                'label' => __('Another value')
            ]
        ];

        return $options;
    }
}

Note:

  • I have used custom as a field name, you can change as per requirement
  • Need to run commands like setup:upgrade, (if production- setup:static-content:deploy setup:di:compile) Hope above will help!
2
  • Thank you @Pawan – Praveen Negimani Dec 23 '20 at 8:46
  • welcome @PraveenNegimani – Pawan Dec 23 '20 at 8:51

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.