开发者

Alter a hardcoded form of options based on $user already saved

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?


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