0

I have two content types Tweet and Instagram. Both content types includes a Date field. The information in the Date fields is imported by a json parser module.

I am displaying both content types in a view titled "homeview". All works well as expected. Both date fields use the same date format d/m/Y.

I am trying to sort both types of content chronologically in a descending order based on the date field. In other words, I would like to see this:

  • Twitter post - date: 26/06/2014

  • Instagram post - date: 25/06/2014

  • Twitter post - date: 24/06/2014

  • Instagram post - date: 23/06/2014

But was I see is this:

  • Twitter post - date: 26/06/2014
  • Twitter post - date: 24/06/2014
  • Instagram post - date: 25/06/2014
  • Instagram post - date: 23/06/2014

I found a tip how to address the issue and I created my custom module:

        <?php

    /**
     * @file
     *
     */


    function mysortmodule_views_query_alter(&$view, &$query) {
      // this is the name of our View and Display
      if ($view->name == 'homeview' && $view->current_display == 'page') {
        // override the ordering
        // this is because the two different date fields are one or the other
        // but not both fields, so we coalesce.
        $query->orderby = array(
          array(
            'field' => 'COALESCE( field_twitter_created_value, field_instagram_date_value )',
            'direction' => 'DESC',
          )
        );
      }
    }

Unfortunately, it doesn't to be working.

Could anyone please give me tips how to sort view using two different date fields? Here's my "homeview" export code:

        $view = new view();
    $view->name = 'homeview';
    $view->description = '';
    $view->tag = 'default';
    $view->base_table = 'node';
    $view->human_name = 'homeview';
    $view->core = 7;
    $view->api_version = '3.0';
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

    /* Display: Master */
    $handler = $view->new_display('default', 'Master', 'default');
    $handler->display->display_options['title'] = 'HomeView';
    $handler->display->display_options['use_more_always'] = FALSE;
    $handler->display->display_options['access']['type'] = 'perm';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'infinite_scroll';
    $handler->display->display_options['pager']['options']['items_per_page'] = '5';
    $handler->display->display_options['pager']['options']['offset'] = '0';
    $handler->display->display_options['pager']['options']['id'] = '0';
    $handler->display->display_options['pager']['options']['quantity'] = '0';
    $handler->display->display_options['style_plugin'] = 'masonry_views_grid';
    $handler->display->display_options['style_options']['masonry_animated'] = TRUE;
    $handler->display->display_options['style_options']['masonry_resizable'] = TRUE;
    $handler->display->display_options['style_options']['masonry_center'] = FALSE;
    $handler->display->display_options['style_options']['masonry_rtl'] = FALSE;
    $handler->display->display_options['row_plugin'] = 'fields';
    /* Field: Content: Title */
    $handler->display->display_options['fields']['title']['id'] = 'title';
    $handler->display->display_options['fields']['title']['table'] = 'node';
    $handler->display->display_options['fields']['title']['field'] = 'title';
    $handler->display->display_options['fields']['title']['exclude'] = TRUE;
    $handler->display->display_options['fields']['title']['link_to_node'] = FALSE;
    /* Field: Content: Type */
    $handler->display->display_options['fields']['type']['id'] = 'type';
    $handler->display->display_options['fields']['type']['table'] = 'node';
    $handler->display->display_options['fields']['type']['field'] = 'type';
    $handler->display->display_options['fields']['type']['label'] = '';
    $handler->display->display_options['fields']['type']['element_label_colon'] = FALSE;
    /* Field: Content: Post date */
    $handler->display->display_options['fields']['created']['id'] = 'created';
    $handler->display->display_options['fields']['created']['table'] = 'node';
    $handler->display->display_options['fields']['created']['field'] = 'created';
    $handler->display->display_options['fields']['created']['label'] = 'Drupal';
    $handler->display->display_options['fields']['created']['exclude'] = TRUE;
    $handler->display->display_options['fields']['created']['date_format'] = 'universal_date';
    $handler->display->display_options['fields']['created']['second_date_format'] = 'long';
    /* Field: Content: Created */
    $handler->display->display_options['fields']['field_twitter_created']['id'] = 'field_twitter_created';
    $handler->display->display_options['fields']['field_twitter_created']['table'] = 'field_data_field_twitter_created';
    $handler->display->display_options['fields']['field_twitter_created']['field'] = 'field_twitter_created';
    $handler->display->display_options['fields']['field_twitter_created']['label'] = '';
    $handler->display->display_options['fields']['field_twitter_created']['element_label_colon'] = FALSE;
    $handler->display->display_options['fields']['field_twitter_created']['hide_empty'] = TRUE;
    $handler->display->display_options['fields']['field_twitter_created']['settings'] = array(
      'format_type' => 'universal_date',
      'fromto' => 'both',
      'multiple_number' => '',
      'multiple_from' => '',
      'multiple_to' => '',
    );
    /* Field: Content: Date */
    $handler->display->display_options['fields']['field_instagram_date']['id'] = 'field_instagram_date';
    $handler->display->display_options['fields']['field_instagram_date']['table'] = 'field_data_field_instagram_date';
    $handler->display->display_options['fields']['field_instagram_date']['field'] = 'field_instagram_date';
    $handler->display->display_options['fields']['field_instagram_date']['label'] = '';
    $handler->display->display_options['fields']['field_instagram_date']['element_label_colon'] = FALSE;
    $handler->display->display_options['fields']['field_instagram_date']['hide_empty'] = TRUE;
    $handler->display->display_options['fields']['field_instagram_date']['settings'] = array(
      'format_type' => 'universal_date',
      'fromto' => 'both',
      'multiple_number' => '',
      'multiple_from' => '',
      'multiple_to' => '',
    );
    /* Field: Content: Link */
    $handler->display->display_options['fields']['view_node']['id'] = 'view_node';
    $handler->display->display_options['fields']['view_node']['table'] = 'views_entity_node';
    $handler->display->display_options['fields']['view_node']['field'] = 'view_node';
    $handler->display->display_options['fields']['view_node']['label'] = '';
    $handler->display->display_options['fields']['view_node']['exclude'] = TRUE;
    $handler->display->display_options['fields']['view_node']['element_label_colon'] = FALSE;
    /* Field: Content: Link */
    $handler->display->display_options['fields']['field_instagram_link']['id'] = 'field_instagram_link';
    $handler->display->display_options['fields']['field_instagram_link']['table'] = 'field_data_field_instagram_link';
    $handler->display->display_options['fields']['field_instagram_link']['field'] = 'field_instagram_link';
    $handler->display->display_options['fields']['field_instagram_link']['label'] = '';
    $handler->display->display_options['fields']['field_instagram_link']['exclude'] = TRUE;
    $handler->display->display_options['fields']['field_instagram_link']['element_label_colon'] = FALSE;
    $handler->display->display_options['fields']['field_instagram_link']['click_sort_column'] = 'url';
    /* Field: Content: Image */
    $handler->display->display_options['fields']['field_instagram_image']['id'] = 'field_instagram_image';
    $handler->display->display_options['fields']['field_instagram_image']['table'] = 'field_data_field_instagram_image';
    $handler->display->display_options['fields']['field_instagram_image']['field'] = 'field_instagram_image';
    $handler->display->display_options['fields']['field_instagram_image']['label'] = '';
    $handler->display->display_options['fields']['field_instagram_image']['alter']['alter_text'] = TRUE;
    $handler->display->display_options['fields']['field_instagram_image']['alter']['make_link'] = TRUE;
    $handler->display->display_options['fields']['field_instagram_image']['alter']['path'] = '[field_instagram_link]';
    $handler->display->display_options['fields']['field_instagram_image']['alter']['external'] = TRUE;
    $handler->display->display_options['fields']['field_instagram_image']['alter']['target'] = '_blank';
    $handler->display->display_options['fields']['field_instagram_image']['element_label_colon'] = FALSE;
    $handler->display->display_options['fields']['field_instagram_image']['click_sort_column'] = 'fid';
    $handler->display->display_options['fields']['field_instagram_image']['settings'] = array(
      'image_style' => 'thumbnail',
      'image_link' => '',
    );
    /* Field: Content: Twitter Text */
    $handler->display->display_options['fields']['field_twitter_text']['id'] = 'field_twitter_text';
    $handler->display->display_options['fields']['field_twitter_text']['table'] = 'field_data_field_twitter_text';
    $handler->display->display_options['fields']['field_twitter_text']['field'] = 'field_twitter_text';
    $handler->display->display_options['fields']['field_twitter_text']['label'] = '';
    $handler->display->display_options['fields']['field_twitter_text']['alter']['alter_text'] = TRUE;
    $handler->display->display_options['fields']['field_twitter_text']['alter']['text'] = '<a href="https://twitter.com/MusicMusiqueNB/status/[title]" target="_blank">[field_twitter_text]</a>';
    $handler->display->display_options['fields']['field_twitter_text']['element_label_colon'] = FALSE;
    $handler->display->display_options['fields']['field_twitter_text']['hide_empty'] = TRUE;
    /* Sort criterion: Content: Created (field_twitter_created) */
    $handler->display->display_options['sorts']['field_twitter_created_value']['id'] = 'field_twitter_created_value';
    $handler->display->display_options['sorts']['field_twitter_created_value']['table'] = 'field_data_field_twitter_created';
    $handler->display->display_options['sorts']['field_twitter_created_value']['field'] = 'field_twitter_created_value';
    $handler->display->display_options['sorts']['field_twitter_created_value']['order'] = 'DESC';
    /* Sort criterion: Content: Date (field_instagram_date) */
    $handler->display->display_options['sorts']['field_instagram_date_value']['id'] = 'field_instagram_date_value';
    $handler->display->display_options['sorts']['field_instagram_date_value']['table'] = 'field_data_field_instagram_date';
    $handler->display->display_options['sorts']['field_instagram_date_value']['field'] = 'field_instagram_date_value';
    $handler->display->display_options['sorts']['field_instagram_date_value']['order'] = 'DESC';
    /* Filter criterion: Content: Published */
    $handler->display->display_options['filters']['status']['id'] = 'status';
    $handler->display->display_options['filters']['status']['table'] = 'node';
    $handler->display->display_options['filters']['status']['field'] = 'status';
    $handler->display->display_options['filters']['status']['value'] = '1';
    $handler->display->display_options['filters']['status']['group'] = 1;
    $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
    /* Filter criterion: Content: Type */
    $handler->display->display_options['filters']['type']['id'] = 'type';
    $handler->display->display_options['filters']['type']['table'] = 'node';
    $handler->display->display_options['filters']['type']['field'] = 'type';
    $handler->display->display_options['filters']['type']['value'] = array(
      'instagram' => 'instagram',
      'tweet' => 'tweet',
    );

    /* Display: Page */
    $handler = $view->new_display('page', 'Page', 'page');
    $handler->display->display_options['path'] = 'homeview';

    /* Display: Block */
    $handler = $view->new_display('block', 'Block', 'block');
    $handler->display->display_options['defaults']['pager'] = FALSE;
    $handler->display->display_options['pager']['type'] = 'some';
    $handler->display->display_options['pager']['options']['items_per_page'] = '5';
