2
votes

I have an Expression Engine site with the following requirement.

I need to be able to filter a channel entries result set by a pair of matching fields e.g. my entry will have a field_a and field_b. If these fields match I want it to be part of the return result set. I cant just do this check on the frontend because then the result count will not be correct. I was thinking I could use a hook to be able to pass in the fields into the exp:channel:entries tag and alter the returned data.

Does this seem sensible and if so does anyone know details on manipulating that data? Looking at the doc's I guess I want to use the 'channel_entries_query_result' hook, but i'm not sure on how to actually manipulate the data. I have created the hook which is firing fine, and I can see the template tag_data etc, but i'm not sure where to go next.

Thanks

3

3 Answers

2
votes

You can write your own module, extending the channel entries loop.

You would use it like this:

{exp:custom_module:entries}

This method would support any of the default parameters.

The method in the module would look like this:

public function entries()
{
    if( ! class_exists('Channel'))
    {
        require_once PATH_MOD.'channel/mod.channel.php';
    }

    // queries grabbing entry ids you want
    ....

    $this->EE->TMPL->tagparams['entry_id'] = implode('|', $entry_ids);

    $channel = new Channel();
    $tagdata = $channel->entries();

    return $tagdata;
 }
0
votes

This sounds like it will need a custom query, you will not to use any of EEs hooks to accomplish this.

You could either write your own plugin / module or use EE's native Query module.

You'll simply need to compair the two columns with your query. For example:

SELECT
    *
FROM
    some_table
WHERE
    col1 = col2
0
votes

For anyone else interested in how this was done, here is the code. I went down the hooks route and bound to the 'channel_entries_query_result' hook with the following code.

public function query_result_filtered($c, $res){
    # maybe this can be done better? Grabs the tag data for this template call
    $tags = $c->EE->TMPL->tag_data;

    foreach($tags as $tag):
        # We're only interested in exp:channel:entries
        if($tag['class'] == 'channel' && $tag['method'] == 'entries'):
            # We're only interested if the tag has a param of matching, e.g. {exp:channel:entries matching="field_1|field_2"}
            if(isset($tag['params']['matching'])):
                $res = $this->_parse_results($res, $tag['params']['matching']);
            endif;
        endif;
    endforeach;

    return $res;
}

private function _parse_results($res, $fields){

    $ret = array();
    $fields = explode('|', $fields);

    //If we dont have multiple tags to match against, return the result set as is
    if(!is_array($fields)):
        return $res;
    endif;

    # Get the field id's and count how many fields we're checking against
    $fields = $this->_get_field_ids($fields);
    $field_count = count($fields);

    foreach($res as $row):
        # Value to match against (just use the first value)
        $tomatch = $row[$fields[0]];
        # Keep a count on how many matches, so we can check that they all match
        $match = 0;

        foreach($fields as $field):
            # If the current field matches that of the first field then we have a match, increment the count
            if($row[$field] == $tomatch):
                $match++;
            endif;
        endforeach;

        # If we have matched all fields then add this row to the returned array
        if($match == $field_count):
            $ret[] = $row;
        endif;

    endforeach;

    return $ret;
}

private function _get_field_ids($fields){

    $ret = array();

    # Loop through the fields given and find their ID's (this could be better and check site id for multisite compatibility)
    foreach($fields as $field):
        $q = $this->EE->db->select('field_id')->where('field_name', $field)->get('exp_channel_fields');
        # Create a nice name that we can use as an array key when we check each rows value
        $ret[] = 'field_id_' . $q->row('field_id');
    endforeach;

    return $ret;
}

Not particularly elegant, but it worked. If anyone else has a better solution i'd love to hear about it. Thanks