/   /   /  OroCommerce for developer: Custom Block type based on block or container

Note:

For more extensions and themes visit our store

OroCommerce for developer: Custom Block type based on block or container


By default oro came with many types of blocks the most important two is container and block . 
But many cases has a custom behavior for example pass a required variable to your block and using it inside the twig template.

So with Oro layout system you have the ability to create you custom widget based on block So the class parent responsible is Oro\Bundle\LayoutBundle\Layout\Block\Type\ConfigurableType or you can create custom widget based on container by parent oro_layout.block_type.abstract_configurable or oro_layout.block_type.abstract_configurable_container:

  1.  
  2.     oro_layout.block_type.abstract_configurable:
  3.         abstract: true
  4.         class: Oro\Bundle\LayoutBundle\Layout\Block\Type\ConfigurableType
  5.     oro_layout.block_type.abstract_configurable_container:
  6.         abstract: true
  7.         parent: oro_layout.block_type.abstract_configurable
  8.         calls:
  9.             - [setParent, ['container']]


Let’s start using your custom one firstly to organize the tasks will create block_types.yml inside
Ibnab/Bundle/CustomWidgetBundle/Resources/config/block_types.yml and fill with:

  1.  
  2.     ibnab_custom_widget.layout.type.product:
  3.         parent:  oro_layout.block_type.abstract_configurable
  4.         calls:
  5.             - [setOptionsConfig, [{product_id: {}}]]
  6.             - [setName, ['ibnab_custom_widget_product']]


Here yo have added a custom widget based on block widget which able to accept and option product_id and you can add for example is required
- [setOptionsConfig, [{product_id: {required: true}}]]

So Now how you can using it inside you theme or default blank … or other theme  add layout.yml: Ibnab/Bundle/CustomWidgetBundle/Resources/views/layouts/blank/oro_product_frontend_product_view/layout.yml

inside the layout handler  oro_product_frontend_product_view (is folder with the route name of product page view which well get dispatched when you’re on product details page) , So you can fill the layout.yml for example with:

  1.  
  2. layout:
  3.     actions:
  4.         - '@setBlockTheme':
  5.             themes: 'CustomWidgetBundle:layouts:blank/oro_product_frontend_product_view/layout.html.twig' 
  6.         - '@add':
  7.             id:  ibnab_custom_widget_product_direct
  8.             parentId: product_view_primary_container
  9.             blockType:  ibnab_custom_widget_product
  10.             prepend: true
  11.             options:
  12.                 product_id: '=data["product"].getId()'
  13.  

I think is clear that you have using  blockType:  ibnab_custom_widget_product with option product_id with =data["product"].getId() (you are in product details page so the product instance is already passed to this handler by custom layout data).

No is the time to create you twig and using you widget and option so add layout.twig and fill with :

  1.  
  2. {% block _ibnab_custom_widget_product_direct_widget %}
  3.     {% set attr = layout_attr_defaults(attr, {
  4.         '~class': ' html'
  5.     }) %}
  6.     <div {{ block('block_attributes') }}>
  7.         Product Id is : {{ product_id}}
  8.     </div>
  9. {% endblock %}
  10.  

Is a simple example of how you create you custom widget based on block or container abstract service .
For more info PLZ Refer to :
OroCommerce Video for Frontend Developer : Customize product view page

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.