douxu9707 2015-09-14 19:31
浏览 50
已采纳

Drupal:如何从api中检索数据并将其中的一部分用作下拉菜单中的选项

I have a drupal content type called "Event", and I'd like to create a Drupal module that retrieves data from an api and uses that data to populate a set of options in a dropdown form field on the editing interface for "Event".

I have two questions:

  1. What drupal function can I use to retrieve data from an api and store it
  2. What functions can I use within a module to create a form field for a specific type and to set the options of its dropdown menu to be data retrieved from the api.
  • 写回答

1条回答 默认 最新

  • drmgg4411 2015-09-15 23:14
    关注
    $option_terms = taxonomy_get_tree(VID_OF_THE_TAXONOMY_VOCAB);
    $options = array();
    foreach ($option_terms as $option_term) {
      $options[$option_term->tid] = $option_term->name;
    }
    
    $form['dropdown_field_name'] = array(
      '#type' => 'select',
      '#title' => t('The dropdown'),
      '#options' => $options,
    );
    
    1. Getting data from an API and storing it - Presumably there is an external endpoint you will get this JSON from. So you want to make an HTTP request there to grab it probably. So I would look at drupal_http_request. As for storing it you may wish to store it in the database so you will want to have a table created that can accept the data and then obviously put it there with SQL. It really depends on the data... maybe it would be good to use taxonomy, it depends...

    2. You will want to use hook_form() and then create a dropdown field. For example if you stored the options for the dropdown in a Drupal taxonomy, you could generate the "array of options" based on the code at the top here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”