There exist several plugins within the pantheon of textpattern plugins that could really use a tweak or addition to the callback system that allows for event-based plugin integration.
The two plugins most concerning me are wlk_article_glyph and wlk_sphereit. Both of these require that the user of the plugin replaces the <txp:body /> tag in their article form. This is inconvenient, at best. For instance, should a user wish to employ both of these tags, they will quickly find that they cannot.
wlk_sphereit, as part of the Sphere.com indexing method, generates it’s own body tag so that it can add some tags around the body to improve the Sphere search engine’s indexing capabilities (and it’s specificity…what is and isn’t article content).
wlk_article_glyph was created in a few minutes at the request of someone of the textpattern forums as a method of adding a glyph to the end of an article, as they do in some newspaper and magazines.
So, to allow both of these to work in textpattern, I forked the unofficial dev 4.0 branch over at github and started hacking at it.
First I added a new callback_event function called “callback_out_event”. This function is only in use in areas like <txp:body /> and <txp:excerpt /> so I removed the $step and $pre variables. Also, I created this function because, while php5 will let you set a default attribute for a function argument that’s being passed by reference, php4 will not.
function callback_out_event($event, &$the_out) { global $plugin_callback;if (!is_array($plugin_callback)) return; $return_value = ''; foreach ($plugin_callback as $c) { if ($c['event'] == $event) { if (is_callable($c['function'])) { $return_value .= call_user_func($c['function'], &$the_out); } } } return $return_value; }
I also now pass by reference whatever variable is being returned by the tag function where I’m placing the callback event. In the case of the body tag, I placed it here:
function body() { global $thisarticle, $is_article_body; assert_article();$is_article_body = 1; $out = parse($thisarticle['body']); $is_article_body = 0; callback_out_event('pre_body_render', $out); return $out; }
So that my plugin function that’s accessing the callback can access it like this:
register_callback('wlk_test_body', 'pre_body_render');function wlk_test_body(&$content) { $character = '★';if(substr($content, -4)=='</p>') { $content = '<!-- sphereit start -->'. substr($content, 0, -4).' '. $character. '</p><!-- sphereit end -->'; } }
The wlk_test_body function gets called whenever the pre_body_render callback is called. I can access the body content (as it’s passed by reference) and add whatever I need to the body.
I have created a test plugin which you can download here. Also, the textpattern source code fork is here
This could be very handy for some plugins, but why did you choose an out arg instead of chaining the plugin results?
— Manfre · Dec 10, 06:05 PM · #
Manfre,
I’m not entirely certain what you’re referring to. Could you be more explicit? I would love to know that I could do it without the out arg.
— Walker Hamilton · Dec 10, 10:26 PM · #
Walker, why not move the article glyph to a JavaScript DOM insertion? Because a glyph is decorative non-content, it should be in CSS, but CSS obviously isn’t powerful enough to handle that.
I whipped up an example that finds the last paragraph to insert the glyph, but I’ve been looking for something to allow the last element to be a list or definition description element. childNodes returned too much stuff (text nodes, etc.), didn’t go deep enough and was awkward to work with, but maybe this will be of some use.
Article Glyph example
— Rich · Jan 25, 11:10 PM · #
After tinkering around with my old code, I figured out a more robust solution that will include paragraph, list item and definition description elements.
Realistically, no other elements make much sense to attach a glyph to. Would it make much sense to attach the glyph to the end of a <table>, <img>, <h3>, etc.?
Article Glyph example 2
— Rich · Jan 26, 06:47 PM · #
Rich,
You’re absolutely right, and that’s a perfect solution.
No, I don’t think that some other elements (like table, img, h3, etc) should have a glyph attached.
Thanks for the example!
— Walker Hamilton · Feb 4, 09:44 AM · #