Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
WCMS
uw_ct_event
Commits
4b7d6c45
Commit
4b7d6c45
authored
Sep 24, 2021
by
Chris Shantz
Browse files
Merge branch '1.0.x' into prod/1.0.x
parents
b80ba262
26687d5d
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
config/install/views.view.uw_view_events.yml
View file @
4b7d6c45
...
...
@@ -298,7 +298,19 @@ display:
position
:
2
display_options
:
display_extenders
:
metatag_display_extender
:
{
}
metatag_display_extender
:
metatags
:
title
:
'
[current-page:title]
|
[site:name]'
description
:
'
Listing
the
events
on
the
[site:name]
site.'
canonical_url
:
'
[current-page:url:absolute]'
content_language
:
en-CA
image_src
:
'
https://uwaterloo.ca/university-of-waterloo-logo-152.png'
og_url
:
'
[current-page:url:absolute]'
og_title
:
'
[current-page:metatag:title]'
og_description
:
'
[current-page:metatag:description]'
og_image
:
'
[current-page:metatag:image_src]'
og_locale
:
en_CA
twitter_cards_type
:
summary_large_image
display_description
:
'
'
title
:
Events
defaults
:
...
...
@@ -749,7 +761,7 @@ display:
timezone_override
:
'
'
format
:
default
force_chronological
:
false
add_classes
:
0
add_classes
:
false
group_column
:
value
group_columns
:
{
}
group_rows
:
false
...
...
js/location_autofill.js
0 → 100644
View file @
4b7d6c45
/**
* @file
* Javascript for event location presets menu.
*/
(
function
(
$
,
Drupal
,
drupalSettings
)
{
'
use strict
'
;
Drupal
.
behaviors
.
uw_ct_event_location_autofill
=
{
attach
:
function
(
context
,
drupalSettings
)
{
var
location_select
=
$
(
'
select#edit-location-presets-select
'
);
location_select
.
change
(
function
()
{
// Get chosen location.
var
location
=
drupalSettings
.
uwCtEvent
[
location_select
.
val
()];
// Do nothing when re-selecting the initial value.
if
(
!
location
)
{
return
;
}
// Set coordinates and re-center map.
$
(
'
input#lat-edit-field-uw-event-location-coord-0-value
'
).
val
(
location
.
latitude
);
$
(
'
input#lon-edit-field-uw-event-location-coord-0-value
'
).
val
(
location
.
longitude
).
change
();
// Set the country if it has changed.
var
select_country
=
$
(
'
#edit-field-uw-event-location-address-0 select.country
'
);
if
(
select_country
.
val
()
!==
location
.
country_code
)
{
select_country
.
val
(
location
.
country_code
).
change
();
}
// Update the address fields.
var
address_fields
=
{
location_name_and_code
:
'
input.organization
'
,
address
:
'
input.address-line1
'
,
city
:
'
input.locality
'
,
postal_code
:
'
input.postal-code
'
,
province_code
:
'
select.administrative-area
'
,
};
// Give time for the progress throbber to appear. Fields are updated by
// Ajax following any change in the country.
setTimeout
(
function
()
{
var
counter
=
0
;
// Repeat until the progress throbber disappears and the address field
// appears. Update the address and clearInterval() to stop repeating.
var
checkExist
=
setInterval
(
function
()
{
// Stop repeating after 10 seconds if Ajax gets stuck.
counter
++
;
if
(
counter
>
50
)
{
clearInterval
(
checkExist
);
}
else
if
(
!
$
(
'
.ajax-progress-throbber
'
).
length
&&
$
(
'
#edit-field-uw-event-location-address-0 input.organization
'
).
length
)
{
clearInterval
(
checkExist
);
for
(
var
key
in
address_fields
)
{
var
element
=
$
(
'
#edit-field-uw-event-location-address-0
'
+
address_fields
[
key
]);
if
(
element
.
val
()
!==
location
[
key
])
{
element
.
val
(
location
[
key
]).
change
();
}
}
}
},
200
);
},
500
);
});
}
}
})(
jQuery
,
Drupal
,
drupalSettings
);
uw_ct_event.libraries.yml
View file @
4b7d6c45
uw_ct_event_edit
:
js
:
js/uw_ct_event_edit.js
:
{}
location_autofill
:
js
:
js/location_autofill.js
:
{}
uw_ct_event.module
View file @
4b7d6c45
...
...
@@ -5,6 +5,7 @@
* Provides configuration and settings for events.
*/
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\node\Entity\Node
;
use
Drupal\views\ViewExecutable
;
...
...
@@ -24,17 +25,62 @@ function uw_ct_event_views_pre_view(ViewExecutable $view, $display_id, array &$a
// Get the exposed filters from the view.
$exposed
=
$view
->
getExposedInput
();
// If there are no exposed filters set, then set
// set the default date to today.
if
(
$exposed
==
NULL
)
{
// Get todays date in the correct format.
// 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
;
}
}
}
// 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 todays date.
// Set the default
to
today
'
s date.
$view
->
setExposedInput
(
$exposed
);
}
}
}
/**
...
...
@@ -91,3 +137,104 @@ function uw_ct_event_preprocess_views_view_unformatted(&$variables) {
}
}
}
/**
* Load data about UW locations from a JSON file.
*
* @return array[]
* The location data.
*/
function
_uw_ct_event_load_locations
():
array
{
// Cached results of this function.
static
$locations
;
if
(
$locations
)
{
return
$locations
;
}
// Load data file.
$locations_file
=
file_get_contents
(
__DIR__
.
'/uw_locations.json'
);
$locations_file
=
json_decode
(
$locations_file
,
TRUE
);
// Field mapping for renaming some keys.
$fields
=
[
'street'
=>
'address'
,
'city'
=>
NULL
,
'province'
=>
'province_code'
,
'province_name'
=>
NULL
,
'country'
=>
'country_code'
,
'country_name'
=>
NULL
,
'postal_code'
=>
NULL
,
'latitude'
=>
NULL
,
'longitude'
=>
NULL
,
];
$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
;
}
/**
* Return locations as a $code => $title array.
*
* @return string[]
* An array of locations with their codes as the keys and names as the values.
*/
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
{
// Add locations pre-fill menu.
$form
[
'group_event_location'
][
'location_presets_select'
]
=
[
'#title'
=>
t
(
'Location lookup'
),
'#description'
=>
t
(
'Choose a location or enter location information below.'
),
'#type'
=>
'select'
,
'#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
();
}
/**
* 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'
:
uw_ct_event_form_node_uw_ct_event_form_alter
(
$form
,
$form_state
,
$form_id
);
break
;
}
}
uw_locations.json
0 → 100644
View file @
4b7d6c45
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment