Query in Formit hook doesn't work


This code works inside snippet, but not in de Formit hook, why ?



$ps_database_host     = $modx->getOption('sconf_psdb-host');
$ps_database_charset  = $modx->getOption('sconf_psdb-charset');
$ps_database_name     = $modx->getOption('sconf_psdb-name');
$ps_database_prefix   = $modx->getOption('sconf_psdb-prefix');
$ps_database_username = $modx->getOption('sconf_psdb-username');
$ps_database_password = $modx->getOption('sconf_psdb-pw');
$ps = new xPDO('mysql:host=' . $ps_database_host .
        ';dbname=' . $ps_database_name .
        ';charset=' . $ps_database_charset,
    $ps_database_password );
// text connex
//$o = ($ps->connect()) ? 'Connected' : 'Not Connected';
//echo "<br>";
// Include the xpdo and modx classes
require_once (MODX_CORE_PATH . 'xpdo/xpdo.class.php');
require_once (MODX_CORE_PATH . 'model/modx/modx.class.php');

// Instantiate a new modx object.  MODx inherits from xpdo so we can use it
// like an xpdo object, but it has the extra functions needed for saving content.
// Thanks to Shaun McCormick for writing docs on this.

// load package
$can_work_ps = $ps->addPackage('presta',MODX_CORE_PATH.'components/',$ps_database_prefix);
if (!$can_work_ps) die('Prestashop Package could not be loaded.');

$prestafounrid_single = "100136";
//requete sur la table EmailList pout le récuépration des emails
$query = $ps->newQuery('EmailList');
$query->where(array('id' => $prestafounrid_single));
$results2 = $ps->getCollection('EmailList',$query);
//$emails_forunisseurs = $fournisseur = $cat = array();
//$emails_forunisseurs = array();
foreach($results2 as $result2){
    $emails_fournisseurs = $result2->get('email_fournisseur');
    //$fournisseur[] = $result->get('fournisseur');
    //$cat[] = $result->get('categorie');

   'emailfournisseurinloop' => $emails_fournisseurs,
// data fournisseurs

return $emails_fournisseurs;

Can you elaborate on “does not work”? Are you getting errors, is FormIt not progressing after the hook, what are you seeing?

I’d start by removing $modx->invokeEvent('OnHandleRequest');, require_once (MODX_CORE_PATH . 'xpdo/xpdo.class.php'); and require_once (MODX_CORE_PATH . 'model/modx/modx.class.php'); as those things will already have been done wether you call it as a snippet or a hook.


in Formit hook : returns nothing
in snippet : returns the right value

A FormIt hook has to return true or false.

And maybe use $hook->setValue to set the value:

$hook->setValue('emailfournisseurinloop', $emails_fournisseurs);

No change…
If I set a placeholder without the variable $email_fournisseurs, it works

       'emailfournisseurinloop' =>'test',//ok


  • In Formit hook
 $query = $ps->newQuery('EmailList');
    $query->where(array('id' => 1));
    $results = $ps->getCollection('EmailList', $query);
 die(print_r($results));// ------> returns Array ( ) 1
  • in a test page & snippet
 $query = $ps->newQuery('EmailList');
    $query->where(array('id' => 1));
    $results = $ps->getCollection('EmailList', $query);
 die(print_r($results));// ------> returns object Array ( [1] => EmailList_mysql Object ( [xpdo] => xPDO Object ( [pdo] => P.......

Just discovered when I remove ‘&preHooks=fournCheckboxes
the query works.