Commit c4a5c272 authored by Chris Shantz's avatar Chris Shantz
Browse files

Merge branch '1.0.x' into prod/1.0.x

parents 749caf45 f02546ef
name: 'UW Event'
description: 'Provides Event content type and related configuration.'
type: module
package: WCMS
core_version_requirement: '^8.9 || ^9'
......
......@@ -6,80 +6,147 @@
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\views\ViewExecutable;
/**
* Implements hook_preprocess_node().
*/
function uw_ct_event_preprocess_node(&$variables) {
// If on an event, add the ical link.
if ($variables['node']->getType() == 'uw_ct_event') {
// Get the nid.
$nid = $variables['node']->id();
// Get the URL to ical feed.
$ical_url = Url::fromRoute('view.uw_view_events.events_ical');
$ical_url = $ical_url->toString() . '/' . $nid;
// Add the ical url to the variable so can be used on templates.
$variables['ical'] = $ical_url;
}
}
/**
* Implements hook_views_pre_view().
*/
function uw_ct_event_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
// Get the id of the view.
$id = $view->id();
// If we are on an event listing page view,
// set the default date filter today if no
// filters are already set.
if (
$view->id() == 'uw_view_events' &&
$id == 'uw_view_events' &&
$display_id == 'event_page'
) {
// Get the exposed filters from the view.
$exposed = $view->getExposedInput();
// Get date filter's operator.
if (isset($exposed['date_type'])) {
$date_operator = $exposed['date_type'];
$date_check = $exposed['date']['value'];
if ($date_operator == 'between') {
// If we are using "between", we want to use the max date as our check.
// Note that if "between" doesn't have two values, it doesn't seem to
// apply at all, so we don't do a comparison if there isn't a start
// value.
if ($exposed['date']['min']) {
$base_date = date('m/d/Y');
$date_check = $exposed['date']['max'];
}
else {
$date_operator = NULL;
}
// Set the correct exposed inputs, especially the correct
// date.
_uw_ct_event_exposed_inputs($view);
// Get the URL to RSS feed.
$url = Url::fromRoute('view.uw_view_events.uw_public_event_feed');
$url->setOptions(['query' => $view->getExposedInput()]);
$rss = $url->toString();
// Set the variable to the RSS.
$view->rss = $rss;
// Get the URL to ical feed.
$ical_url = Url::fromRoute('view.uw_view_events.events_ical');
$ical_url->setOptions(['query' => $view->getExposedInput()]);
// Set the variable to the ical.
$view->ical = $ical_url->toString();
}
}
/**
* Function to set the exposed inputs on the events view.
*
* @param \Drupal\views\ViewExecutable $view
* The view object.
*/
function _uw_ct_event_exposed_inputs(ViewExecutable $view) {
// Get the exposed filters from the view.
$exposed = $view->getExposedInput();
// Get date filter's operator.
if (isset($exposed['date_type'])) {
$date_operator = $exposed['date_type'];
$date_check = $exposed['date']['value'];
if ($date_operator == 'between') {
// If we are using "between", we want to use the max date as our check.
// Note that if "between" doesn't have two values, it doesn't seem to
// apply at all, so we don't do a comparison if there isn't a start
// value.
if ($exposed['date']['min']) {
$base_date = date('m/d/Y');
$date_check = $exposed['date']['max'];
}
}
// Operators which automatically reverse the sorting.
$automatic_operators = [
'<',
'<=',
];
// Operators that require a date check to automatically reverse the sorting.
$additional_operators = [
'between',
];
// All operators.
$check_operators = array_merge($automatic_operators, $additional_operators);
// If there is a date filter set, see if we need to act on it.
if (isset($date_check) && in_array($date_operator, $check_operators)) {
// Reverse the sort order if filtering by one of the automatic operators,
// or if filtering by one of the additional operators, where the date to
// check is entirely in the past.
if (in_array($date_operator, $automatic_operators) || (in_array($date_operator, $additional_operators) && $date_check < $base_date)) {
// Get the display and the sort options from the view.
$display = $view->getDisplay();
$sorts = $display->getOption('sorts');
// Reverse the sort order.
$sorts['field_uw_event_date_value']['order'] = 'DESC';
$display->setOption('sorts', $sorts);
else {
$date_operator = NULL;
}
}
// If there are no exposed filters set, then set the default date to today.
elseif ($exposed == NULL) {
// Get today's date in the correct format.
$exposed['date']['value'] = date('m/d/Y', time());
}
// Set the default to today's date.
$view->setExposedInput($exposed);
}
// Operators which automatically reverse the sorting.
$automatic_operators = [
'<',
'<=',
];
// Operators that require a date check to automatically reverse the sorting.
$additional_operators = [
'between',
];
// All operators.
$check_operators = array_merge($automatic_operators, $additional_operators);
// If there is a date filter set, see if we need to act on it.
if (isset($date_check) && in_array($date_operator, $check_operators)) {
// Reverse the sort order if filtering by one of the automatic operators,
// or if filtering by one of the additional operators, where the date to
// check is entirely in the past.
if (
in_array($date_operator, $automatic_operators) ||
(
in_array($date_operator, $additional_operators) &&
$date_check < $base_date
)
) {
// Get the display and the sort options from the view.
$display = $view->getDisplay();
$sorts = $display->getOption('sorts');
// Reverse the sort order.
$sorts['field_uw_event_date_value']['order'] = 'DESC';
$display->setOption('sorts', $sorts);
}
}
// If there are no exposed filters set, then set the default date to today.
elseif ($exposed == NULL) {
// Get today's date in the correct format.
$exposed['date']['value'] = date('m/d/Y', time());
// Set the default to today's date.
$view->setExposedInput($exposed);
}
}
/**
......@@ -88,8 +155,11 @@ function uw_ct_event_views_pre_view(ViewExecutable $view, $display_id, array &$a
function uw_ct_event_views_pre_render(ViewExecutable $view) {
// Remove exposed filters on block listing view.
if ($view->id() == 'uw_view_events' &&
$view->getTitle() == 'Event listing') {
if (
$view->id() == 'uw_view_events' &&
$view->getTitle() == 'Event listing'
) {
$view->exposed_widgets = NULL;
}
}
......@@ -97,7 +167,12 @@ function uw_ct_event_views_pre_render(ViewExecutable $view) {
/**
* Implements hook_geofield_map_latlon_element_alter().
*/
function uw_ct_event_geofield_map_latlon_element_alter(array &$map_settings, array &$complete_form, array &$form_state_values) {
function uw_ct_event_geofield_map_latlon_element_alter(
array &$map_settings,
array &$complete_form,
array &$form_state_values
) {
// Library for editing uw_ct_event.
if ($map_settings['id'] === 'edit-field-uw-event-location-coord-0-value') {
$complete_form['#attached']['library'][] = 'uw_ct_event/uw_ct_event_edit';
......@@ -167,8 +242,10 @@ function uw_ct_event_preprocess_views_view_unformatted(&$variables) {
* The location data.
*/
function _uw_ct_event_load_locations(): array {
// Cached results of this function.
static $locations;
if ($locations) {
return $locations;
}
......@@ -191,25 +268,34 @@ function _uw_ct_event_load_locations(): array {
];
$locations = [];
foreach ($locations_file as $file_location) {
// Parse name into code and name.
list($location_code, $location_name) = explode('-', $file_location['name'], 2);
$location_code = trim($location_code);
// Create array for this location.
$location = [
'location_code' => $location_code,
'location_name' => trim($location_name),
];
$location['location_name_and_code'] = $location['location_code'] . ' - ' . $location['location_name'];
// Copy values from file, renaming some keys.
foreach ($fields as $field_in => $field_out) {
$location[$field_out ?: $field_in] = $file_location[$field_in];
}
// Use capital letters for country codes.
$location['country_code'] = strtoupper($location['country_code']);
// Append to main array.
$locations[$location_code] = $location;
}
return $locations;
}
......@@ -222,21 +308,29 @@ function _uw_ct_event_load_locations(): array {
function _uw_ct_event_get_location_options(): array {
// Cached results of this function.
static $options;
if ($options) {
return $options;
}
$options = [];
foreach (_uw_ct_event_load_locations() as $location_code => $location) {
$options[$location_code] = $location_code . ' - ' . $location['location_name'];
}
return $options;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function uw_ct_event_form_node_uw_ct_event_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
function uw_ct_event_form_node_uw_ct_event_form_alter(
array &$form,
FormStateInterface $form_state,
string $form_id
): void {
// Add locations pre-fill menu.
$form['group_event_location']['location_presets_select'] = [
'#title' => t('Location lookup'),
......@@ -245,7 +339,9 @@ function uw_ct_event_form_node_uw_ct_event_form_alter(array &$form, FormStateInt
'#empty_option' => '- Choose location -',
'#options' => _uw_ct_event_get_location_options(),
];
$form['#attached']['library'][] = 'uw_ct_event/location_autofill';
$form['#attached']['drupalSettings']['uwCtEvent'] = _uw_ct_event_load_locations();
}
......@@ -253,6 +349,7 @@ function uw_ct_event_form_node_uw_ct_event_form_alter(array &$form, FormStateInt
* Implements hook_form_alter().
*/
function uw_ct_event_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
switch ($form_id) {
case 'node_uw_ct_event_edit_form':
case 'node_uw_ct_event_quick_node_clone_form':
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment