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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题