kevinjardine / shoutout

Allows adding content (possibly with file attachments) directly to the river
5 stars 3 forks source link

Make elgg-river-timestamp have a link to the shoutout item page #14

Closed mrclay closed 12 years ago

mrclay commented 12 years ago

(just a suggestion we've already implemented on our side)

We applied a plugin hook handler for "view", "river/elements/body" with this code inside the handler:

if (isset($params['vars']['item'])) {
    $item = $params['vars']['item'];
    if ($item instanceof ElggRiverItem) {
        /* @var ElggRiverItem $item  */
        $entity = $item->getObjectEntity();
        if (elgg_instanceof($entity, 'object', 'shoutout')) {
            $url = $entity->getURL();
            $returnValue = preg_replace_callback(
                '@(<span class="elgg-river-timestamp">)([^/]+)(</span>)@',
                function ($m) use ($url) {
                    $link = elgg_view('output/url', array(
                        'href' => $url,
                        'text' => $m[2],
                    ));
                    return $m[1] . $link . $m[3];
                },
                $returnValue);
        }
    }
}

This uses an anonymous function, but you can pull that out to a global function for 5.2 compat.

kevinjardine commented 12 years ago

Yes, nice to have closures and anonymous functions.

I recoded this so it should work under PHP 5.2 so it works for others when we release this on elgg.org, and committed the change to github.