Views Custom Field Rocks. But it would be even better if it were possible to sort on Custom Fields.

CommentFileSizeAuthor
#10 views_customfield.sorting.patch5.74 KBinfojunkie

Comments

casey’s picture

It uses PHP afterwards to generate row-specific text so its currently not possible.

Could you give some examples how you like to use it? Sort on what?

ipto’s picture

Version: 5.x-1.0-beta1 » 6.x-1.x-dev

For example have values row: 8 / 2, 10 / 5, 8 / 7, 3.4 / 2, 8 / 6 (before '/' - average fivestar value, after - voting user count), you need to sort this value as a string:

10/5
8/7
8/6
8/2
3.4/2

To do this, I glued them into one customfield field, but only now noticed that sort of pressing header in the table does not.

In views theming filles (\modules\views\theme\) I have full access to all views fields table, and just merge (or do other activities with them) they is not difficult without customfield :

          <td class="views-field views-field-voting_value">
            <?=((!empty($row[value]) && is_numeric($row[value])) ? (round($row[value]/10, 2)) : '&nbsp;')?>
            <?=(!empty($row[value_1]) ? '/' . $row[value_1] : '&nbsp;')?>
          </td>
casey’s picture

You already can do this. Just add two sorts to your view; first on "average fivestar value" second on "voting user count".

Let me know if that's what you're looking for.

ipto’s picture

Yes, ask if the criteria for grading in block vies, all appear as you want, but when you click on the header field tables default sorting break and starts sorting the field corresponding pressed headline, that is why I combine two fields (value_average and value_count) in one

casey’s picture

I could introduce a new customfield "fieldcombination" which combines two or more fields in the query; then it would be possible to sort on those fields.

But currently I don't have much time; I'll look into it next week.

casey’s picture

Status: Active » Postponed

Sorry don't have time for this on the moment.

asak’s picture

I'm using CustomField to calculate some user info, which results in a single or double digit number (e.g 1 or 3 or 24) - how can i get views to enable sorting by this results?

casey’s picture

You can't, as customfields are evaluated after querying of the (sorted) result.

I have however some ideas to introduce some fields that allows to add calculations (or other sql functions) to a views query. Unfortunatly I don't have very much time right now; so it can take some time before this is implemented.

emagus’s picture

Hey casey.

First of all: Thanks for your great module.
Reading this, i get the feeling this isn't gonna happen too soon, which i totally understand. It's a rather critical feature for me (and possibly many others). So it would be really nice if you could share your aforementioned ideas with us. Maybe someone who really needs this too and who is an experienced drupal developer is able to implement your ideas. And maybe, if i feel like it's easy enough for me, I will even try this myself (even though i'm very low skill on php and new to drupal development).

thanx in advance

infojunkie’s picture

StatusFileSize
new5.74 KB

I use this module all the time, thanks for a great contribution.

Attached is a patch to provide sorting for PHP Code custom fields. I'd be happy if people here could use it and validate it, and if the module maintainer would consider committing it in. Since I need this for my professional (commercial) work, I'm willing to provide support for it. If it works well, I can extend this to Row Number field as well (I guess it doesn't make sense for Markup).

To activate sorting, just choose a sorting method in the field's settings. Make sure the fields in the Table style settings are marked as sortable.

casey’s picture

@kratib For about a month I don't have time to work on my drupal modules. I could give you CVS access?

infojunkie’s picture

Status: Postponed » Needs review

@casey thanks for our offer. Go ahead and grant me CVS access. In any case I will wait to hear from people who tried the patch before committing it.

Did you get a chance to try the patch?

casey’s picture

CVS access granted.

No, don't have time for anything at all (working on a paper for my study with a short deadline and after that a couple of exams).

I was actually also working on a solution for this. My idea was to allow the usage of views sort handlers as clicksorts (configurable per custom field). Don't have it fully working yet though.

infojunkie’s picture

Right now my patch is just for the field handler to be click sortable. I don't know what it takes to write a sort handler, so I'll wait for people to tell me which one they need.

emagus’s picture

@kratib: Tested the patch and works perfectly fine so far for me! Thank you so much!!! Great job!
@casey: Thank you for giving kratib cvs access. That's true open source spirit! And also thanx for checking your issue queue despite your school stress!

infojunkie’s picture

Status: Needs review » Active

I just committed the above patch (tweaked for performance) to the latest dev release. I tested it on a view with 10K nodes so things look alright to me. Note that this applies to PHP custom field only, not Rownumber yet.

I'll keep this active until I commit a similar patch for Rownumber, which will probably require refactoring some code to a common ancestor of these two classes to avoid code duplication.

infojunkie’s picture

FYI: I just found a bug that happens when the PHP code generates HTML to be rendered. I committed a fix to the latest dev.

ari-meetai’s picture

Well, this last commit would be great for me if it would work. I've tried with all sorting types and nothing.

I'm trying to show the user's first + last name, eg:

echo '<a href="profile/' . 
strtolower(str_replace(" ", "-", 
$data->node_data_field_location_field_name_value . "_"
. $data->node_data_field_location_field_surname_value)) . '"'
. ' rel="lightframe[|width:670px; height:490px;]">'
. $data->node_data_field_location_field_name_value . ", "
. $data->node_data_field_location_field_surname_value
. '</a>';

(tried also with simpler versions)

The output is correct, but I just can't sort... The column title doesn't appear as a link..

Any help appreciated.

infojunkie’s picture

If you're using table output, then you need to open the table settings and set this field as sortable. Does that apply?

ari-meetai’s picture

Oh yes! Forgot that... Thank you for the quick answer!

Great job.

markus_petrux’s picture

@kratib: Could you please add a note in the description of the sorting option to describe a little that sorting will load all records of the query in memory to resolve the sort? Please. On my installation there is staff that can create views and I have given this module to them, but they may try to against thousands of records.

I would even ask for an option to disable sorting for PHP fields, or maybe if there was a PHP field sortable, and another one that is not sortable, then we could use the later so that users cannot take the site down when creating and then running a view against a huge number of records.

Other than that, nicely done. ;-)

Thanks

infojunkie’s picture

@markus_petrux: I added the warning note as a description to the field. It is easy to set the sorting option to none (which it is by default). Also, sorting in memory won't occur until the header of the custom field is actually clicked.

markus_petrux’s picture

Thank you. This will save me one or two headaches.

tengoku’s picture

hi.... thanks for the patch... it works on table outputs.... sadly i use de grid output.. and don't works.... u have to do something special to this??

infojunkie’s picture

The implemented feature is only for tables so far. To make it work for other styles, one has to implement sort handlers which is still not done. You can open a new feature request for it if you wish.

Bilmar’s picture

subscribing

YK85’s picture

subscribing

Pidot’s picture

Category: feature » bug

nvm this post, the dev version contains a fix

if you use the patch, the behaviour changes:

for example if you insert this in your customfield:
<?PHP echo "<ul><li>bla</li><li>bla</li></ul>" ?>

it outputs the html code and it doesn't make a list...

infojunkie’s picture

Category: bug » feature
Status: Active » Fixed

Marking as fixed. For bugs, please open new issues.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

phyadmin’s picture

So this patch has been tested and verified to work...yet it still hasn't been pushed to the production version of this module? What's the deal? If it works, megre it and add a module configuration settings page for this that makes it optional. If not, then don't mark the feature request as fixed. But a user shouldn't have to hunt down an old forum thread from X months ago just to add a feature.

wcndave’s picture

Also, this patch is for custom field, however in D7 it's just called views php so needs to be ported to views php as well...