EmicoEcommerce/Magento2Tweakwise

View on GitHub
Setup/Patch/Data/AddFilterAndFilterValuesCategoryAttribute.php

Summary

Maintainability
A
3 hrs
Test Coverage
<?php

namespace Tweakwise\Magento2Tweakwise\Setup\Patch\Data;

use Tweakwise\Magento2Tweakwise\Model\Config;
use Magento\Catalog\Model\Category;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class AddFilterAndFilterValuesCategoryAttribute implements DataPatchInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function apply()
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $eavSetup->addAttribute(
            Category::ENTITY,
            Config::ATTRIBUTE_FILTER_WHITELIST,
            [
                'type' => 'varchar',
                'label' => 'Filter whitelist',
                'input' => 'text',
                'sort_order' => 10,
                'source' => '',
                'global' => ScopedAttributeInterface::SCOPE_STORE,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => null,
                'group' => 'Tweakwise',
                'backend' => ''
            ]
        );

        $eavSetup->addAttribute(
            Category::ENTITY,
            Config::ATTRIBUTE_FILTER_VALUES_WHITELIST,
            [
                'type' => 'varchar',
                'label' => 'Filter values whitelist',
                'input' => 'text',
                'sort_order' => 10,
                'source' => '',
                'global' => ScopedAttributeInterface::SCOPE_STORE,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => null,
                'group' => 'Tweakwise',
                'backend' => ''
            ]
        );
    }


    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}