根据已保存的$ user更改硬编码选项

时间:2011-08-31 14:47:39

标签: drupal-6 drupal-modules drupal-fapi

我想要完成的事情

我正在建立一个收藏夹模块,我需要能够:

  • 从下拉列表中选择硬编码的选项列表
  • 将其保存到数据库
  • 刷新页面后,从选项列表中删除已保存的选项,以便可能不会再次添加

第三部分是我不确定如何继续的地方。

我的代码是如何设置的

这是我的表格:

/*
 * Implentation of hook_form().
 */
function f25_favorites_form() {
  $listOfPaths = f25_favorites_listOfPaths();

  $form['path_options'] = array(
    '#type' => 'value',
    '#value' => array(
      'default' => $listOfPaths['default']['#title'],
      'concierge' => $listOfPaths['concierge']['#title'],
      'concierge/add' => $listOfPaths['concierge/add']['#title'],
      'survey-questions' => $listOfPaths['survey-questions']['#title'],
      'survey-questions/add' => $listOfPaths['survey-questions/add']['#title'],
      'profiles' => $listOfPaths['profiles']['#title'],
      'profiles/add' => $listOfPaths['profiles/add']['#title'],
      'statistics' => $listOfPaths['statistics']['#title'],
    )
  ); 

  $form['path'] = array(
    '#type' => 'select',
    '#title' => t('Select Page'),
    '#required' => TRUE,
    '#weight' => '11',
    '#options' => $form['path_options']['#value'],
  );

  $form[submit] = array(
    '#type' => 'submit',
    '#weight' => '1000000',
    '#value' => t('Add')
  );

  return $form;
}

路径/选项的名称通过参考数组调用:

/*
 * List of Paths to add to favorites
 */
function f25_favorites_listOfPaths() {
  $list = array();
  $list = array(
    'default' => array(
      '#title' => t('Add to favorites'), 
      ),
    'concierge' => array(
      '#title' => t('Concierge'), 
      '#image' => drupal_get_path('module', 'f25_favorites').'/img/concierge.png',
      '#desc' => t('Concierge'), 
      ),
    'concierge/add' => array(
      '#title' => t('New Concierge'), 
      '#image' => drupal_get_path('module', 'f25_favorites').'/img/concierge.png',
      '#desc' => t('Concierge > Add'), 
      ),
    'survey-questions' => array(
      '#title' => t('Survey Questions'), 
      '#image' => drupal_get_path('module', 'f25_favorites').'/img/survey-questions.png',
      '#desc' => t('Current Survey Questions'), 
      ),
    'survey-questions/add' => array(
      '#title' => t('New Survey Question'), 
      '#image' => drupal_get_path('module', 'f25_favorites').'/img/survey-questions.png',
      '#desc' => t('Survery Question > Add'), 
      ),
    'profiles' => array(
      '#title' => t('Profiles'), 
      '#image' => drupal_get_path('module', 'f25_favorites').'/img/profiles.png',
      '#desc' => t('User Profiles'), 
      ),
    'profiles/add' => array(
      '#title' => t('Add Profile'), 
      '#image' => drupal_get_path('module', 'f25_favorites').'/img/profiles.png',
      '#desc' => t('Profiles > Add'), 
      ),
    'statistics' => array(
      '#title' => t('Statistics'), 
      '#image' => drupal_get_path('module', 'f25_favorites').'/img/statistics.png',
      '#desc' => t('Performance Stats'), 
      ),
  );
  return $list;
}

所有这些都是抓取数据库上的数据:

/*
 * Write Form data to database
 */
function f25_favorites_form_submit($form, &$form_state){
  global $user;
  $listOfPaths = f25_favorites_listOfPaths();
  $selected = $form_state['values']['path'];

  $data = array(
    'uid' => $user->uid,
    'path' => $selected,
    'title' => $listOfPaths[$selected]['#title'],
    'weight' => 10,
    'timestamp' => time(),
  );

  drupal_write_record(f25_favorites, $data);
}

可能的解决方案

我被告知我可以使用hook_form_alter()来修改我的数组,但我不确定何时应该将db_query与我的数组进行比较以及如何相应地修改差异。
< / p>

我希望我已经做好了解释我尝试做的事情。
实现这一目标的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

不是在f25_favorites_listOfPaths()中写下每个响应,你不应该从数据库中获取它们吗?

然后,您可以将提交功能中的任何内容更改为数据库,以便您不再获取以前选择的答案。

示例:

function f25_favorites_listOfPaths() {
  return variable_get('f25_favorites_array_' . $user->uid, array(
    // your $list array
  ));
}

function f25_favorites_submit_form($form, &$form_state) {
  // your stuff already
  drupal_write_record(f25_favorites, $data);

  // Now what I propose you to do :)
  variable_set('f25_favorites_array_' . $user->uid, array(
    // new $list array without the favorite selected
  ));
}

如果你有太多的数据,那么当然应该用你自己的表替换variable_get / set()的使用。

P.S。 :hook_form()不存在:)