Skip to content

Instantly share code, notes, and snippets.

@lkacenja
Created March 22, 2018 21:07
Show Gist options
  • Save lkacenja/190afdcadb7df51a19cf31ec03e2aae2 to your computer and use it in GitHub Desktop.
Save lkacenja/190afdcadb7df51a19cf31ec03e2aae2 to your computer and use it in GitHub Desktop.
public function buildVariationForm(array &$form, FormStateInterface $form_state, $product_variation) {
$delta_key = 'section-delta-'. $product_variation->id();
$session_delta = $form_state->get($delta_key);
if (empty($session_delta)) {
$session_delta = 1;
$form_state->set($delta_key, 1);
}
$element = [];
$id = 'wrapper-' . $product_variation->id();
$element['section_container'] = [
'#type' => 'container',
'#prefix' => '<div id="' . $id . '">',
'#suffix' => '</div>',
];
$element['section_container']['section_title'] = [
'#type' => 'html_tag',
'#tag' => 'h3',
'#value' => $product_variation->getTitle()
];
for ($delta = 0; $delta < $session_delta; $delta ++) {
$element['section_container'][$delta]['date_and_time'] = [
'#type' => 'details',
'#title' => $this->t('Date and Time'),
'#open' => TRUE
];
$element['section_container'][$delta]['date_and_time']['start_date'] = [
'#type' => 'datetime',
'#title' => 'Start Date and Time',
'#default_value' => time(),
'#required' => TRUE
];
$element['section_container'][$delta]['date_and_time']['end_time'] = [
'#type' => 'datetime',
'#title' => 'End Time',
'#default_value' => time(),
'#date_date_element' => 'none',
'#date_time_element' => 'time',
'#required' => TRUE
];
}
$element['section_container']['add_session'] = [
'#type' => 'submit',
'#limit_validation_errors' => [],
'#submit' => ['::incrementDelta'],
'#value' => $this->t('Add Session'),
'#ajax' => [
'callback' => '::addSession',
'wrapper' => $id
],
'#product_variation' => $product_variation->id()
];
$form[$product_variation->id()] = $element;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment