diff --git a/config/schema/recurring_events.schema.yml b/config/schema/recurring_events.schema.yml index 668621c663303207fbea51bcf10f85188587f359..9de82051dd7a1ac4885c41e60b39a115c14b590b 100644 --- a/config/schema/recurring_events.schema.yml +++ b/config/schema/recurring_events.schema.yml @@ -1,6 +1,6 @@ -recurring_events.config: +recurring_events.eventseries.config: type: config_object - label: 'Recurring Events' + label: 'Recurring Events Series Config' mapping: interval: type: integer @@ -13,4 +13,7 @@ recurring_events.config: label: 'The latest an event can start' time_format: type: string - label: 'The formatting of times when creating events' \ No newline at end of file + label: 'The formatting of times when creating events' + days: + type: string + label: 'The days of the week available when creating events' \ No newline at end of file diff --git a/recurring_events.permissions.yml b/recurring_events.permissions.yml index 88013bd0a5f3ae8dfc27472fbdec1938a919c81d..28af1d21d40ed250a88b93af80c7cffde8a69f84 100644 --- a/recurring_events.permissions.yml +++ b/recurring_events.permissions.yml @@ -14,6 +14,10 @@ delete eventseries entity: clone eventseries entity: title: 'Clone eventseries entity' description: 'Clone existing event series entities' +administer eventseries entity: + title: 'Administer eventseries entity' + description: 'Make changes to the event series entity type.' + restrict access: true # Event Series Administration. administer eventseries: diff --git a/recurring_events.routing.yml b/recurring_events.routing.yml index 19406429089798a1c854783ee5e85017511386ff..0f1c544f16cb357bd0e403248863e353b49e3790 100644 --- a/recurring_events.routing.yml +++ b/recurring_events.routing.yml @@ -54,4 +54,13 @@ entity.eventseries.collection: path: '/events/series' requirements: # Checks for permission directly. - _permission: 'access eventseries overview' \ No newline at end of file + _permission: 'access eventseries overview' + +# Event Series settings admin page. +eventseries.settings: + path: '/admin/structure/events/series/settings' + defaults: + _form: '\Drupal\recurring_events\Form\EventSeriesSettingsForm' + _title: 'Event Series Settings' + requirements: + _permission: 'administer eventseries entity' \ No newline at end of file diff --git a/src/Form/EventSeriesSettingsForm.php b/src/Form/EventSeriesSettingsForm.php new file mode 100644 index 0000000000000000000000000000000000000000..fa86f134aa647a0037441ef95acdeca8a4561f46 --- /dev/null +++ b/src/Form/EventSeriesSettingsForm.php @@ -0,0 +1,126 @@ +<?php + +namespace Drupal\recurring_events\Form; + +use Drupal\Core\Form\ConfigFormBase;; +use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Url; +use Drupal\Core\Link; + +/** + * Class EventSeriesSettingsForm. + * + * @ingroup recurring_events + */ +class EventSeriesSettingsForm extends ConfigFormBase { + + /** + * Returns a unique string identifying the form. + * + * @return string + * The unique string identifying the form. + */ + public function getFormId() { + return 'eventseries_settings'; + } + + /** + * {@inheritdoc} + */ + protected function getEditableConfigNames() { + return ['recurring_events.eventseries.config']; + } + + /** + * Form submission handler. + * + * @param array $form + * An associative array containing the structure of the form. + * @param Drupal\Core\Form\FormStateInterface $form_state + * An associative array containing the current state of the form. + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->config('recurring_events.eventseries.config') + ->set('interval', $form_state->getValue('interval')) + ->set('min_time', $form_state->getValue('min_time')) + ->set('max_time', $form_state->getValue('max_time')) + ->set('time_format', $form_state->getValue('time_format')) + ->set('days', implode(',', array_filter($form_state->getValue('days')))) + ->save(); + + parent::submitForm($form, $form_state); + } + + /** + * Define the form used for Events settings. + * + * @param array $form + * An associative array containing the structure of the form. + * @param Drupal\Core\Form\FormStateInterface $form_state + * An associative array containing the current state of the form. + * + * @return array + * Form definition array. + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $config = $this->config('recurring_events.eventseries.config'); + + $form['interval'] = [ + '#type' => 'number', + '#title' => $this->t('Event Series Time Intervals'), + '#required' => TRUE, + '#description' => $this->t('Enter the interval, in minutes, to be used to separate event start times. Default is 15 minutes.'), + '#default_value' => $config->get('interval'), + ]; + + $form['min_time'] = [ + '#type' => 'textfield', + '#title' => $this->t('Event Series Minimum Time'), + '#required' => TRUE, + '#description' => $this->t('Enter the earliest an event can start, in h:ia format. For example 08:00am.'), + '#default_value' => $config->get('min_time'), + ]; + + $form['max_time'] = [ + '#type' => 'textfield', + '#title' => $this->t('Event Series Maximum Time'), + '#required' => TRUE, + '#description' => $this->t('Enter the latest an event can start, in h:ia format. For example 11:45pm.'), + '#default_value' => $config->get('max_time'), + ]; + + $url = Url::fromUri('https://secure.php.net/manual/en/function.date.php'); + $link = Link::fromTextAndUrl($this->t('PHP time format'), $url); + $form['time_format'] = [ + '#type' => 'textfield', + '#title' => $this->t('Event Series Time Format'), + '#required' => TRUE, + '#description' => $this->t('Enter the @link used when selecting times. Default is h:i A.', [ + '@link' => $link->toString(), + ]), + '#default_value' => $config->get('time_format'), + ]; + + $days = [ + 'monday' => t('Monday'), + 'tuesday' => t('Tuesday'), + 'wednesday' => t('Wednesday'), + 'thursday' => t('Thursday'), + 'friday' => t('Friday'), + 'saturday' => t('Saturday'), + 'sunday' => t('Sunday'), + ]; + + $form['days'] = [ + '#type' => 'checkboxes', + '#title' => $this->t('Event Series Days'), + '#required' => TRUE, + '#options' => $days, + '#description' => $this->t('Select the days of the week available when creating events.'), + '#default_value' => explode(',', $config->get('days')), + ]; + + return parent::buildForm($form, $form_state); + } + +} diff --git a/src/Plugin/Field/FieldWidget/WeeklyRecurringDateWidget.php b/src/Plugin/Field/FieldWidget/WeeklyRecurringDateWidget.php index 589f401b446c191f78b07db3b49b352ef5120794..3d97340c4cba4c65b07a59e86d6e09fdc66e7e1b 100644 --- a/src/Plugin/Field/FieldWidget/WeeklyRecurringDateWidget.php +++ b/src/Plugin/Field/FieldWidget/WeeklyRecurringDateWidget.php @@ -110,19 +110,15 @@ class WeeklyRecurringDateWidget extends DateRangeDefaultWidget { protected function getTimeOptions() { $times = []; - // TODO: Hook these up to configs. - /* $config = \Drupal::config('recurring_events.config'); + $config = \Drupal::config('recurring_events.eventseries.config'); // Take interval in minutes, and multiply it by 60 to convert to seconds. $interval = $config->get('interval') * 60; $min_time = $config->get('min_time'); $max_time = $config->get('max_time'); - $format = $config->get('time_format'); */ + $format = $config->get('time_format'); - // Take interval in minutes, and multiply it by 60 to convert to seconds. - $interval = 15 * 60; - $min_time = DrupalDateTime::createFromFormat('h:ia', '08:00am'); - $max_time = DrupalDateTime::createFromFormat('h:ia', '11:00pm'); - $format = 'h:i A'; + $min_time = DrupalDateTime::createFromFormat('h:ia', $min_time); + $max_time = DrupalDateTime::createFromFormat('h:ia', $max_time); // Convert the mininum time to a number of seconds after midnight. $lower_hour = $min_time->format('H') * 60 * 60; @@ -201,19 +197,11 @@ class WeeklyRecurringDateWidget extends DateRangeDefaultWidget { * An array of days suitable for a checkbox field. */ protected function getDayOptions() { - // TODO: Hook these up to configs. - /* $config = \Drupal::config('recurring_events.config'); - $days = $config->get('days'); */ - - $days = [ - 'monday' => t('Monday'), - 'tuesday' => t('Tuesday'), - 'wednesday' => t('Wednesday'), - 'thursday' => t('Thursday'), - 'friday' => t('Friday'), - 'saturday' => t('Saturday'), - 'sunday' => t('Sunday'), - ]; + $config = \Drupal::config('recurring_events.eventseries.config'); + $days = explode(',', $config->get('days')); + // All labels should have a capital first letter as they are proper nouns. + $day_labels = array_map('ucwords', $days); + $days = array_combine($days, $day_labels); \Drupal::moduleHandler()->alter('recurring_events_days', $days);