public function DateRecurModularSierraModalForm::ajaxSubmitForm in Recurring Date Field Modular Widgets 8
Same name and namespace in other branches
- 3.x src/Form/DateRecurModularSierraModalForm.php \Drupal\date_recur_modular\Form\DateRecurModularSierraModalForm::ajaxSubmitForm()
- 2.x src/Form/DateRecurModularSierraModalForm.php \Drupal\date_recur_modular\Form\DateRecurModularSierraModalForm::ajaxSubmitForm()
File
- src/
Form/ DateRecurModularSierraModalForm.php, line 291
Class
- DateRecurModularSierraModalForm
- Generate a form designed for display in modal.
Namespace
Drupal\date_recur_modular\FormCode
public function ajaxSubmitForm(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
if ($form_state
->getErrors()) {
// Inspired by \Drupal\form_api_example\Form\ModalForm::ajaxSubmitForm.
$form['status_messages'] = [
'#type' => 'status_messages',
];
// Open the form again as a modal.
return $response
->addCommand(new OpenModalDialogCommand($this
->t('Errors'), $form, [
'width' => '575',
]));
}
$frequency = $form_state
->getValue('freq');
$parts = [];
$parts['FREQ'] = strtoupper($frequency);
$parts['INTERVAL'] = $form_state
->getValue('interval');
if (static::MODE_WEEKLY === $frequency) {
$weekDays = array_values(array_filter($form_state
->getValue('weekdays')));
$parts['BYDAY'] = implode(',', $weekDays);
}
if (static::MODE_MONTHLY === $frequency) {
$monthlyMode = $form_state
->getValue('monthly_mode');
$monthlyParts = $form_state
->getTemporaryValue([
'monthly_parts',
$monthlyMode,
]);
$parts += $monthlyParts;
}
$endsMode = $form_state
->getValue('ends_mode');
/** @var \Drupal\Core\Datetime\DrupalDateTime|array|null $endsDate */
$endsDate = $form_state
->getValue('ends_date');
// Ends mode.
if ($endsMode === DateRecurModularWidgetOptions::ENDS_MODE_OCCURRENCES) {
$parts['COUNT'] = (int) $form_state
->getValue('ends_count');
}
elseif ($endsMode === DateRecurModularWidgetOptions::ENDS_MODE_ON_DATE && $endsDate instanceof DrupalDateTime) {
$endsDateUtcAdjusted = (clone $endsDate)
->setTimezone(new \DateTimeZone('UTC'));
$parts['UNTIL'] = $endsDateUtcAdjusted
->format('Ymd\\THis\\Z');
}
// Build RRULE.
$ruleKv = [];
foreach ($parts as $k => $v) {
$ruleKv[] = "{$k}={$v}";
}
$ruleString = implode(';', $ruleKv);
// Rset cannot be casted to string yet, rebuild it here, see also
// https://github.com/rlanvin/php-rrule/issues/37
$lines = [];
$lines[] = 'RRULE:' . $ruleString;
// Preserve non-RRULE components from original string.
$originalString = $form_state
->getValue('original_string');
$rset = new RSet($originalString);
$utc = new \DateTimeZone('UTC');
$exDates = array_map(function (\DateTime $exDate) use ($utc) {
$exDate
->setTimezone($utc);
return $exDate
->format(static::UTC_FORMAT);
}, $rset
->getExDates());
if (count($exDates) > 0) {
$lines[] = 'EXDATE:' . implode(',', $exDates);
}
$collection = $this->tempStoreFactory
->get(DateRecurModularSierraWidget::COLLECTION_MODAL_STATE);
$collection
->set(DateRecurModularSierraWidget::COLLECTION_MODAL_STATE_KEY, implode("\n", $lines));
$refreshBtnName = sprintf('[name="%s"]', $collection
->get(DateRecurModularSierraWidget::COLLECTION_MODAL_STATE_REFRESH_BUTTON));
$response
->addCommand(new CloseDialogCommand())
->addCommand(new InvokeCommand($refreshBtnName, 'trigger', [
'click',
]));
return $response;
}