Search code examples
prestashopprestashop-1.6

Shop association in HelperForm


In my prestashop helperForm I want to use the inbuilt shop selector. The select tree shows up, but I can't manage to set up the tpl_vars correctly. Strangely it doesn't matter what I put under 'name' (blabla). The checkboxes always have the name "checkBoxShopAsso_configuration".

    if (Shop::isFeatureActive()) {
        $inputs[] = array(
            'type' => 'shop',
            'label' => $this->l('Shop association:'),
            'name' => 'genzo_link',
        );
    }

    $fields_form = array(
        'form' => array(
            'legend' => array(
                'title' => ('Add Link'),
                'icon' => 'icon-cogs'
            ),
            'input' => $inputs,
            'submit' => array(
                'title' => ('Update'),
                'class' => 'btn btn-default pull-right'
            )
        )
    );

    $helper = new HelperForm();
    $helper->submit_action = 'saveLink';
    $helper->default_form_language = $this->context->language->id;
    $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name .'&module_name=' . $this->name;
    $helper->token = Tools::getAdminTokenLite('AdminModules');

    $vars['blabla'][3] = 0;

    $helper->tpl_vars = array(
        'fields_value' => $vars,
        'languages' => $this->context->controller->getLanguages(),
        'id_language' => $this->context->language->id,
    );

How do I need to set up tpl_vars, so that only the shops with an entry are selected? Let's say I have 3 shops and an entry is only in the first two shops. I thought I have to do like this:

$vars['blabla'][1] = 1;
$vars['blabla'][2] = 1;
$vars['blabla'][3] = 0;

But this is not working. It selects always all 3 shops.

Thanks for your help! If something is unclear, please tell me where you need better explanation.

Update: For other people. You just need to set up your object with this three informations.

$helper->table = 'genzo_link';
$helper->id = $id_genzo_link;
$helper->identifier = 'id_genzo_link';

Solution

  • You don't have (or can't) input the selected shops in the form creation. It will do it for you from the database.

    When type is 'shop' it will execute:

    $params['html'] = $this->renderAssoShop($disable_shops);
    

    In HelperForm::renderAssoShop($disable_shared = false, $template_directory = null) it will check if the feature is active, then:

     if ((int)$this->id) {
          $sql = 'SELECT `id_shop`, `'.bqSQL($this->identifier).'`
                  FROM `'._DB_PREFIX_.bqSQL($this->table).'_shop`
                  WHERE `'.bqSQL($this->identifier).'` = '.(int)$this->id;
    
          foreach (Db::getInstance()->executeS($sql) as $row) {
              $assos[$row['id_shop']] = $row['id_shop'];
          }
      }
    

    So, if your object table is blabla you only need to create blabla_shop with id_blabla and id_shop. Btw, if (int)$this->id is 0 it will preselect all shops from context.