Bhaveshpp

Professional Magento Developr - Healp eachother to grow

Magento2: Add CMS Block/Page selectbox and image uploader in system config

28 Aug 2021 » magento2

add system.xml

<?xml version = "1.0"?>
<config xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="Bhaveshpp" translate="label" sortOrder="100">
            <label>Bhaveshpp</label>
        </tab>
        <section id="shipping_rescriction" translate="label" sortOrder="100" showInDefault="1" showInWebsite="0" showInStore="0">
            <class>separator-top</class>
            <label>Shipping Rescriction</label>
            <tab>Bhaveshpp</tab>
            <resource>Bhaveshpp_ShippingRestriction::setting_configuration</resource>
            <group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>General Configuration</label>
                <field id="product_page_label" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label comment" type="image">
                    <label>Image</label>
                    <comment>This label will show on product view page, Allowed file types: jpg, jpeg, gif, png</comment>
                    <backend_model>Magento\Config\Model\Config\Backend\Image</backend_model>
                    <base_url type="media" scope_info="1">shipping_restriction_config</base_url>
                    <upload_dir>pub/media/shipping_restriction_config</upload_dir>
                </field>
                <field id="checkout_page_cms_block" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="1">
                    <label>Checkout CMS block</label>
                    <comment>This block will show on shipping method section</comment>
                    <source_model>Bhaveshpp\ShippingRestriction\Model\Config\Source\CmsBlock</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

\app\code\Bhaveshpp\ShippingRestriction\Model\Config\Source\CmsBlock.php

<?php
namespace Bhaveshpp\ShippingRestriction\Model\Config\Source;
use Magento\Cms\Model\ResourceModel\Block\CollectionFactory;

/**
 * Class CmsBlock Source for system config
 */
class CmsBlock implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * get colloection of cms block list
     *
     * @var CollectionFactory
     */
    protected $collection;

    /**
     * initialize
     *
     * @param CollectionFactory $collection
     */
    public function __construct(
        CollectionFactory $collection
    )
    {
        $this->collection = $collection;
    }

    /**
     * Options getter
     *
     * @return array
     */
    public function toOptionArray()
    {
	    $res = array();
 	    $collection = $this->collection->create();
  	    $collection->addFieldToFilter('is_active' , \Magento\Cms\Model\Block::STATUS_ENABLED);		
		foreach($collection as $page){
		   $data['value'] = $page->getData('identifier');
	           $data['label'] = $page->getData('title');
		   $res[] = $data;
		} 
		return $res;
    }
}