Zen Cart Logo
Forums / Code Collaboration / Strategy when multiple observers inject content in admin/orders.php

Strategy when multiple observers inject content in admin/orders.php

Views: 11,630

Results 1 to 3 of 3
17 Feb 2019, 1:32 AM
#1
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Strategy when multiple observers inject content in admin/orders.php

There are several comments in admin/orders.php advising checking (for example) $extra_headings for (bool) false before initializing $extra_headings, since multiple observers might be injecting content. Suppose there ARE multiple observers from different add-ons injecting content. What strategy should be employed in this case? I suppose if $extra_headings is not (bool) false because observer A injected content, that observer B should add their content to $extra_headings, either at the beginning or end of the array depending on the desired order in the output. But how would an observer writer know the end users desired order? Another possibility might be for the user to control the loading order of the observers. It seems to me that guidance to observer writers is needed to avoid conflicts when $extra_headings is not (bool) false. Or perhaps guidance to users in add-on instructions. Thoughts anyone?

17 Feb 2019, 12:53 PM
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,065
Plugin Contributions:
56

Re: Strategy when multiple observers inject content in admin/orders.php

@Dave224, the approach that I've taken for the heading/content injections is for the watching observers to add their content in the order in which they're loaded (via their associated auto-loader). That way, the outcome is repeatable and, as you indicated, also controllable by adjusting the content-adding plugins' auto-load checkpoints.

Here's a section from one such plugin, showing how I handle the header-addition (content-addition uses a similar approach):

            // -----
            // Issued by /admin/orders.php when generating the orders' listing, allowing the
            // insert of an additional column heading.  
            //
            // On entry:
            //
            // $p2 ... Contains a reference to the $extra_headings array, to be returned in the
            //         following format:
            //
            // $extra_headings = array(
            //     array(
            //       'align' => $alignment,    // One of 'center', 'right', or 'left' (optional)
            //       'text' => $value
            //     ),
            // );
            //
            // Note:  Be sure to check that the $p2/$extra_headings value is specifically (bool)false before initializing, since
            // multiple observers might be injecting content!
            //
            case 'NOTIFY_ADMIN_ORDERS_LIST_EXTRA_COLUMN_HEADING':
                if ($p2 === false) {
                    $p2 = array();
                }
                $p2[] = array(
                    'align' => 'center',
                    'text' => MYPLUGIN_ORDERS_HEADING_STATUS
                );
                break;
17 Feb 2019, 7:28 PM
#3
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Strategy when multiple observers inject content in admin/orders.php

Slick! I was thinking if--then--else, but your approach is better. Thanks!