Injecting Custom PHP Code into your Templates

The following is for advanced users or developers. You can break your site attempting to make changes here. Proceed with caution.

It is possible to inject the results of custom PHP code into your Twig templates. Here is how:

  1. In your custom calendar theme, edit the functions.php file.
  2. Add the following lines of code, replacing sometemplate.twig with the name of the template file you wish to inject the results of PHP into, and mytheme_sometemplate_args with a suitable unique function name. (For example, if your theme were located in a folder called bluedot, and you were modifying the posterboard.twig template, then a reasonably named function would be bluedot_posterboard_args – but the function name is entirely your choice.)
    add_filter( 'ai1ec_theme_args_sometemplate.twig', 'mytheme_sometemplate_args', 10, 2 );
    function mytheme_sometemplate_args( $args, $is_admin ) {
      // ...
      //
      // ... Modify the values of the $args array accordingly...
      //
      // ...
    
      return $args;
    }
    
  3. Replace the commented lines above with the PHP code required to modify the template’s $args array and provide the custom values that you need in your template.
  4. If you have added new values to the $args array, or new custom values to a sub-array or object, override that template file by copying it to your custom theme, then revise that copy to display those new values using standard Twig evaluators. If you are simply modifying existing values, however, then you probably don’t need to override the template and can just continue using the default one from Vortex.

Example 1: Modifying Agenda view

The following example displays the value of a custom post field with each event shown in Agenda view.

Please note: If you have the advanced setting Use frontend rendering enabled, event data as passed to calendar views (such as Agenda view here) will not be in the form of a list of Ai1ec_Event objects, but rather a list of simple PHP arrays containing event properties. That means that instead of using the $event->get() and $event->set() methods to retrieve and save properties, respectively, you would manipulate $event as a PHP array (for example, $event['my_property']).

functions.php:

add_filter( 'ai1ec_theme_args_agenda.twig', 'mytheme_agenda_args', 10, 2 );
/**
 * Adds value of custom field to event arrays in Agenda view.
 *
 * @param  array $args     Arguments passed to the agenda.twig template
 * @param  bool  $is_admin Whether this is an admin-area template
 * @return array           Modified $args array
 */
function mytheme_agenda_args( $args, $is_admin ) {
  // Iterate through every date of Agenda view.
  foreach ( $args['dates'] as $date => &$date_info ) {
    // Iterate through the "all-day" and "not all-day" categories of that day.
    foreach ( $date_info['events'] as &$category ) {
      // Iterate through every event of the current category.
      foreach ( $category as &$event ) {
        $post_id      = $event->get( 'post_id' );
        $custom_field = get_post_meta( $post_id, 'custom_field', true );
        // Add value of custom field to event entity to be used in template.
        $event->set( 'custom_field', $custom_field );
      }
    }
  }

  return $args;
}

twig/agenda.twig:

Everything remains identical to the default Vortex version, except for a small change to the <div class="ai1ec-event-description"> ... </div> section:

...
  <div class="ai1ec-event-description">
    {% if event.content_img_url is empty %}
      {{ event.avatar | raw }}
    {% endif %}
    <div><strong>Custom field:</strong> {{ event.get( 'custom_field' ) }}</div>
    {{ event.filtered_content | raw }}
 </div>
...

Example 2: Modifying single event view

Similar to the above example, this one displays the value of a custom post field with an event, but this time for single event view.

functions.php:

add_filter( 'ai1ec_theme_args_event-single.twig', 'mytheme_event_single_args', 10, 2 );
/**
 * Adds value of custom field to event arrays in Agenda view.
 *
 * @param  array $args     Arguments passed to the event-single.twig template
 * @param  bool  $is_admin Whether this is an admin-area template
 * @return array           Modified $args array
 */
function mytheme_event_single_args( $args, $is_admin ) {
  // In single event view, event properties are retrieved using the get() and
  // get_runtime() methods of the event object. Inspect the event-single.twig
  // template to see which method makes sense for you. For post ID, we need to
  // call the get() method.
  $post_id = $args['event']->get( 'post_id' );
  // Add value of custom field to args array to be used in template.
  $args['custom_field'] = get_post_meta( $post_id, 'custom_field', true );

  return $args;
}

twig/event-single.twig:

Now you can insert your Twig markup to display the value of the custom field wherever you wish:

...
  <div><strong>Custom field:</strong> {{ custom_field }}</div>
...