Seeking help on using the 'view', 'viewname' plugin hook

I'm using the 'view', 'viewname' plugin hook within the Fivestar plugin to add the rating html code to entities where the code in a view matches certain criteria. Actually, the plugin hook 'view', 'all' is used to parse all views for matching code within the callback function.

All in all this works well. But there's one exception: if the same entity within the same view appears more than once on a single page the rating html code is only added to one instance of the view (I think the view that is rendered last on the page). I've made several attempts during the last year to fix this issue but I never even a promising starting point to fix it (while it might not happen often that the same view is included more than once on a page I do have one case I would need it and another member of the community site asked for a solution to this problem recently, too).

So, I'm now hoping someone might have an idea and would help me. I'm not sure if maybe even using the 'view', 'viewname' plugin hook is the reason for the rating code not being added to each instance of the view on a page in the first place. Does the plugin hook get triggered for each instance or is already the triggering the problem - and if that's the case what would be an alternative to this plugin hook?

For reference here's the code used in the Fivestar plugin:

The callback function of the 'view', 'all' plugin hook:

function elggx_fivestar_view($hook, $entity_type, $returnvalue, $params) {

    $lines = explode("\n", elgg_get_plugin_setting('elggx_fivestar_view', 'elggx_fivestar'));
    foreach ($lines as $line) {
        $options = array();
        $parms = explode(",", $line);
        foreach ($parms as $parameter) {
            preg_match("/^(\S+)=(.*)$/", trim($parameter), $match);
            $options[$match[1]] = $match[2];
        }

        if ($options['elggx_fivestar_view'] == $params['view']) {
            list($status, $html) = elggx_fivestar_widget($returnvalue, $params, $options);
            if (!$status) {
                continue;
            } else {
                return($html);
            }
        }
    }
}

The function used to parse the views:

function elggx_fivestar_widget($returnvalue, $params, $options) {

    $guid = $params['vars']['entity']->guid;

    if (!$guid) {
        return;
    }

    if (elgg_in_context('widgets')) {
        $widget = elgg_view("elggx_fivestar/voting", array('fivestar_guid' => $guid, 'min' => true));
    } else {
        $widget = elgg_view("elggx_fivestar/voting", array('fivestar_guid' => $guid));
    }

    // get the DOM
    $html = str_get_html($returnvalue);

    $match = 0;
    foreach ($html->find($options['tag']) as $element) {
        if ($element->$options['attribute'] == $options['attribute_value']) {
            $element->innertext .= $options['before_html'] . $widget . $options['after_html'];
            $match = 1;
            break;
        }
    }

    $returnvalue = $html;
    return(array($match, $returnvalue));
}

The array of views the rating widget should be added to including the code within the view that defines the location the rating widget should be added:

function elggx_fivestar_defaults() {

$elggx_fivestar_view = 'elggx_fivestar_view=object/blog, tag=div, attribute=class, attribute_value=elgg-subtext, before_html=<br />
elggx_fivestar_view=object/file, tag=div, attribute=class, attribute_value=elgg-subtext, before_html=<br />
elggx_fivestar_view=object/bookmarks, tag=div, attribute=class, attribute_value=elgg-subtext, before_html=<br />
elggx_fivestar_view=object/page_top, tag=div, attribute=class, attribute_value=elgg-subtext, before_html=<br />
elggx_fivestar_view=object/thewire, tag=div, attribute=class, attribute_value=elgg-subtext, before_html=<br />
elggx_fivestar_view=group/default, tag=div, attribute=class, attribute_value=elgg-subtext, before_html=<br>
elggx_fivestar_view=object/groupforumtopic, tag=div, attribute=class, attribute_value=elgg-subtext, before_html=<br />
elggx_fivestar_view=icon/user/default, tag=div, attribute=class, attribute_value=elgg-avatar elgg-avatar-large, before_html=<br>
elggx_fivestar_view=object/album, tag=div, attribute=class, attribute_value=elgg-subtext, before_html=<br />
elggx_fivestar_view=object/image, tag=div, attribute=class, attribute_value=elgg-subtext, before_html=<br />';

elgg_set_plugin_setting('elggx_fivestar_view', $elggx_fivestar_view, 'elggx_fivestar');
}

 

  • Not sure, but the plugin hook is called on each usage of elgg_view()

    var_dump is your friend

  • Well, I already started (for about the xth time) using var_dump / elgg_dump to debug this code line by line. I guess var_dump will become an even better friend... Right now I only know that the plugin hook seems to be triggered correctly for each (multiple occuring) view. That's where I stopped yesterday. Now I need to figure out if the elggx_fivestar/voting view is not inserted correctly (i.e. only insterted once for whatever reason) or if the problem might be connected with this view containing JS code (and this is failing for whatever reason if the code needs to be inserted for the same entity more than once).