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

发布于 2024-12-02 07:54:13 字数 3788 浏览 1 评论 0原文

我正在尝试实现的目标

是构建一个收藏夹模块,并且我需要能够:

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

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

我的代码是如何设置的

这是我的形式:

/*
 * 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 与我的数组进行比较以及如何相应地修改差异。

我希望我做得很好解释我是什么尝试去做。
实现这一目标的最佳方法是什么?

What I'm trying to accomplish

I'm building a favorites module and I need the ability to:

  • Select from a dropdown, hardcoded list of options
  • Have it save to the database
  • Upon refreshing the page, remove the already saved option from the list of options so it may not be added again

The third part is where I am unsure of how to proceed.

How my code is set up

This is my form:

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

The name of the paths/options are called via a reference array:

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

And all this is what grabs the data on the databse:

/*
 * 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);
}

Possible Solutions

I've been told that I could used hook_form_alter() in order to modify my array but I am unsure as to when I should be comparing the db_query to my array and how to modify the differences accordingly.

I hope I've done a good job explaining what I'm try to do.
What would be the best way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆 2024-12-09 07:54:13

您不应该从数据库中获取它们,而不是将每个响应写入 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() 当然应该替换为您自己的表。

PS:hook_form() 不存在:)

Instead of writing every response in f25_favorites_listOfPaths(), shouldn't you get them from the database?

You can then change whatever you want in the submit function to the database so that you don't fetch again the previously selected answer.

Example :

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

The use of variable_get/set() should of course be replaced by your own table if you have too much datas.

P.S. : hook_form() does not exist :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文