Companyattribute

With this snippet you can create company attribute programmatically through a Magento 2 InstallSchema or UpdateSchema setup script and adds the fields to the admin form.

Magento 2 create company attribute programmatically

Use the snippet in the Magento 2 module creator.

Files

view/base/ui_component/company_form.xml

<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
	<fieldset name="general">
		<field formElement="input" name="test">
			<argument name="data" xsi:type="array">
				<item name="config" xsi:type="array">
					<item name="source" xsi:type="string">test</item>
				</item>
			</argument>
			<settings>
				<label translate="true">Test</label>
				<dataType>text</dataType>
				<validation>
					<rule name="required-entry" xsi:type="boolean">false</rule>
				</validation>
			</settings>
		</field>
	</fieldset>
</form>

etc/extension_attributes.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
	<extension_attributes for="Magento\Company\Api\Data\CompanyInterface">
		<attribute code="test" type="string"/>
	</extension_attributes>
</config>

etc/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\Company\Model\Company\DataProvider">
		<plugin disabled="false" name="Mage2Gen_Module_Plugin_Magento_Company_Model_Company_DataProvider" sortOrder="10" type="Mage2Gen\Module\Plugin\Magento\Company\Model\Company\DataProvider"/>
	</type>
	<type name="Magento\Company\Controller\Adminhtml\Index\Save">
		<plugin disabled="false" name="Mage2Gen_Module_Plugin_Magento_Company_Controller_Adminhtml_Index_Save" sortOrder="10" type="Mage2Gen\Module\Plugin\Magento\Company\Controller\Adminhtml\Index\Save"/>
	</type>
	<type name="Magento\Company\Api\CompanyRepositoryInterface">
		<plugin disabled="false" name="Mage2Gen_Module_Plugin_Magento_Company_Api_CompanyRepositoryInterface" sortOrder="10" type="Mage2Gen\Module\Plugin\Magento\Company\Api\CompanyRepositoryInterface"/>
	</type>
</config>

Setup/InstallSchema.php

<?php


namespace Mage2Gen\Module\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class InstallSchema implements InstallSchemaInterface
{

    /**
     * {@inheritdoc}
     */
    public function install(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->getConnection()->addColumn(
            $setup->getTable('company'),
            'test',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'length' => 255,
                'comment' =>'Test'
            ]
        );
    }
}

Plugin/Magento/Company/Model/Company/DataProvider.php

<?php


namespace Mage2Gen\Module\Plugin\Magento\Company\Model\Company;

class DataProvider
{

    /**
     * @param \Magento\Company\Model\Company\DataProvider $subject
     * @param $result
     * @return mixed
     */
    public function afterGetGeneralData(
        \Magento\Company\Model\Company\DataProvider $subject,
        $result,
        \Magento\Company\Api\Data\CompanyInterface $company
    ) {
        $result['test'] = $company->getData('test');

        return $result;
    }
}

Plugin/Magento/Company/Api/CompanyRepositoryInterface.php

<?php


namespace Mage2Gen\Module\Plugin\Magento\Company\Api;

class CompanyRepositoryInterface
{

    
    /**
     * @var \Magento\Company\Model\CompanyRepository
     */
    protected $companyRepository;
    
    /**
     * @var \Magento\Company\Api\Data\CompanyExtensionFactory
     */
    protected $companyExtensionFactory;

    /**
     * @param \Magento\Company\Model\CompanyRepository $companyRepository
     * @param \Magento\Company\Api\Data\CompanyExtensionFactory $companyExtensionFactory
     */
    public function __construct(
        \Magento\Company\Model\CompanyRepository $companyRepository,
        \Magento\Company\Api\Data\CompanyExtensionFactory $companyExtensionFactory
    ) {
        $this->companyRepository = $companyRepository;
        $this->companyExtensionFactory = $companyExtensionFactory;
    }

    /**
     * @param \Magento\Company\Api\CompanyRepositoryInterface $subject
     * @param $result
     * @return mixed
     */
    public function afterGet(
        \Magento\Company\Api\CompanyRepositoryInterface $subject,
        $result
    ) {
        $company = $result;
        $extensionAttributes = $company->getExtensionAttributes();
        $companyExtension = $extensionAttributes ? $extensionAttributes : $this->companyExtensionFactory->create();
        
        $companyExtension->setTest($company->getData('test'));

        $company->setExtensionAttributes($companyExtension);
        return $company;
    }

    /**
     * @param \Magento\Company\Api\CompanyRepositoryInterface $subject
     * @param $result
     * @return mixed
     */
    public function afterSave(
        \Magento\Company\Api\CompanyRepositoryInterface $subject,
        $result
    ) {
        $company = $result;
        $extensionAttributes = $company->getExtensionAttributes();
        if (!$extensionAttributes) {
            return $company;
        }
        
        $company->setData('test', $extensionAttributes->getTest());

        $company->save();
        return $company;
    }

    /**
     * @param \Magento\Company\Api\CompanyRepositoryInterface $subject
     * @param $result
     * @return mixed
     */
    public function afterGetList(
        \Magento\Company\Api\CompanyRepositoryInterface $subject,
        $result
    ) {
        foreach ($result->getItems() as $company) {
            $this->afterGet($subject, $company);
        }
        return $result;
    }
}

Plugin/Magento/Company/Controller/Adminhtml/Index/Save.php

<?php


namespace Mage2Gen\Module\Plugin\Magento\Company\Controller\Adminhtml\Index;

class Save
{

    /**
     * @param \Magento\Company\Controller\Adminhtml\Index\Save $subject
     * @param $result
     * @return mixed
     */
    public function afterSetCompanyRequestData(
        \Magento\Company\Controller\Adminhtml\Index\Save $subject,
        $result
    ) {
        $result->setData('test', $subject->getRequest()->getPostValue('general')['test']);

        return $result;
    }
}