With other answers, you can't saved to database, it only show in backend.
Here's how you create and save the field:
app/code/Jajuma/AssetPreload/Setup/InstallData.php
<?php
namespace Jajuma\AssetPreload\Setup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'lcp_image', [
'type' => 'varchar',
'label' => 'LCP Image',
'input' => 'text',
'visible' => true,
'required' => false,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE
]);
$eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'get_category_image', [
'type' => 'int',
'label' => 'Get Category image',
'input' => 'boolean',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'visible' => true,
'default' => '1',
'required' => false,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE
]);
}
}
app/code/Jajuma/AssetPreload/view/adminhtml/ui_component/category_form.xml
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="asset_preload_category_tab" sortOrder="80">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Asset Preload</item>
<item name="collapsible" xsi:type="boolean">true</item>
<item name="sortOrder" xsi:type="number">50</item>
</item>
</argument>
<field name="lcp_image">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="sortOrder" xsi:type="number">10</item>
<item name="dataType" xsi:type="string">string</item>
<item name="formElement" xsi:type="string">input</item>
<item name="scopeLabel" xsi:type="string" translate="true">[STORE VIEW]</item>
<item name="label" xsi:type="string" translate="true">LCP Image</item>
</item>
</argument>
</field>
<field name="get_category_image">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="sortOrder" xsi:type="number">20</item>
<item name="dataType" xsi:type="string">boolean</item>
<item name="formElement" xsi:type="string">select</item>
<item name="scopeLabel" xsi:type="string" translate="true">[STORE VIEW]</item>
<item name="label" xsi:type="string" translate="true">Get Category image</item>
<item name="valueMap" xsi:type="array">
<item name="true" xsi:type="string">1</item>
<item name="false" xsi:type="string">0</item>
</item>
<item name="default" xsi:type="number">1</item>
</item>
</argument>
</field>
</fieldset>
</form>
And the result:
https://i.sstatic.net/6JULB.png
https://i.sstatic.net/c3YE6.png
Hope it's help.
Products in Category
already exists and it's not quite clear what you mean by "access the current category product collection".