如何在drupal表下拉列表中创建所有语言的数组

时间:2022-09-12 08:48:03

I need to create dropdown list for all languages in my Drupal form, Is there any function in PHP to achieve this. I want to create an array of all languages and pass it to drupal form.

我需要为我的Drupal表单中的所有语言创建下拉列表,PHP中是否有任何功能可以实现此目的。我想创建一个包含所有语言的数组并将其传递给drupal形式。

function video_upload_subtitles_form($form, &$form_state) {
 $form = array('#attributes' => array('enctype' => 'multipart/form-data'));
 $lang_list = array();//**how to create this array**
 $form['video_name'] = array(
    '#title' => t('Name Of the video'),
    '#type' => 'textfield',
 );

 $form['sub_file'] = array(
        '#type' => 'file',
        '#title' => t('Upload video'),
        '#description' => t('Pick a video file to upload.'),
    );
$form['user_list']=array(
    '#type'=>'select',
    '#title' => t('Language'),
    '#options' => $lang_list,//array of language
    '#multiple' => false,
    '#attributes'=>array('size'=>4),
    '#weight'=>8,
);
 $form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
 );

 return $form;

}

I know listing of language can be done in php by listing and making a array manually but is it possible to do with any php function, just looking for some easy way to save time.

我知道语言的列表可以通过列表和手动制作数组在PHP中完成,但它是否可以使用任何PHP函数,只是寻找一些简单的方法来节省时间。

Got this cool website for Country List . Is there anything for the Language like this

有这个很酷的国家列表网站。这样的语言有什么用吗?

2 个解决方案

#1


1  

Try this,

尝试这个,

    $select = db_select('Your table name', 's');
    $select = $select->fields('s',array('languages_name','id'));
    // 'languages_name' , 'id' this is a column
    $queried_nodes = $select->execute()
    ->fetchAllAssoc('id');
    $lang_list = array();
    foreach ($queried_nodes as $result)
    {   
       $lang_list[$result->languages_name] = t($result->languages_name);
    }

after set a $lang_list variable

设置$ lang_list变量后

$form['user_list']=array(
     '#type'=>'select',
     '#title' => t('Language'),
     '#options' => $lang_list,//array of language
     '#multiple' => false,
     '#attributes'=>array('size'=>4),
     '#weight'=>8,
);

you are create a new Language table or content types and you use a drupal inbuilt language list so try this code.

您正在创建一个新的语言表或内容类型,并使用drupal内置语言列表,所以请尝试此代码。

  include_once DRUPAL_ROOT . '/includes/iso.inc';
  $lang = _locale_get_predefined_list();

  $lang_list = array();

  foreach ($lang as $key => $value) 
  {
    $lang_list[$value[0]] = t($value[0]);
  }

after use a $lang_list varible in form api

在形式api中使用$ lang_list变量之后

#2


0  

The easiest way is probably to put all the languages you want in a table in your DB.

最简单的方法可能是将所需的所有语言放在数据库的表中。

Then you could just use the following code to create your select box:

然后你可以使用以下代码来创建你的选择框:

$languages = db_query("SELECT id, name FROM languages")->fetchAllKeyed();

$form['language'] = array(
    '#type' => 'select',
    '#title' => t('Choose language'),
    '#options' => $languages,
);

To populate your table of languages you can download a csv here.

要填充您的语言表,您可以在此处下载csv。

#1


1  

Try this,

尝试这个,

    $select = db_select('Your table name', 's');
    $select = $select->fields('s',array('languages_name','id'));
    // 'languages_name' , 'id' this is a column
    $queried_nodes = $select->execute()
    ->fetchAllAssoc('id');
    $lang_list = array();
    foreach ($queried_nodes as $result)
    {   
       $lang_list[$result->languages_name] = t($result->languages_name);
    }

after set a $lang_list variable

设置$ lang_list变量后

$form['user_list']=array(
     '#type'=>'select',
     '#title' => t('Language'),
     '#options' => $lang_list,//array of language
     '#multiple' => false,
     '#attributes'=>array('size'=>4),
     '#weight'=>8,
);

you are create a new Language table or content types and you use a drupal inbuilt language list so try this code.

您正在创建一个新的语言表或内容类型,并使用drupal内置语言列表,所以请尝试此代码。

  include_once DRUPAL_ROOT . '/includes/iso.inc';
  $lang = _locale_get_predefined_list();

  $lang_list = array();

  foreach ($lang as $key => $value) 
  {
    $lang_list[$value[0]] = t($value[0]);
  }

after use a $lang_list varible in form api

在形式api中使用$ lang_list变量之后

#2


0  

The easiest way is probably to put all the languages you want in a table in your DB.

最简单的方法可能是将所需的所有语言放在数据库的表中。

Then you could just use the following code to create your select box:

然后你可以使用以下代码来创建你的选择框:

$languages = db_query("SELECT id, name FROM languages")->fetchAllKeyed();

$form['language'] = array(
    '#type' => 'select',
    '#title' => t('Choose language'),
    '#options' => $languages,
);

To populate your table of languages you can download a csv here.

要填充您的语言表,您可以在此处下载csv。