class AppointmentCalendarListForm in Appointment Calendar 8
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\appointment_calendar\Form\AppointmentCalendarListForm
Expanded class hierarchy of AppointmentCalendarListForm
1 string reference to 'AppointmentCalendarListForm'
File
- src/
Form/ AppointmentCalendarListForm.php, line 11
Namespace
Drupal\appointment_calendar\FormView source
class AppointmentCalendarListForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'appointment_calendar_list_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
global $base_url;
// Default year.
$default_year = date('Y', time());
$beginOfDay = strtotime("midnight", time());
$endOfDay = strtotime("tomorrow", time()) - 1;
$from_date = \Drupal::request()->query
->get('date');
$to_date = \Drupal::request()->query
->get('todate');
if ($from_date == '') {
$from_date = DrupalDateTime::createFromTimestamp($beginOfDay);
}
else {
$from_date = DrupalDateTime::createFromTimestamp($from_date);
}
if ($to_date == '') {
$to_date = DrupalDateTime::createFromTimestamp($endOfDay);
}
else {
$to_date = DrupalDateTime::createFromTimestamp($to_date);
}
$form['filter_date'] = [
'#type' => 'datetime',
'#title' => t('From date'),
'#date_date_element' => 'date',
'#date_time_element' => 'none',
'#date_year_range' => $default_year . ':+3',
'#default_value' => $from_date,
];
$form['filter_to_date'] = [
'#type' => 'datetime',
'#title' => t('To date'),
'#date_date_element' => 'date',
'#date_time_element' => 'none',
'#date_year_range' => $default_year . ':+3',
'#default_value' => $to_date,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Filter'),
];
$form['reset'] = [
'#type' => 'submit',
'#value' => t('Reset'),
];
// Headers.
$headers = [
t('Date'),
t('No. Slots'),
t('Operations'),
];
$db_conn = \Drupal::database();
$date_query = $db_conn
->select('appointment_date', 'ad');
$date_query
->fields('ad');
if (\Drupal::request()->query
->get('date')) {
$date_query
->condition('date', \Drupal::request()->query
->get('date'), '>=');
}
if (\Drupal::request()->query
->get('todate')) {
$date_query
->condition('date', \Drupal::request()->query
->get('todate'), '<=');
}
$date_query
->orderBy('date');
$table_sort = $date_query
->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
->orderBy('date');
$pager = $table_sort
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->limit(25);
$date_result = $pager
->execute();
$rows = [];
foreach ($date_result as $date) {
$capacity = appointment_calendar_slot_capacity($date->date);
$slots = count((array) json_decode($capacity));
$view = Html::escape($base_url . '/admin/appointment-calendar/view?date=' . $date->date);
$edit = Html::escape($base_url . '/admin/appointment-calendar/edit?date=' . $date->date);
$delete = Html::escape($base_url . '/admin/appointment-calendar/delete?date=' . $date->date);
$row = [];
$row[] = date('Y-m-d', $date->date);
$row[] = $slots;
$row[] = Markup::create('<div><a href="' . $view . '">' . t('View') . '</a></div><div><a href="' . $edit . '">' . t('Edit') . '</a></div><div><a href="' . $delete . '">' . t('Delete') . '</a></div>');
$rows[] = $row;
}
$form['data'] = [
'#theme' => 'table',
'#header' => $headers,
'#rows' => $rows,
];
$form['pager'] = [
'#type' => 'pager',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$op = (string) $values['op'];
if ($op == $this
->t('Filter')) {
$filter_date = $values['filter_date']
->getTimestamp();
$filter_to_date = $values['filter_to_date']
->getTimestamp();
if ($filter_date > $filter_to_date) {
$form_state
->setErrorByName('filter_date', $this
->t('From Date is greater than "TO" date'));
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$op = (string) $values['op'];
// Goto current path if reset.
if ($op == $this
->t('Reset')) {
$form_state
->setRedirect('appointment_calendar.list_page');
}
// Pass values to url.
if ($op == $this
->t('Filter')) {
$filter_date = $values['filter_date']
->getTimestamp();
$filter_to_date = $values['filter_to_date']
->getTimestamp();
$params['date'] = Html::escape($filter_date);
$params['todate'] = Html::escape($filter_to_date);
$form_state
->setRedirect('appointment_calendar.list_page', [
$params,
]);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AppointmentCalendarListForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
AppointmentCalendarListForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
AppointmentCalendarListForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
AppointmentCalendarListForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
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 | |
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:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
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. |