5

I am building a module where I would like to use the fields from a user generated view. So let's say the user create a view with 3 fields: Class, Subject, & Nid. I run views_get_view_result() on the view, but that returns entire node objects.

How do I get the simple values like this: {"1 grade", "English", 54} as an array perhaps?

Remember I don't know beforehand what fields have been selected by the user, so I have to get the raw data like that, or be able to see what fields have been selected and in what order.

Just did a print_r on $view->result[1].

stdClass Object
(
    [node_title] => "The Trial" by Franz Kafka
    [nid] => 52
    [node_created] => 1334720754
    [field_data_field_subject_node_entity_type] => node
    [_field_data] => Array
        (
            [nid] => Array
                (
                    [entity_type] => node
                    [entity] => stdClass Object
                        (
                            [vid] => 52
                            [uid] => 1
                            [title] => "The Trial" by Franz Kafka
                            [log] => 
                            [status] => 1
                            [comment] => 2
                            [promote] => 1
                            [sticky] => 0
                            [nid] => 52
                            [type] => assignment
                            [language] => und
                            [created] => 1334720754
                            [changed] => 1334733704
                            [tnid] => 0
                            [translate] => 0
                            [revision_timestamp] => 1334733704
                            [revision_uid] => 1
                            [body] => Array
                                (
                                    [und] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [value] => Many attempts have been made to examine Kafka's legal background and the role of law in his fiction. These attempts remain relatively few in number compared to the vast collection of literature devoted to the study of his life and works, and marginal to legal scholarship. Mainstream studies of Kafka's works normally present his fiction as an engagement with absurdity, a critique of bureaucracy or a search for redemption, failing to account for the images of law and legality which constitute an important part of "the horizon of meaning" in his fiction.

However, James Hawes argues many of Kafka's descriptions of the legal proceedings in The Trial – metaphysical, absurd, bewildering and "Kafkaesque" as they might appear – are, in fact, based on accurate and informed (although exaggerated) descriptions of German and Austrian criminal proceedings of the time, not well understood by many British or American people, who were familiar with an adversarial rather than inquisitorial system of justice. Similarly, the requirement for the traveller to register with the authorities in The Castle to stay a night seems repressive and odd to Britons and Americans, whereas in the present-day Germans (and most continental Europeans) are required to register their address (and hoteliers their guests) with the local authorities.

The significance of law in Kafka's fiction is also neglected within legal scholarship, for as Richard Posner pointed out, most lawyers do not consider writings about law in the form of fiction of any relevance to the understanding or the practice of law. Regardless of the concerns of mainstream studies of Kafka with redemption and absurdity, and what jurists such as Judge Posner might think relevant to law and legal practice, the fact remains that Kafka was an insurance lawyer who, besides being involved in litigation, was also "keenly aware of the legal debates of his day" (Ziolkowski, 2003, p. 224).

In a recent study which uses Kafka's office writings as its point of departure, Reza Banakar argues that "legal images in Kafka's fiction are worthy of examination, not only because of their bewildering, enigmatic, bizarre, profane and alienating effects, or because of the deeper theological or existential meaning they suggest, but also as a particular concept of law and legality which operates paradoxically as an integral part of the human condition under modernity. To explore this point Kafka's conception of law is placed in the context of his overall writing as a search for Heimat which takes us beyond the instrumental understanding of law advocated by various schools of legal positivism and allows us to grasp law as a form of experience"
                                                    [summary] => 
                                                    [format] => filtered_html
                                                    [safe_value] => 

Many attempts have been made to examine Kafka's legal background and the role of law in his fiction. These attempts remain relatively few in number compared to the vast collection of literature devoted to the study of his life and works, and marginal to legal scholarship. Mainstream studies of Kafka's works normally present his fiction as an engagement with absurdity, a critique of bureaucracy or a search for redemption, failing to account for the images of law and legality which constitute an important part of "the horizon of meaning" in his fiction.

However, James Hawes argues many of Kafka's descriptions of the legal proceedings in The Trial – metaphysical, absurd, bewildering and "Kafkaesque" as they might appear – are, in fact, based on accurate and informed (although exaggerated) descriptions of German and Austrian criminal proceedings of the time, not well understood by many British or American people, who were familiar with an adversarial rather than inquisitorial system of justice. Similarly, the requirement for the traveller to register with the authorities in The Castle to stay a night seems repressive and odd to Britons and Americans, whereas in the present-day Germans (and most continental Europeans) are required to register their address (and hoteliers their guests) with the local authorities.

The significance of law in Kafka's fiction is also neglected within legal scholarship, for as Richard Posner pointed out, most lawyers do not consider writings about law in the form of fiction of any relevance to the understanding or the practice of law. Regardless of the concerns of mainstream studies of Kafka with redemption and absurdity, and what jurists such as Judge Posner might think relevant to law and legal practice, the fact remains that Kafka was an insurance lawyer who, besides being involved in litigation, was also "keenly aware of the legal debates of his day" (Ziolkowski, 2003, p. 224).

In a recent study which uses Kafka's office writings as its point of departure, Reza Banakar argues that "legal images in Kafka's fiction are worthy of examination, not only because of their bewildering, enigmatic, bizarre, profane and alienating effects, or because of the deeper theological or existential meaning they suggest, but also as a particular concept of law and legality which operates paradoxically as an integral part of the human condition under modernity. To explore this point Kafka's conception of law is placed in the context of his overall writing as a search for Heimat which takes us beyond the instrumental understanding of law advocated by various schools of legal positivism and allows us to grasp law as a form of experience"

[safe_summary] => ) ) ) [field_files] => Array ( [und] => Array ( [0] => Array ( [fid] => 16 [display] => 1 [description] => [uid] => 1 [filename] => Franz Kafka - The Trial.pdf [uri] => public://Franz Kafka - The Trial.pdf [filemime] => application/pdf [filesize] => 977591 [status] => 1 [timestamp] => 1334720754 [rdf_mapping] => Array ( ) ) ) ) [field_submissions] => Array ( ) [field_class] => Array ( [und] => Array ( [0] => Array ( [value] => 9B ) ) ) [field_subject] => Array ( [und] => Array ( [0] => Array ( [value] => English ) ) ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item [1] => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date [1] => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1334720754 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => admin [picture] => 0 [data] => a:1:{s:7:"overlay";i:1;} ) ) ) [field_field_subject] => Array ( [0] => Array ( [rendered] => Array ( [#markup] => English [#access] => 1 ) [raw] => Array ( [value] => English ) ) ) )

In the view I have selected:

Content: Title
Content: Subject (Subject)
Content: Nid (Nid)

Solution:

    $view_fields = $view->style_plugin->options['columns']; 
    foreach ($view->result as $index => $row) {
        $view->row_index = $index;

        foreach($view_fields as $field){    
            $field_output = $view->style_plugin->get_field_value($index, $field);
            error_log($field.": ".$field_output."\n", 3, "debug.log");
        }
    }

Instead of the error_log build an array instead or whatever you need

1
  • if you could edit your question and include the print_r of what you are getting here, that might help track down your issue..
    – Jimajamma
    Commented Apr 22, 2012 at 16:39

2 Answers 2

6

EDIT:

Sorry, didn't quite read this exactly. $view=views_get_view_result() returns that result array. If you want them in array form, you can cast them, eg, $row0=(array) $view[0]; and now you would have a keyed array of the views results for the first row along with some views housekeeping information like the nid if present, etc. I should also mention that at times when dealing with nodes, Views only returns the id, or nid, and it's in the render stage that this is turned into a link or body, etc.

ORIGINAL:

Within the $view object, you have $view->result which is an array of values, eg, $view->result[0] contains all the raw data for the first row, $view->result[1] contains all the raw data for the second row, etc.

MORE INFORMATION:

I created a view here that has my 'tagline' field in it, and the result looks like this:

    Array
    (
        [0] => stdClass Object
            (
                [node_data_field_tagline_field_tagline_value] => The Dirty Dirty Duval
                [node_type] => area
                [nid] => 1
                [node_vid] => 1
            )

        [1] => stdClass Object
            (
                [node_data_field_tagline_field_tagline_value] => Atlanta GA is in the house...
                [node_type] => area
                [nid] => 18
                [node_vid] => 18
            )

so I see my tagline field up in there (along with other views housekeeping info) so I am not sure what you are seeing unless your view row style is node instead of fields, but then all you would see is the nid at this point...

9
  • Thanks for the reply :-), however I just did a grep -r "views_get_result" * in the views directory. There is no function called views_get_result() or am I misunderstanding something?
    – Jens
    Commented Apr 19, 2012 at 2:13
  • sorry, that was a typo on my part...I meant to repeat your views_get_view_result() (which is a slick wrapper around views_get_view() that returns $view->result)
    – Jimajamma
    Commented Apr 19, 2012 at 5:04
  • yeah, that's the one I have been using. The problem with that is that you get the entire node object, and no information regarding what fields have been selected by the user and the order of those fields. I have opened an issue on the views project on drupal.org. It seems a bit strange that this function is not commonly used/known, since I would assume that other module developers would need this functionality. Hopefully MerlinOfChaos will respond to my post. But thanks anyway :)
    – Jens
    Commented Apr 19, 2012 at 6:16
  • Maybe I am not understanding your question/comment, but $view->result is an array of objects that describes each row returned by views, and as such includes each and every field there.
    – Jimajamma
    Commented Apr 19, 2012 at 7:15
  • When I do a print_r on the result of views_get_view_result(). I get the entire node object, not the field selected. So if I have a field in my node called Subject, and I only select subject in my view. I would expect to get an array that consists of only the subject field. not the body, title etc.
    – Jens
    Commented Apr 19, 2012 at 14:30
3

This probably piggybacks on the Jimajamma's post above, but this was some code that I pulled from somewhere (I forget where, it was in the Views queue) for dealing with a similar kind of situation.

$view = views_get_view('view_machine_name');
$view->set_display('view_display_name');
$view->pre_execute();
$view->execute('view_display_name');
foreach ($view->result as $result) {
  // do something with your results
  // print_r() or dsm() from the devel module would be helpful here.
  // dsm($result);
}
2
  • true true, in fact, up until your foreach, that's pretty much the guts of views_get_view_result() :)
    – Jimajamma
    Commented Apr 22, 2012 at 17:04
  • Good to know. Would be great to get things a little more succinct. :) Commented Apr 23, 2012 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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