Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
merlinofchaos CreditAttribution: merlinofchaos commentedI'm sorry, what is the issue? All this seems to do is point to other issues. What is the issue you're reporting?
Comment #2
mmilo CreditAttribution: mmilo commentedI 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:
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!
Comment #3
dawehneryou have to use $view->field['yourfield']->get_value($view->result[0]); to get the actual data, there is no way around it.
Comment #5
mmilo CreditAttribution: mmilo commentedThat produced the following:
It should be noted that
result[0]
is justThis is also happening in a post_execute phase. Is this information just not available at that point maybe?
Comment #6
merlinofchaos CreditAttribution: merlinofchaos commentedField 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.
Comment #7
mmilo CreditAttribution: mmilo commentedCool, 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?Comment #8
dawehnerThe 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.