10

I have a view of a content type that contains two different date fields (one with date repeats and one multivalued single dates). Only one date field is filled out at any given time. In the view's display, I have combined the two fields by hiding the first date field and rewriting the second and including the replacement token of the first and second. Is there any way to now use the rewritten field to sort the view in ascending order based on this combined date field? It appears it sorts it based on the original value of the date field.

I have done my research and have only found old, somewhat-related, threads with dead-ends.

views sorting by rewritten field?

Table does not sort on rewritten field, but on original field

Views: custom field for sort criteria

Sort by global:custom text. Is it possible?

I was hoping there is a new or sneaky way of making this happen.

  • Is there some reason you can't use merlinofchaos's answer? drupal.org/node/1260160#comment-4907526 – Kari Kääriäinen Sep 9 '13 at 22:09
  • He doesn't actually provide an answer on how to use computed field. He just recommends using it. I don't mind trying it but I do not know what to do with the module. I also hoped that there might be something new to try since that was 2 years ago and for Drupal 6. – Nigel Waters Sep 9 '13 at 22:19
  • Yeah, that was a short answer. The principle is still the same though. See my answer. – Kari Kääriäinen Sep 9 '13 at 22:56
  • What is it you want to do with those date fields? Pick the next date coming up? – Kari Kääriäinen Sep 17 '13 at 8:35
  • I want to sort the event content type by the two date fields in ascending order. – Nigel Waters Sep 17 '13 at 17:04
4

If you don't want to go for the module route, this is one alternative:

1) install Computed Field module
2) add a computed field to your content type
3) in the field settings, in the Computed code (PHP) textarea, you can access the field data.

Get your date fields in your current langugage to a variable.

$repeating_dates = field_get_items($entity_type, $entity, 'field_repeating_date_example');
$multidates = field_get_items($entity_type, $entity, 'field_multidate_example');

Your date values can now be found in

$repeating_dates[0]['value'],
$repeating_dates[1]['value'],
$repeating_dates[2]['value'], etc.

$multidates[0]['value'],
$multidates[1]['value'],
$multidates[2]['value'], etc.

Do whatever logic you need with your date fields and assign the result to

$entity_field[0]['value']

Now you have a sortable field you can use in views, but first you have to update all the nodes as the computed field will only be computed and saved to the database at node save. There are many ways to do this, one is using Views Bulk Operations, it gives you an option to re-save nodes at admin/content.

|improve this answer|||||
  • I am not sure this will work with one date field with repeating dates and another field with single date (with setting of unlimited). This may work in the case where both fields are only single values. – Nigel Waters Sep 9 '13 at 22:59
  • I edited my answer covering those cases. Using dpm($entity) you can see what the structure of your date fields is. dpm comes with the Devel module. – Kari Kääriäinen Sep 16 '13 at 8:11
  • Please don't recommend the [und] syntax. Use field_get_items instead. – Letharion Sep 16 '13 at 8:28
1

I think you can do it with the Views Natural Sort . It have the option to sort the fields means - Enable Sorting for Specific Fields . If you follow this tutorial you will get an idea!!.

|improve this answer|||||
0

The only sneaky way of making this happen is if you do not use pagination, then you can use PHP sorting in a custom query override that doesn't rely on the database.

According to stevector,

Pagination is what really stops us from sorting by computed fields because normally we need to say "Database! get me results 21-30 when sorting by the nid column".

So you can't reliably get results 21-30 from the db if the db doesn't know how to define the 21-30 (like in the case of a computed field).

So the most practical and reliable solution is to use Computed Field as mentioned by Kari Kääriäinen.

|improve this answer|||||
  • It looks like someone downvoted me after I specified version number and fixed a copy paste error. Is there something wrong with this answer? – beth Sep 17 '13 at 21:51
  • I upvoted earlier for the module approach, but later downvoted because this, just like any other, mine included, isn't a usable answer because the question itself is still unclear, at least to me. – Kari Kääriäinen Sep 18 '13 at 9:19
  • The question seems clear to me. I'll add some clarification to my answer. – beth Sep 18 '13 at 15:59
  • Never mind, I realized Views doesn't behave exactly like I though it does. – beth Sep 18 '13 at 16:24
0

This is a bit of a hacky way to do it, but it works and you don't need any additional modules.

Create a label for the field that you want to sort on and do NOT exclude it from display. Instead, rewrite the display as "blank" by adding an empty html <!----> comment as the rewrite value. Then add your other php/rewritten field as a child of this column.

This will display the table header for the correct column for sorting.

|improve this answer|||||

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.