Make compatible with Drupal 9+
This commit is contained in:
1 parent
6bbb40d61d
commit
b6799a5ba5
3 files changed
+32
-60
No files matched your search
@@ -2,7 +2,8 @@ name: Reservations
|
|||||||
description: Reservations module. Make possible creation of reservation system on your website.
|
description: Reservations module. Make possible creation of reservation system on your website.
|
||||||
#package: Other
|
#package: Other
|
||||||
type: module
|
type: module
|
||||||
core: 8.x
|
#core: 8.x
|
||||||
|
core_version_requirement: ^8 || ^9.2 || ^10
|
||||||
dependencies:
|
dependencies:
|
||||||
- drupal:node
|
- drupal:node
|
||||||
- drupal:path
|
- drupal:path
|
||||||
|
|||||||
+8
-19
@@ -1,31 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
* Module file for reservations.
|
* Module file for reservations.
|
||||||
*/
|
*/
|
||||||
//function reservations_theme() {
|
|
||||||
// return array(
|
|
||||||
// 'reservations' => array(
|
|
||||||
// 'template' => 'reservations.reservation_add',
|
|
||||||
// 'variables' => array('title' => NULL),
|
|
||||||
// ),
|
|
||||||
// );
|
|
||||||
//}
|
|
||||||
|
|
||||||
/* Implements hook_form_FORM_ID_alter() */
|
/* Implements hook_form_FORM_ID_alter() */
|
||||||
function reservations_form_node_courts_form_alter(&$form, &$form_state, $form_id) {
|
function reservations_form_node_courts_form_alter(&$form, &$form_state, $form_id) {
|
||||||
$form['#after_build'][] = 'reservations_add_css_in_admin_part';
|
$form['#after_build'][] = 'reservations_add_css_in_admin_part';
|
||||||
}
|
}
|
||||||
function reservations_form_node_courts_edit_form_alter(&$form, &$form_state, $form_id) {
|
function reservations_form_node_courts_edit_form_alter(&$form, &$form_state, $form_id) {
|
||||||
$form['#after_build'][] = 'reservations_add_css_in_admin_part';
|
$form['#after_build'][] = 'reservations_add_css_in_admin_part';
|
||||||
}
|
}
|
||||||
|
|
||||||
function reservations_add_css_in_admin_part($form, &$form_state) {
|
function reservations_add_css_in_admin_part($form, &$form_state) {
|
||||||
// $style_path = drupal_get_path('module','reservations') . '/css/reservations.css';
|
|
||||||
// dpm($form);
|
|
||||||
// drupal_add_css('file', $style_path);
|
|
||||||
$form['#attached']['library'][] = 'reservations/reservation_admin';
|
$form['#attached']['library'][] = 'reservations/reservation_admin';
|
||||||
return $form;
|
return $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,6 +25,7 @@ class ReservationAddForm extends FormBase {
|
|||||||
$form = array();
|
$form = array();
|
||||||
|
|
||||||
$query = \Drupal::entityQuery('node');
|
$query = \Drupal::entityQuery('node');
|
||||||
|
$query->accessCheck(FALSE);
|
||||||
$query->condition('status', 1);
|
$query->condition('status', 1);
|
||||||
$query->condition('type', 'courts');
|
$query->condition('type', 'courts');
|
||||||
$courtsIds = $query->execute();
|
$courtsIds = $query->execute();
|
||||||
@@ -38,16 +39,15 @@ class ReservationAddForm extends FormBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$query2 = \Drupal::entityQuery('node');
|
$query2 = \Drupal::entityQuery('node');
|
||||||
|
$query2->accessCheck(FALSE);
|
||||||
$query2->condition('status', 1);
|
$query2->condition('status', 1);
|
||||||
$query2->condition('type', 'reservations');
|
$query2->condition('type', 'reservations');
|
||||||
$query2->condition('field_reservation_time', time(), '>');
|
$query2->condition('field_reservation_time', time(), '>');
|
||||||
$reservationIds = $query2->execute();
|
$reservationIds = $query2->execute();
|
||||||
$reservations = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($reservationIds);
|
$reservations = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($reservationIds);
|
||||||
$reservationsLst = array();
|
$reservationsLst = array();
|
||||||
// dpm($reservation->field_reservation_time->value);
|
|
||||||
foreach ($reservations as $reservation) {
|
foreach ($reservations as $reservation) {
|
||||||
$court = $reservation->field_court->target_id;
|
$court = $reservation->field_court->target_id;
|
||||||
// $reservationsLst[date("j.m.Y", $reservation->field_reservation_time->value)][] = date("H:i", $reservation->field_reservation_time->value);
|
|
||||||
$reservationsLst[$court][date("d.m.Y", strtotime($reservation->field_reservation_time->value))][] = date("H:i", strtotime($reservation->field_reservation_time->value));
|
$reservationsLst[$court][date("d.m.Y", strtotime($reservation->field_reservation_time->value))][] = date("H:i", strtotime($reservation->field_reservation_time->value));
|
||||||
}
|
}
|
||||||
$reservationArr = array();
|
$reservationArr = array();
|
||||||
@@ -58,13 +58,10 @@ class ReservationAddForm extends FormBase {
|
|||||||
|
|
||||||
}
|
}
|
||||||
foreach ($court_reservations as $date => $arr) {
|
foreach ($court_reservations as $date => $arr) {
|
||||||
// dpm($arr);
|
|
||||||
$reservationArr["'".$court."'"][$date] = "['" . implode("','", $arr). "']";
|
$reservationArr["'".$court."'"][$date] = "['" . implode("','", $arr). "']";
|
||||||
|
|
||||||
}
|
}
|
||||||
// echo'reservations["' . $key . '"]=["' . implode('","', $value) . '"];';
|
|
||||||
}
|
}
|
||||||
//dpm($reservationArr);
|
|
||||||
$form['message'] = array(
|
$form['message'] = array(
|
||||||
'#markup' => $this->t('Make your reservation.'),
|
'#markup' => $this->t('Make your reservation.'),
|
||||||
);
|
);
|
||||||
@@ -90,25 +87,13 @@ class ReservationAddForm extends FormBase {
|
|||||||
'#title' => t('Your phone') . ":",
|
'#title' => t('Your phone') . ":",
|
||||||
'#size' => 15,
|
'#size' => 15,
|
||||||
'#required' => TRUE
|
'#required' => TRUE
|
||||||
// '#description' => t("Values greater than 127 will cause an exception. Try it - it's a great example why exception handling is needed with DTBNG."),
|
|
||||||
);
|
);
|
||||||
$form['court'] = array(
|
$form['court'] = array(
|
||||||
'#type' => 'select',
|
'#type' => 'select',
|
||||||
'#title' => t('Court') . ":",
|
'#title' => t('Court') . ":",
|
||||||
'#options' => $courtsLst,
|
'#options' => $courtsLst,
|
||||||
'#required' => TRUE,
|
'#required' => TRUE,
|
||||||
'#attributes' => array('id' => 'court'),
|
'#attributes' => array('id' => 'court')
|
||||||
// '#options' => [
|
|
||||||
// '1' => $this->t('One'),
|
|
||||||
// '2' => $this->t('Two'),
|
|
||||||
// //'2' => [
|
|
||||||
// //'2.1' => $this->t('Two point one'),
|
|
||||||
// //'2.2' => $this->t('Two point two'),
|
|
||||||
// //],
|
|
||||||
// '3' => $this->t('Three'),
|
|
||||||
// ]
|
|
||||||
//'#size' => 15,
|
|
||||||
//'#attributes' => array('id'=>'datetimepicker', 'class' => array('datetimepicker')),
|
|
||||||
);
|
);
|
||||||
$form['reservation_time'] = array(
|
$form['reservation_time'] = array(
|
||||||
'#type' => 'textfield',
|
'#type' => 'textfield',
|
||||||
@@ -140,10 +125,6 @@ class ReservationAddForm extends FormBase {
|
|||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||||
// Confirm that age is numeric.
|
|
||||||
// if (empty($form_state->getValue('pname'))) {
|
|
||||||
// $form_state->setErrorByName('pname', $this->t('Please fill your name.'));
|
|
||||||
// }
|
|
||||||
if (empty($form_state->getValue('pemail'))) {
|
if (empty($form_state->getValue('pemail'))) {
|
||||||
$form_state->setErrorByName('pemail', $this->t('Please fill your e-mail.'));
|
$form_state->setErrorByName('pemail', $this->t('Please fill your e-mail.'));
|
||||||
}
|
}
|
||||||
@@ -162,19 +143,6 @@ class ReservationAddForm extends FormBase {
|
|||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||||
// Gather the current user so the new record has ownership.
|
|
||||||
// $account = $this->currentUser();
|
|
||||||
// // Save the submitted entry.
|
|
||||||
// $entry = array(
|
|
||||||
// 'name' => $form_state->getValue('name'),
|
|
||||||
// 'surname' => $form_state->getValue('surname'),
|
|
||||||
// 'age' => $form_state->getValue('age'),
|
|
||||||
// 'uid' => $account->id(),
|
|
||||||
// );
|
|
||||||
// $return = DbtngExampleStorage::insert($entry);
|
|
||||||
// if ($return) {
|
|
||||||
// drupal_set_message(t('Created entry @entry', array('@entry' => print_r($entry, TRUE))));
|
|
||||||
// }
|
|
||||||
//Get vlaues from the form
|
//Get vlaues from the form
|
||||||
$pname = $form_state->getValue('pname');
|
$pname = $form_state->getValue('pname');
|
||||||
$pemail = $form_state->getValue('pemail');
|
$pemail = $form_state->getValue('pemail');
|
||||||
@@ -183,10 +151,13 @@ class ReservationAddForm extends FormBase {
|
|||||||
$reservation_time = $form_state->getValue('reservation_time');
|
$reservation_time = $form_state->getValue('reservation_time');
|
||||||
$note = $form_state->getValue('note');
|
$note = $form_state->getValue('note');
|
||||||
|
|
||||||
// $user = $this->currentUser();
|
|
||||||
// drupal_set_message(print_r($courts));
|
|
||||||
//Create an Entity.
|
//Create an Entity.
|
||||||
|
if(function_exists('entity_create')){
|
||||||
$entry = entity_create('node', array('type' => 'reservations'));
|
$entry = entity_create('node', array('type' => 'reservations'));
|
||||||
|
}else{
|
||||||
|
$entry = \Drupal::entityTypeManager()->getStorage('node')->create(['type' => 'reservations']);
|
||||||
|
}
|
||||||
|
|
||||||
//Specify the author.
|
//Specify the author.
|
||||||
$entry->uid = \Drupal::currentUser()->id();
|
$entry->uid = \Drupal::currentUser()->id();
|
||||||
@@ -196,10 +167,14 @@ class ReservationAddForm extends FormBase {
|
|||||||
$entry->field_person_phone->value = $phone;
|
$entry->field_person_phone->value = $phone;
|
||||||
$entry->field_person_e_mail->value = $pemail;
|
$entry->field_person_e_mail->value = $pemail;
|
||||||
$entry->field_court->target_id = $court;
|
$entry->field_court->target_id = $court;
|
||||||
// $entry->field_reservation_time->value = \Drupal::service('date.formatter')->format(time(), 'html_datetime');
|
if(defined('DATETIME_DATETIME_STORAGE_FORMAT')){
|
||||||
$entry->field_reservation_time->value = date(DATETIME_DATETIME_STORAGE_FORMAT, strtotime($reservation_time));
|
$entry->field_reservation_time->value = date(DATETIME_DATETIME_STORAGE_FORMAT, strtotime($reservation_time));
|
||||||
|
}else{
|
||||||
|
$entry->field_reservation_time->value = date(\Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATETIME_STORAGE_FORMAT, strtotime($reservation_time));
|
||||||
|
}
|
||||||
$entry->field_note->value = $note;
|
$entry->field_note->value = $note;
|
||||||
$entry->save();
|
$entry->save();
|
||||||
|
if(function_exists('drupal_set_message')){
|
||||||
drupal_set_message(t('Your reservation has been created.'));
|
drupal_set_message(t('Your reservation has been created.'));
|
||||||
drupal_set_message(t('We will see you on @date at @hour', array(
|
drupal_set_message(t('We will see you on @date at @hour', array(
|
||||||
'@date' => date('d.m.Y', strtotime($reservation_time)),
|
'@date' => date('d.m.Y', strtotime($reservation_time)),
|
||||||
@@ -207,6 +182,15 @@ class ReservationAddForm extends FormBase {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
}else{
|
||||||
|
\Drupal::messenger()->addStatus(t('Your reservation has been created.'));
|
||||||
|
\Drupal::messenger()->addStatus(t('We will see you on @date at @hour', array(
|
||||||
|
'@date' => date('d.m.Y', strtotime($reservation_time)),
|
||||||
|
'@hour' => date('H:i', strtotime($reservation_time))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function makeAllowedHours($court=null){
|
private function makeAllowedHours($court=null){
|
||||||
@@ -232,8 +216,6 @@ class ReservationAddForm extends FormBase {
|
|||||||
}
|
}
|
||||||
$allowedHours["'".$key."'"] = implode(",", $allowedHoursTmp);
|
$allowedHours["'".$key."'"] = implode(",", $allowedHoursTmp);
|
||||||
}
|
}
|
||||||
// dpm($allowedHours);
|
|
||||||
|
|
||||||
return $allowedHours;
|
return $allowedHours;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user