Last active
March 10, 2022 22:15
-
-
Save rafaehlers/5086fe06adf6aa9c33f469c0f3287623 to your computer and use it in GitHub Desktop.
Modify event colors based on a field value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // DO NOT COPY THIS LINE | |
/** | |
* Modify event array that is output to FullCalendar | |
* In this sample code, we add a background color to a specific event | |
* | |
* @param array $events Array of events. | |
* @param object $form Calendar form. | |
* @param object $feed Calendar feed. | |
* @param array $field_map Array of feed fields mapped to calendar settings (e.g., start_time, end_time). | |
* @param array $entries Array of entries being displayed in the calendar (Requires 1.5.2) | |
* | |
* @return array $events Modified events | |
*/ | |
add_filter( 'gravityview/calendar/events', function ( $events, $form, $feed, $field_map = array(), $entries = array() ) { | |
if ( empty( $events ) || empty( $entries ) || $form['id'] !== 9 ) { // Replace 9 with your Form ID | |
return $events; | |
} | |
$event_ids = wp_list_pluck( $events, 'event_id' ); | |
foreach ( $entries as $entry ) { | |
$event_key = array_search( $entry['id'], $event_ids ); | |
if ( false === $event_key ) { | |
continue; | |
} | |
switch ($entry[5]) { // Replace 5 with the ID of your field | |
case 'event': | |
$events[ $event_key ]['backgroundColor'] = 'red'; | |
$events[ $event_key ]['borderColor'] = 'red'; | |
$events[ $event_key ]['textColor'] = 'black'; | |
break; | |
case 'appointment': | |
$events[ $event_key ]['backgroundColor'] = 'blue'; | |
$events[ $event_key ]['borderColor'] = 'blue'; | |
$events[ $event_key ]['textColor'] = 'white'; | |
break; | |
case 'reminder': | |
$events[ $event_key ]['backgroundColor'] = 'yellow'; | |
$events[ $event_key ]['borderColor'] = 'yellow'; | |
$events[ $event_key ]['textColor'] = 'black'; | |
break; | |
default: | |
$events[ $event_key ]['backgroundColor'] = 'yellow'; | |
$events[ $event_key ]['borderColor'] = 'blue'; | |
$events[ $event_key ]['textColor'] = 'yellow'; | |
break; | |
} | |
} | |
return $events; | |
}, 10, 5 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment