class EditRepeatingRuleModalForm in Booking and Availability Management Tools for Drupal 8
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\bat_event_series\Form\EditRepeatingRuleModalForm
Expanded class hierarchy of EditRepeatingRuleModalForm
1 string reference to 'EditRepeatingRuleModalForm'
- bat_event_series.routing.yml in modules/
bat_event_series/ bat_event_series.routing.yml - modules/bat_event_series/bat_event_series.routing.yml
File
- modules/
bat_event_series/ src/ Form/ EditRepeatingRuleModalForm.php, line 28 - Contains \Drupal\bat_event_series\Form\EditRepeatingRuleModalForm.
Namespace
Drupal\bat_event_series\FormView source
class EditRepeatingRuleModalForm extends FormBase {
/**
* Event series object.
*
* @var \Drupal\bat_event_series\Entity\EventSeries
*/
protected $event_series;
/**
* The tempstore object.
*
* @var \Drupal\user\SharedTempStore
*/
protected $tempStore;
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilder
*/
protected $formBuilder;
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'edit_repeating_rule_modal_form';
}
/**
* Constructs a new EditRepeatingRuleModalForm object.
*
* @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
* The tempstore factory.
*/
public function __construct(PrivateTempStoreFactory $temp_store_factory, FormBuilder $formBuilder) {
$this->tempStore = $temp_store_factory
->get('edit_repeating_rule');
$this->formBuilder = $formBuilder;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.private'), $container
->get('form_builder'));
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, EventSeries $bat_event_series = NULL) {
$this->event_series = $bat_event_series;
$event_series_type = bat_event_series_type_load($bat_event_series
->bundle());
$start_date = new \DateTime($bat_event_series
->get('event_dates')->value);
$end_date = new \DateTime($bat_event_series
->get('event_dates')->end_value);
$rrule = RfcParser::parseRRule($bat_event_series
->getRRule());
$event_type = $event_series_type
->getEventGranularity();
$form['errors'] = [
'#markup' => '<div class="form-validation-errors"></div>',
];
$form['start_date'] = [
'#type' => $event_type == 'bat_daily' ? 'date' : 'datetime',
'#title' => t('Start date'),
'#default_value' => $event_type == 'bat_daily' ? $start_date
->format('Y-m-d') : new DrupalDateTime($start_date
->format('Y-m-d H:00')),
'#date_increment' => 60,
'#required' => TRUE,
];
$form['end_date'] = [
'#type' => $event_series_type
->getEventGranularity() == 'bat_daily' ? 'date' : 'datetime',
'#title' => t('End date'),
'#default_value' => $event_type == 'bat_daily' ? $end_date
->format('Y-m-d') : new DrupalDateTime($end_date
->format('Y-m-d H:00')),
'#date_increment' => 60,
'#required' => TRUE,
];
$form['repeat_frequency'] = [
'#type' => 'select',
'#title' => t('Repeat frequency'),
'#options' => [
'daily' => t('Daily'),
'weekly' => t('Weekly'),
'monthly' => t('Monthly'),
],
'#default_value' => isset($rrule['FREQ']) ? strtolower($rrule['FREQ']) : '',
'#required' => TRUE,
];
$form['repeat_until'] = [
'#type' => 'date',
'#title' => t('Repeat until'),
'#default_value' => isset($rrule['UNTIL']) ? $rrule['UNTIL']
->format('Y-m-d') : '',
'#required' => TRUE,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Update this event series'),
'#attributes' => [
'class' => [
'button--primary',
],
],
'#ajax' => [
'callback' => [
$this,
'ajaxSubmit',
],
'url' => Url::fromRoute('entity.bat_event_series.edit_form_modal', [
'bat_event_series' => $bat_event_series
->id(),
]),
'options' => [
'query' => [
FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
],
],
],
];
$form['actions']['cancel'] = [
'#type' => 'submit',
'#value' => $this
->t('Cancel'),
'#attributes' => [
'class' => [
'button--danger',
'dialog-cancel',
],
],
];
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$start_date = $values['start_date'];
$end_date = $values['end_date'];
$event_series_type = bat_event_series_type_load($this->event_series
->bundle());
$event_type = $event_series_type
->getEventGranularity();
if ($event_type == 'bat_daily') {
$start_date = new \DateTime($start_date);
$end_date = new \DateTime($end_date);
}
$date_start_date = $start_date
->format('Y-m-d');
$date_end_date = $end_date
->format('Y-m-d');
$dates_valid = TRUE;
if ($event_type == 'bat_hourly') {
// Validate the input dates.
if (!$start_date instanceof DrupalDateTime) {
$form_state
->setErrorByName('start_date', $this
->t('The start date is not valid.'));
$dates_valid = FALSE;
}
if (!$end_date instanceof DrupalDateTime) {
$form_state
->setErrorByName('end_date', $this
->t('The end date is not valid.'));
$dates_valid = FALSE;
}
}
if ($dates_valid) {
if ($end_date <= $start_date) {
$form_state
->setErrorByName('end_date', $this
->t('End date must be after the start date.'));
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = [
'start_date' => $form_state
->getValue('start_date'),
'end_date' => $form_state
->getValue('end_date'),
'repeat_frequency' => $form_state
->getValue('repeat_frequency'),
'repeat_until' => $form_state
->getValue('repeat_until'),
];
$this->tempStore
->set($this
->currentUser()
->id(), $values);
}
public function ajaxSubmit(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$messages = [
'#type' => 'status_messages',
];
$response
->addCommand(new HtmlCommand('.form-validation-errors', $messages));
if (!$form_state
->getErrors()) {
$response
->addCommand(new CloseModalDialogCommand());
$modal_form = $this->formBuilder
->getForm('Drupal\\bat_event_series\\Form\\EditRepeatingRuleConfirmationModalForm', $this->event_series);
$modal_form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$response
->addCommand(new OpenModalDialogCommand($this
->t('Edit repeating rule'), $modal_form, [
'width' => 600,
]));
}
return $response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EditRepeatingRuleModalForm:: |
protected | property | Event series object. | |
EditRepeatingRuleModalForm:: |
protected | property | The form builder. | |
EditRepeatingRuleModalForm:: |
protected | property | The tempstore object. | |
EditRepeatingRuleModalForm:: |
public | function | ||
EditRepeatingRuleModalForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
EditRepeatingRuleModalForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
EditRepeatingRuleModalForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
EditRepeatingRuleModalForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
EditRepeatingRuleModalForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
EditRepeatingRuleModalForm:: |
public | function | Constructs a new EditRepeatingRuleModalForm object. | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |