1

I've been trying to get a custom format placed upon a date range with particular rules (Explained in code comments) within Drupal 8 views.

What I currently have worked with regard to me being able to get the original two dates and to customize them but what I can't seem to do is pass the result ($display_date_range) forward to the fields view TWIG file (views-view-field--group-travel.html.twig)

What I have so far

/*****************************************************************************
 * VIEWS VIEW PRE-PROCESS
 * @param $variables
 *****************************************************************************/
function cazloyd_preprocess_views_view(&$variables) {
  $view = $variables['view'];
  $id = $view->storage->id();

  // Handle the Group Travel Listing view
  if($id == 'group_travel_listing'){
    foreach ($view->result as $key => $result){
      // Get start date from view
      $date_start = $result->_entity->get('field_start_date')->getValue();

      // Get end date from view
      $date_end = $result->_entity->get('field_end_date')->getValue();

      $months_are_same = (date('n', strtotime($date_start[0]['value']))==date('n', strtotime($date_end[0]['value'])))?TRUE:FALSE;

      if($months_are_same){
        // Month of start and end date are same, show only the day part of the start date
        // e.g. 11 - 23 July 2017
        $formatted_date_start = date('j', strtotime($date_start[0]['value']));
      }else{
        // Event crosses over into new month, therefore show day and month part of start date
        // e.g. 11 July - 5 August 2017
        $formatted_date_start = date('j F', strtotime($date_start[0]['value']));
      }

      $formatted_date_end = date('j F Y', strtotime($date_end[0]['value']));

      // Build final date range
      $display_date_range = $formatted_date_start . ' - ' . $formatted_date_end;

      echo '<pre>';
      print_r($display_date_range);
      echo '</pre>';
    }
  }
}
0

hook_preprocess_views_view() is used to alter the variables that are passed to a views-view[...].html.twig template file which is for the entire View, not just for the fields. It might make more sense for you to use hook_preprocess_views_view_fields() or hook_preprocess_views_view_field() since you are trying to alter the variables that are used in a views-view-field[...].html.twig template file (specific to the individual field, not the whole view).

See:

https://api.drupal.org/api/views/theme%21theme.inc/function/template_preprocess_views_view_fields/7.x-3.x

https://api.drupal.org/api/views/theme%21theme.inc/function/template_preprocess_views_view_field/7.x-3.x

  • Well, I'm not altering any existing fields, I am just reading two of them, but I do want to CREATE 1 new variable for each result and pass that through, but I get what you are saying about using the ...fields preprocessor instead – John Cogan Oct 20 '17 at 16:16
  • 1
    You could add a custom text field to the View and leave it empty, then alter that to include the balance value calculated from the other fields. – m.stenta Oct 20 '17 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.