0

I have a webform with 20 different select options under different fieldsets and they all need to have a custom drop down select field title. To achieve that you use this:

function theme_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'webform_client_form_653') {
    $form['submitted']['products']['product_1']['quantity']['#empty_option'] = t('Qty');
  }
}

Now each form is identical except the ['product_1'], so to make that custom title work on every field is that you have to copy paste 20 lines of same code and just rename the ['product_1'] to ['product_2'], ['product_3'] and the etc.

I was thinking is there some method like in CSS where you can use wildcards, e.g. (to get the idea): https://stackoverflow.com/questions/5110249/wildcard-in-css-for-classes

So you could achieve all that with just one line of code and apply it to all quantity fields in the webform.

So instead of spamming:

$form['submitted']['products']['product_1']['quantity']['#empty_option'] = t('Qty');
$form['submitted']['products']['product_2']['quantity']['#empty_option'] = t('Qty');
$form['submitted']['products']['product_3']['quantity']['#empty_option'] = t('Qty');
And so on...

You could do something like

$form['submitted']['products']['*']['quantity']['#empty_option'] = t('Qty');

But it doesn't work.

1
  • I'm not sure about a wildcard; but another approach could be to to SQL search to find out the number of selections and then to use 'foreach' .
    – Deejay
    Jan 22 '15 at 14:16
2

This seems more like a general PHP question, but I'll answer it anyway with some Drupal context.

You usually loop through the array in these instances. You'll find looping as you code in Drupal more and more common.

Consider the following structure:

$form['submitted']['products']['product_1']['quantity']['#empty_option']

Assuming that you're editing the ['quantity']['#empty_option'] of each [product_x], you loop through your array like so:

foreach(element_children($form['submitted']['products']) as $key) {
  $form['submitted']['products'][$key]['quantity']['#empty_option'] = t('Qty');
}

element_children gives you the keys for the array which don't start with a hash #. Whether or not it's suitable for your particular array is a different matter. You can inspect your variable using var_dump($variable) or through Devel module's dpm($variable, 'label'); function.

You may find that you may need to run some if statements to test if $key starts with product_ if you have more than product_x keys under $form['submitted']['products']

foreach(element_children($form['submitted']['products']) as $key) {
  // Only proceed if array key starts with product_
  if (substr($key, 0, 8) == 'product_') {
    $form['submitted']['products'][$key]['quantity']['#empty_option'] = t('Qty');
  }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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