Неопределенный индекс: ошибка id_option в помощнике выбора Prestashop - PullRequest
0 голосов
/ 04 июня 2018

Я пытаюсь настроить раскрывающийся список часовых поясов для модуля Prestashop.Я следовал этому примеру - http://doc.prestashop.com/display/PS16/Using+the+HelperForm+class#UsingtheHelperFormclass-Selector

Это код, который я использовал для получения списка часовых поясов:

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;
    }       

Затем я попытался следовать инструкциям в документации, выполнив это -

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

Мой результат дал выпадающий список со списком, который я хочу, но значения пустые и выдает ошибки ниже - Обратите внимание на строку 786 в файле F: \ xampp \ htdocs \ presta02 \ vendor \prestashop \ smarty \ sysplugins \ smarty_internal_templatebase.php (157): eval () 'd code [8] Неопределенный индекс: id_option

Моя цель - получить что-то вроде этого -

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

В настоящее время я получаю это -

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

1 Ответ

0 голосов
/ 05 июня 2018

Если вы использовали эту часть из документации

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.
    )
),

, то у вас должны быть те же ключи в вашем массиве $ options.Это означает, что вы должны использовать это

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

или изменить ключ опции определения вашего выбора от id_option до 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.
)

, и, кстати, этот комментарий также говорит об этом

// 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.
...