How to create category attribute dropdown with Cms Block list programmatically in magento 2?

Magento 2, You can create a custom category attribute with a list of CMS Block in the dropdown field.

The module is simply residing in the app/code folder of Magento instance.

For example, We kept Package name as Rbj and Module name as CategoryAttribute.
You can keep any name for Packagename/Module name. In our demo, we have kept Packagename_Modulename as Rbj_CategoryAttribute.

Now start with basic module creation, Registration.php file is to register our module.  Create a registration.php file,

Full Path,  app/code/Rbj/CategoryAttribute/registration.php,

Create module.xml file,

Path: app/code/Rbj/CategoryAttribute/etc/module.xml,

We have added Magento catalog module as Sequence in a module.xml file as we have to depend on Catalog module.

Getting all the CMS Block in a dropdown, we have to call InstallData.php file.

We have to add attribute using, Magento\Catalog\Model\Category::ENTITY class.

Get the CMS Block option, we need to use “source_model” to define our CMS block collection and iterate through all the cms block and set option value.

Check the definition for source model inside InstallData.php file,
‘source’ => ‘Magento\Catalog\Model\Category\Attribute\Source\Page’
In the above Page.php at catalog module, you can check the CMS block collection with label and value option.

Now create InstallData.php file,
app/code/Rbj/CategoryAttribute/Setup/InstallData.php,

category_form.xml used to define our attribute in Admin Category page. magento category page follows the ui_component pattern to display all the category attribute.

Now create a category_form.xml file,
app/code/Rbj/CategoryAttribute/view/adminhtml/ui_component/category_form.xml,

We have set up our category attribute in the General Section of Category page.
Our attribute cms_block_list is a type of select attribute and we have to define our option as Magento\Catalog\Model\Category\Attribute\Source\Page Class.

For installing our module in Magento 2,  We need to run the command using SSH,  Login with SSH and go to your project root path where Magento is installed,
Run below command,

The Result:

category attribute cms block option
category attribute cms block