I'm not 100% sure if this is Views-related.

This sounded very similar to #1063418: views_get_view_result drupal 7 returns "node" for fields but related to the other module. I haven't been able to find the cause, but I suspect it's to do with the Views3 API.

Issues are #1188158: Custom Fields Do Not Work As Variables In PHP Filter or #1061388: Add field data to the result set. The module can't operate as expected as a result of this problem.

Thanks!

Comments

merlinofchaos’s picture

Status: Active » Postponed (maintainer needs more info)

I'm sorry, what is the issue? All this seems to do is point to other issues. What is the issue you're reporting?

mmilo’s picture

Category: bug » support
Status: Postponed (maintainer needs more info) » Active

I suppose it's more of a support request.

For the module listed above, $view->result doesn't provide anything meaningful. It would provide something like:

    [0] => stdClass Object
        (
            [nid] => 2
            [file_managed_file_usage_uri] => public://employee_photos/guy3.jpg
            [node_title] => Ian Bobian
            [field_data_field_job_title_node_entity_type] => node
            [field_data_field_dept_node_entity_type] => node
            [field_data_field_fun_fact_node_entity_type] => node
        )

Rather than the CCK fields. This was similar to the above Views issue. However it's propogating in the Views_PHP module, and not with views_get_view().

I could be wrong but it seems more of an issue with Views3. Or is it that the module is not using the API properly (although it does just seem to function as a views plugin)?

Thanks!

dawehner’s picture

Status: Active » Fixed

you have to use $view->field['yourfield']->get_value($view->result[0]); to get the actual data, there is no way around it.

mmilo’s picture

Status: Fixed » Active

That produced the following:

Fatal error: __clone method called on non-object in \sites\all\modules\contrib\views\modules\field\views_handler_field_field.inc on line 650 

It should be noted that result[0] is just

stdClass Object
(
    [nid] => 20
    [node_created] => 1318440058
    [field_data_field_body_node_entity_type] => node
    [field_data_field_ytube_vidos_node_entity_type] => node
    [field_data_field_path_node_entity_type] => node
)

This is also happening in a post_execute phase. Is this information just not available at that point maybe?

merlinofchaos’s picture

Status: Active » Fixed

Field API does not allow us to directly query the data. Therefore, its data will not be in $view->result because it is not part of the query. Instead, the design of field API requires us to load the entity -- so we add the entity type and entity ID to the query and use that to load the associated entity, and then send that to the formatter to extract the data.

It has never been a very good idea to rely on $view->result in general in any case, but in Drupal 7 it is now very, very serious: Never look at $view->result. You have to use what the handlers give you.

->get_value() can only be called after pre_render(). So use hook_views_pre_render() rather than post_execute() and you can get your values using the get_value() method.

mmilo’s picture

Cool, thanks for the clarification, that's useful to know.

Just one more question - if we're executing a view programmatically (ie. views_get_view) is there a best practice to use instead? Or is your advice more related to when developing Views plugins?

dawehner’s picture

The best way is to use views_embed_view(), but you might need the full $view object. If you need this $view = views_get_view() + $view->preview should do it for you.

For just execution there is views_get_view_result.

Status: Fixed » Closed (fixed)

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