问题
I'm using Drupal 7, with the Views, Feeds and Views PHP modules (among others, but I think those are the only relevant ones).
My goal is a single view that combines (a) multiple RSS feeds brought in through the Feeds module, (b) two or three content types of my own, for contant created manually on my site. The result will be a block and a page that has a stream of all my latest work, wherever it is -- my own site or another site (as long as that other site has an RSS feed, of course).
The ideal result would show a single list, sorted by date, mingling the items without regard for content type.
My problem: The Feeds module and my custom content types have different date fields. My content types have a field called Original Date, which I set when I create content (eg, adding an article I wrote three years ago -- I clearly want it to be sorted by when it was published, not when I added it to the site). However, each feed_item has a date field, Published Date, that corresponds to the date in the RSS feed and is received with the rest of the item by RSS.
I have tried to use the Views PHP functionality to use if/then statements, in a variety of ways. For example, I check what the content_type is, and then set a variable to either the Original Date or by the Published Date, depending on the content type. Alternatively, I have tested whether the Original Date field is set; if so, I return that field, otherwise I return the Published Date field. They all fail in various ways. Most throw PHP errors that are visible even to anonymous users (obviously a problem).
I have tried these various approaches directly in the Sort Criterial section of Views; I've also tried to come up with the date in the Fields section, and then have a Sort criterion call that result. I've converted everything to a Unix timestamp, to make sure I have something simple to sort by; I've tried leaving dates as dates. No luck any which way.
Here's the really weird part: I have successfully used these approaches to display the correct date, so that (for example) the block shows • TITLE | Original Date for custom content types, and • TITLE | Published Date for feed items. But when I try the same approach(es) to sort by date, it fails.
Am I overlooking something simpler? Do I have to use PHP Views? Is there some other way to sort a View of multiple content types by a combination of two different (though similar) fields?
Thanks!
tf
回答1:
Have you tried using the relationship feature of views? You could use that to get access to all of the date fields of all of the content types, then add some conditional code to the field item and you should be golden. (That is, if I'm understanding the question correctly...)
回答2:
I found many possible solutions and summarized them here: https://drupal.org/node/2133879
However, this is a simple solution that I'd recommend:
In a custom module use the hook_views_query_alter() function to alter the sort criteria to use CASE ... WHEN to add a conditional in the orderby condition. A good example is shown here:
<?php
/**
* Implements hook_views_query_alter
* @param type $view
* @param type $query
*/
function MODULE_views_query_alter(&$view, &$query) {
if ($view->name == 'views_name' && $view->current_display == 'display_name') {
$query->orderby = array(
array(
'field' => 'CASE WHEN field_data_field_date_publication.field_date_publication_value
THEN field_data_field_date_publication.field_date_publication_value
ELSE node.created END',
'direction' => 'DESC',
)
);
}
}
?>
来源:https://stackoverflow.com/questions/11769902/drupal-7-sorting-multiple-content-types-in-views-with-different-date-fields