/   /   /  OroCommerce For developers : Include CSS and Update on configuration save

Note:

For more extensions and themes visit our store

OroCommerce For developers : Include CSS and Update on configuration save


In this course we will talk about how listening save configuration event inside OroCommerce ,  and we’re going to use this event to update custom included CSS File Our Example Bundle named by 
IbnabConfigCssBundle the full path src/Ibnab/Bundle/ConfigCssBundle/IbnabConfigCssBundle.php
the learn how to create you custom OroCommerce Bundle refer to :
https://oroinc.com/b2b-ecommerce/doc/3.1/dev-guide/extend-and-customize/how-to-create-new-bundle 
and to create configuration setting:
https://oroinc.com/orocrm/doc/current/dev-guide/getting-started-book/configuration 

1 - Start By Configuration and event listener

So after create system_configuration.yml fill with :

  1.  
  2. system_configuration:
  3.     groups:
  4.         ibnab_config_css_setting:
  5.             title: ibnab.config_css.system_configuration.groups.settings.title
  6.             icon:  'fa-file'
  7.         ibnab_config_css_view:
  8.             title: ibnab.config_css.system_configuration.groups.view.title
  9.     fields:
  10.         ibnab_config_css.hcolor:
  11.             data_type: string
  12.             type: 'Oro\Bundle\FormBundle\Form\Type\OroSimpleColorPickerType'
  13.             priority: 75
  14.             options:
  15.                 label: ibnab.config_css.system_configuration.fields.hcolor.label
  16.                 allow_empty_color: 0
  17.                 allow_custom_color: 1
  18.     tree:
  19.         system_configuration:
  20.             platform:
  21.                 children:
  22.                     general_setup:
  23.                         children:
  24.                             ibnab_config_css_setting:
  25.                                 priority: 500
  26.                                 children:
  27.                                     ibnab_config_css_view:
  28.                                         priority: 700
  29.                                         children:
  30.                                             - ibnab_config_css.hcolor
  31.  

We will add our configuration to general_setup section , is just color picker field with name ,  hcolor but the full name ibnab_config_css.hcoloribnab_config_css is the Alias inside DependencyInjection folder plz explore the Configuration.php and IbnabConfigCssExtension.php the see that .
For more explain visit basic infos about symfony:
https://symfony.com/doc/3.4/bundles/configuration.html 

 

Ok let’s start with configuration reader , the service @oro_config.global is responsible to read or write configuration you can use directly , the class related to service is Oro\Bundle\ConfigBundle\Config\ConfigManager , Ok nice but we well use our configuration provider which using this service , inside your service.yml:

  1.  
  2. parameters:
  3.     # Config provider
  4.     ibnab_config_css.provider.configuration.class:         Ibnab\Bundle\ConfigCssBundle\Provider\ConfigurationProvider  
  5. services: 
  6.     ibnab_config_css.provider.configuration:
  7.         class: %ibnab_config_css.provider.configuration.class%
  8.         arguments:
  9.             - '@oro_config.global'
  10.  

content of our class:

  1.  
  2. <?php
  3. namespace Ibnab\Bundle\ConfigCssBundle\Provider;
  4. use Oro\Bundle\ConfigBundle\Config\ConfigManager;
  5. class ConfigurationProvider
  6. {
  7.     const HColor_FIELD = 'ibnab_config_css.hcolor';
  8.     /**
  9.      * @var ConfigManager
  10.      */
  11.     protected $configManager;
  12.     /**
  13.      * @param ConfigManager $configManager
  14.      */
  15.     public function __construct(ConfigManager $configManager)
  16.     {
  17.         $this->configManager = $configManager;
  18.     }
  19.     /**
  20.      * @return string
  21.      */
  22.     public function getHColor()
  23.     {
  24.         return $this->configManager->get(self::HColor_FIELD);
  25.     }
  26. }
  27.  

Just one function to read our header color : getHColor() .
So the event responsible to detect on save config is :
event: oro_config.update_after by using the internal kernel listener name: kernel.event_listener , ok inside your service.yml  push too :
  1.  
  2.     ibnab_config_css.event_listener.color_change:
  3.         class: 'Ibnab\Bundle\ConfigCssBundle\EventListener\ColorChangeListener'
  4.         arguments:
  5.             - '@ibnab_config_css.provider.configuration'
  6.             - "@service_container"
  7.         tags:
  8.             - { name: kernel.event_listener, event: oro_config.update_after, method: onConfigUpdate }
  9.  

method: onConfigUpdate is the function which we call it on config save , the class  ColorChangeListener content :

  1.  
  2. <?php
  3. namespace Ibnab\Bundle\ConfigCssBundle\EventListener;
  4. use Oro\Bundle\ConfigBundle\Event\ConfigUpdateEvent;
  5. use Ibnab\Bundle\ConfigCssBundle\Provider\ConfigurationProvider;
  6. use Symfony\Component\Filesystem\Filesystem;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. /**
  9.  * Saves the date when the config cache was changed.
  10.  */
  11. class ColorChangeListener
  12. {
  13.     /**
  14.      * @var ConfigManager
  15.      */
  16.     protected $configProvider;
  17.     
  18.     /** @var ContainerInterface */
  19.     protected $container;
  20.     /**
  21.      * @param ConfigManager $configProvider
  22.      */
  23.     public function __construct(ConfigurationProvider $configProvider,ContainerInterface $container)
  24.     {
  25.         $this->configProvider = $configProvider;
  26.         $this->container = $container;
  27.     }
  28.    /**
  29.      * @param ConfigUpdateEvent $event
  30.      */
  31.     public function onConfigUpdate(ConfigUpdateEvent $event)
  32.     {
  33.         $changeSet = $event->getChangeSet();
  34.         if(!empty($changeSet) && isset($changeSet[ConfigurationProvider::HColor_FIELD ]) ){
  35.             $cssTemplate = ".middlebar{background-color:".$this->configProvider->getHColor().";}";
  36.             $fs = new Filesystem();
  37.             $publicPath = $this->container->get('kernel')->getProjectDir().'/public/bundles/ibnabconfigcss/css/configcss.css';
  38.             if($fs->exists($publicPath)){
  39.             
  40.             $fs->remove($publicPath);                 
  41.             }
  42.             $fs->dumpFile($publicPath, $cssTemplate);
  43.         }
  44.    }
  45. }
  46.  

$changeSet = $event->getChangeSet();  return array of all changed field and with if statement :

if(!empty($changeSet) && isset($changeSet[ConfigurationProvider::HColor_FIELD ]) ){

if our field has changed we will get the new value and dump to css file inside public bundle .
 

2 – How to include our css file to frontend them:


OroCommerce using the layouts system to manage frontend output  let’s create the file configcss.yml inside path src/Ibnab/Bundle/ConfigCssBundle/Resources/views/layouts/default/page/configcss.yml

 

we have add a layout to default theme inside page folder , page folder contain the management of structure of page body head … the content is :

  1.  
  2. layout:
  3.     actions:
  4.         - '@add':
  5.             id: ibnab_config_css_color
  6.             parentId: head
  7.             blockType: style
  8.             options:
  9.                 src: '=data["asset"].getUrl("bundles/ibnabconfigcss/css/configcss.css")'
  10.  

we have add block of type style to the head section , and with options src = path to file  data["asset"].getUrl return url to public + "bundles/ibnabconfigcss/css/configcss.css".

Comments

Related Posts

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.