简体   繁体   中英

Magento: Delaying Head Block from loading until my event has fired

I have two observers:

<events>
    <controller_action_layout_load_before>
      <observers>
        <QuBit_UniversalVariable_Model_Page_Observer>
          <type>singleton</type>
          <class>universal_variable_main/observer</class>
          <method>setUniversalVariable</method>
        </QuBit_UniversalVariable_Model_Page_Observer>
      </observers>
    </controller_action_layout_load_before>
    <catalog_block_product_list_collection>
      <observers>
        <QuBit_UniversalVariable_Model_Page_Observer>
            <type>singleton</type>
            <class>universal_variable_main/observer</class>
            <method>setCatalogProductListing</method>
        </QuBit_UniversalVariable_Model_Page_Observer>
      </observers>
    </catalog_block_product_list_collection>
  </events>

I want a Block containing javascript to load in my Head, but need to populate it with data that is collected in an Observer . My problem is that the block loads before the event fires. I added some logging to prove this.

2013-07-12T07:18:44+00:00 DEBUG (7): loading setUniversalVariable
2013-07-12T07:18:44+00:00 DEBUG (7): loading template
2013-07-12T07:18:48+00:00 DEBUG (7): loading setCatalogProductListing

At the moment I am adding the block using layout XML, do I need to instead create it programmatically in setCatalogProductListing or is there some other way of prevent it from being loaded until all the data is available?

And is it even possible to create it programmatically here or will the head already have been finalised by this point?

Thanks in advance

At the time the catalog_block_product_list_collection event is dispatched, the head has already been output, so nothing can actually alter it (or in fact, it depends on where the list block is placed, but in almost all cases it will be after the head).

You would have to deal directly with the layout and list blocks to retrieve data from the collection before the head is rendered, but the ways of doing depend on whether you want to apply your code on all list blocks or, eg, just category ones (knowing that blocks can also be called from CMS contents, and therefore may not exist before the corresponding page / block is itself output).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM