I have a webform with a custom composite element that has the following fields item,quantity,po upload,order type,completion date of type text,number,file,select and date respectively.

now in hook_form_formid_alter I am trying to add multiple rows to webform composite element based on the values of the select field. but it is not working it only returns single empty element visible.

I am using the following code.

<?php
function webform_views_form_webform_submission_ordering_process_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['elements']['order_type_']['#ajax'] = [
    'callback' => 'update_items',
    'event' => 'change',
    'wrapper' => 'items_table',
    'progress' => [
      'type' => 'none',
    ],
  ];
   $form['elements']['section_type_']['#ajax'] = [
    'callback' => 'update_items',
    'event' => 'change',
    'progress' => [
      'type' => 'none',
    ],
  ]; 
  $section_type = $form_state->getValue('section_type_');
     $order_type = $form_state->getValue('order_type_');
     $items_value = $form_state->getValue('items');
     if($section_type != null && $order_type != null){
         if($section_type == 'Showroom'){
               if($order_type == 'Bought out'){
                $items = array('item1','item2','item3');
               }
               elseif($order_type == 'Customised'){
               $items = array('item4','item5','item6','item7');
               }

         }
     elseif($section_type == 'Workshop'){
            if($order_type == 'Bought out'){
            $items = array('item8','item9','item10','item11','item12');
            }
           elseif($order_type == 'Customised'){
            $items = array('item13','item14','item15','item16','item17','item18');
            }
     }

  foreach($items as $index => $value){
  print_r($index);
  $values['items'][] = [
   'item' => $value, 'quantity' => '', 'po_upload' =>['fids' => '', 'upload' => null, 'upload_button' => 'Upload', 'remove_button' => 'Remove'], 'order_type' => '', 'completion_date' => ''
  ];
   $form['elements']['items']['items'][$index]['item'] = [
        '#type' => 'textfield',
        '#title' => 'Item',
        '#value' => $value,
        '#title_display' => 'invisible',
        '#_webform_states' => [
        ]
        ];
     $form['elements']['items']['items'][$index]['quantity'] = [
        '#type' => 'number',
        '#title' => 'Quantity',
        '#title_display' => 'invisible',
        '#_webform_states' => [
        ]
        ];
    $form['elements']['items']['items'][$index]['po_upload'] = [
        '#type' => 'managed_file',
        '#title' => 'PO upload',
        '#title_display' => 'invisible',
        '#_webform_states' => [
          ]
        ];
    $form['elements']['items']['items'][$index]['order_type'] = [
        '#type' => 'select',
        '#empty_option' => t('-select-'),
        '#options' => [
        'Full order' => 'Full order',
        'Partial order' => 'Partial order'
        ],
        '#title' => 'Order type',
        '#title_display' => 'invisible',
        '#_webform_states' => [
        ]
        ];
    $form['elements']['items']['items'][$index]['completion_date'] = [
        '#type' => 'date',
        '#title' => 'Completion Date',
        '#title_display' => 'invisible',
        '#datepicker' => true,
        '#date_date_format' => 'd/m/Y',
        '#_webform_states' => [
        ]
        ];
   }
   $form_state->setValue('items',$values);

  $form_state->setRebuild();
  } 

}


function update_items(&$form, FormStateInterface $form_state){
 // print_r(json_encode($form_state->getValue('items')));
  return $form['elements']['items'];
}
?>

Comments

Lokeshwari created an issue. See original summary.

jrockowitz’s picture

Status: Active » Closed (outdated)
The Webform module's issue queue is for bugs, feature requests, and planning. Please post general support questions to Drupal Answers. You can also ask questions or help answer questions on the #webform channel on Drupal Slack.

Ajax in Drupal is very tricky. I recommend checking out https://www.drupal.org/project/examples