0

If they share the same date field, you should just sort by that field descending and it should work perfectly. If not, I think you should be able to add both to the sort one after the other and it should work. But try to have them use the same field, it will make life simpler.

1
  • Thanks. A great suggestion. It worked like a charm! I delete the "Date" field from "Tweet" content type and re-used (added) the "field_instagram_date" instead. I sorted the view (both content types) by "field_instagram_date".
    – Neznalek
    Jun 27 '14 at 12:39
0

You can use Views PHP module for this purpose. The module allows you to code your own condition or expression for a field in a view.

Here are the steps 1)Enable the module 2)Add the date fields to the fields section and exclude them from display incase they should not be displayed 3)add the field "Global: PHP" to sort condition and use the following code in the PHP field.

$date1 = $data->field_field_date_1;
$date2 = $data->field_field_date_2;
if(!empty($date1)){
   return $date1;
}else{
  return $date2;
}

4) Replace "field_field_date_1" and "field_field_date_2" with the appropriate field names.

Please note that using the PHP code in sort and filter conditions might have a performance impact. So this option needs to be used carefully.

1
  • Thanks for the useful info. I wasn't aware of the "Views PHP" module. Ryan88's answer resolved my issue without adding an extra module therefore I am marking his as the accepted answer.
    – Neznalek
    Jun 27 '14 at 12:42

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.