up vote 0 down vote favorite
share [fb]

Here is a small PHP code which I am not clear with.

/**
* Form for configurable Drupal action to beep multiple times
*/
function beep_multiple_beep_action_form($context) {
$form['beeps'] = array(
'#type' => 'textfield',
'#title' => t('Number of beeps'),
'#description' => t('Enter the number of times to beep when this action executes'),
'#default_value' => isset($context['beeps']) ? $context['beeps'] : '1',
'#required' => TRUE,
);
return $form;
}

In the above user defined function return $form what does it return? How do you access the individual elements of $form['beeps'] what is the significance of writing word beeps in square braces in $form['beeps']

function beep_multiple_beep_action_validate($form, $form_state) {
        $beeps = $form_state['values']['beeps'];
        if (!is_int($beeps)) {
        form_set_error('beeps', t('Please enter a whole number between 0 and 10.'));
        }
        else if ((int) $beeps > 10 ) {
        form_set_error('beeps', t('That would be too annoying. Please choose fewer than 10
        beeps.'));
        } else if ((int) $beeps < 0) {
        form_set_error('beeps', t('That would likely create a black hole! Beeps must be a
        positive integer.'));
        }
}

In above code what is

$beeps = $form_state['values']['beeps'];

line doing?

link|improve this question
feedback

1 Answer

up vote 2 down vote accepted

In your first section of code, that function is creating a form using Drupal's Form API and $form['beeps'] contains the settings for a single form element, in this case a textfield.

The second function is the one is used to validate the form values and will display an error for any unacceptable value.

$beeps = $form_state['values']['beeps'] fills the variable $beeps with the value of the "beeps" textfield from the form generated in the first function.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.