Search code examples
phpprestashopsmartyprestashop-1.7

Undefined index: id_option Error in Prestashop Selection Helper


I am trying to set up a dropdown list of timezone for a Prestashop module. I followed this example - http://doc.prestashop.com/display/PS16/Using+the+HelperForm+class#UsingtheHelperFormclass-Selector

This is the code I used to get the list of timezones:

function timezones() {
        $timezones = [];

        foreach (timezone_identifiers_list() as $timezone) {
            $datetime = new \DateTime('now', new DateTimeZone($timezone));
            $timezones[] = [
                'sort' => str_replace(':', '', $datetime->format('P')),
                'offset' => $datetime->format('P'),
                'name' => str_replace('_', ' ', implode(', ', explode('/', $timezone))),
                'timezone' => $timezone,
            ];
        }

        usort($timezones, function($a, $b) {
            return $a['sort'] - $b['sort'] ?: strcmp($a['name'], $b['name']);
        });

        return $timezones;
    }       

I then tried to follow the instruction in the documentation by doing this -

        $timezoneList = timezones();    
    $options = array();
    foreach ($timezoneList as $timezone)
    {
      $options[] = array(
        "id" => $timezone['offset'],
        "name" => '(UTC '.$timezone['offset'].') '.$timezone['name'].''
      );
    }

My result produced a dropdown with the list I want but the values are empty and throwing the errors below - Notice on line 786 in file F:\xampp\htdocs\presta02\vendor\prestashop\smarty\sysplugins\smarty_internal_templatebase.php(157) : eval()'d code [8] Undefined index: id_option

My aim is to get something like this -

<option value="-11:00">(UTC -11:00) Pacific, Midway</option>

Currently I get this -

<option value="">(UTC -11:00) Pacific, Midway</option>


Solution

  • If you have used this part from the documentation

    array(
        'type' => 'select',                              // This is a <select> tag.
        'label' => $this->l('Shipping method:'),         // The <label> for this <select> tag.
        'desc' => $this->l('Choose a shipping method'),  // A help text, displayed right next to the <select> tag.
        'name' => 'shipping_method',                     // The content of the 'id' attribute of the <select> tag.
        'required' => true,                              // If set to true, this option must be set.
        'options' => array(
            'query' => $options,                           // $options contains the data itself.
            'id' => 'id_option',                           // The value of the 'id' key must be the same as the key for 'value' attribute of the <option> tag in each $options sub-array.
            'name' => 'name'                               // The value of the 'name' key must be the same as the key for the text content of the <option> tag in each $options sub-array.
        )
    ),
    

    then you must have the same keys in your $options array. It means that you must use this

    $timezoneList = timezones();    
    $options = array();
    foreach ($timezoneList as $timezone)
    {
      $options[] = array(
        "id_option" => $timezone['offset'],
        "name" => '(UTC '.$timezone['offset'].') '.$timezone['name'].''
      );
    }
    

    or chenge your select's definition option key from id_option to id

    'options' => array(
         'query' => $options,                           // $options contains the data itself.
         'id' => 'id',                                  // The value of the 'id' key must be the same as the key for 'value' attribute of the <option> tag in each $options sub-array.
         'name' => 'name'                               // The value of the 'name' key must be the same as the key for the text content of the <option> tag in each $options sub-array.
    )
    

    and by the way, this comment also says this

    // The value of the 'id' key must be the same as the key for 'value' attribute of the <option> tag in each $options sub-array.