سؤال

I have a hook_form() and I'm trying to submit the data to the database.
When the form options generates, it generates like <option value="foo">Foo</option>

I'm not trying to get the 'value' of the option.
I'm trying to grab the part in-between the <option> which would be 'Foo'

My form looks like this:

/*
 * Implentation of hook_form().
 */
function f25_favorites_form() {
  $form['path_options'] = array(
    '#type' => 'value',
    '#value' => array('default' => t('Add a favorite'), 'foo' => t('Foo'), 'bar' => t('Bar')),
  );  

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

And this is the form_submit():

/*
 * Write Form data to database
 */
function f25_favorites_form_submit($form, &$form_state){
  global $user;
  $data = array(
    'uid' => $user->uid,
    'path' => $form_state['values']['path'],
    'title' => $title,
    'weight' => $weight,
    'timestamp' => time(),
  );

  drupal_write_record('', $data);
}

What I'm expecting

I'm think of having something similar to this:

    'path' => $form_state['values']['path'],

But instead be something like this:

    'title' => $form_state['values']['path'][#value],
هل كانت مفيدة؟

المحلول

According to Drupal docs $form variable contains data from f25_favorites_form(), so this should work:

'title' => $form['path_options']['#value'][$form_state['values']['path']],

Basically you would take value that is selected (received through $form_state['values']['path']) and since it is a key for $form['path_options']['#value'] array, you should find the text by using it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top