pre_boucle

The pipeline pre_boucle modifies the SQL queries that result from the interpretation of the loops of SPIP. It is called at each compilation phase, after the compiler has already taken into account the selection criteria (the critere_NAME() functions), and before the call to the boucle_NAME() functions.

It receives as argument a "Boucle" object that contains the data issued from the previous compilation steps for the current loop.

It is therefore possible to take actions based on the criteria that are passed to the loop, like modifying the selection parameters or the "where" condition for the loop’s SQL query.

Example

The "mots techniques" plugin adds a technical field to the groups of keywords of SPIP.

When there is no {technique} criteria passed to the loop GROUPE_MOTS, the loop automatically filters its results, returning only those where the field {technique} is empty. This same feature could also be implemented by creating a function called boucle_GROUPES_MOTS().

function mots_techniques_pre_boucle($loop){
    if ($loop->type_requete == 'groupes_mots') {
        $table_name = $loop->id_table;
        $technical_kw = $table_name .'.technique';
        // Select only the loop without the "technical" keyword
        if (!isset($loop->modificateur['criteres']['technique']) &&
            !isset($loop->modificateur['tout'])) {
                $loop->where[]= array("'='", "'$technical_kw'", "'\"\"'");
        }      
    }
    return $loop;
}

The array $loop->where[] contains arrays with 3 entries: successively being the operator, the field and the value. Here, we add to the query the string {$table_name}.technique='' with:

$boucle->where[]= array("'='", "'$technical_kw'", "'\"\"'");

Author Gilles Vincent Published : Updated : 30/05/10

Translations : English, français