Closed (fixed)
Project:
Entity API
Version:
7.x-1.x-dev
Component:
Code - misc
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
1 Mar 2011 at 11:38 UTC
Updated:
6 Sep 2011 at 12:19 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
fagofor 2) we probably want to wait on #975400: Refactor field render() functions to accept $value, allowing reuse to land first.
Comment #2
drunken monkeyWould be a great addition, subscribing.
Code from the linked Search API issue and from efq_views could be a base for 1).
Maybe a style plugin (instead of a row style) would be better, though, because it would require only a single call to
entity_view().Comment #3
fagoYep, also 2) is something that efq_views has already done I think + search api does on its own too.
Comment #4
fago>For that the handlers need a way to determine the entity_type / entity to use though.
So what about adding an optional 'entity type' key to views base tables, such that modules may specify the relation there? So we could read it from there + just pass pass the entity id as field value. For cross-entity use cases we'd could support reading it from $this->alias['entity type'] if the base table hasn't specified the type.
Comment #5
fagook, here is a try of a generic entity-view row-style. It works for all base tables which specify the 'entity type', whereas backends like efq_views / search_api would have to specify 'skip entity load' => TRUE too to skip a second entity load (which would be cached but anyway).
thoughts?
Comment #6
drunken monkeyLooks great! And, after shortly testing it, I can say that it also works great.
I wanted to do the same once, store additional information in the base table definition, but didn't come up with a way to do it. Good to see it does work, if you know how.
OK, if you are already committing this soon, I can probably just abandon my own solution (#1064518: Support entity-based Views handlers) and wait for yours. Since there isn't even a dependency (I only have to change one internal array key and add two yet meaningless keys to views data definitions), I can even support this before you commit it. ;)
One thing, though: What about calling
entity_views()on all entities at once, like you mentioned in the Search API issue? Not practicable? Or not possible, due to inconsistent returned render arrays?And you're right, of course, 2) is also already implemented at least in the Search API.
Comment #7
fago>...I can even support this before you commit it. ;)
Awesome!
>One thing, though: What about calling entity_views() on all entities at once, like you mentioned in the Search API issue? Not practicable? Or not possible, due to inconsistent returned render arrays?
Ops, forgot about that. We should do it that way.
While this "guessing" works probably for most cases, it's a bit weird and not extensible. Maybe we should off-load that to the query class instead?
Comment #8
drunken monkeyI think, most query classes will be able to take this into account and use one of the available methods (of course they should be documented). What we could additionally do here is to provide an optional way to circumvent this behaviour, e.g. with:
Comment #9
fagore-rolled #5 with -p0 for drush make
Comment #10
fagore-rolled #5 with -p0 for drush make
Comment #11
Shadlington commentedSubscribing
Comment #12
bojanz commentedDon't have time to look into this right now (still no internet in Paris apartment), but I'm pleased to announce that #975400: Refactor field render() functions to accept $value, allowing reuse has landed :)
EDIT:
Tested the patch with efq_views, works nicely.
However, that file needs to go into plugins/ not into handlers/.
Also, not sure why you're using non standard class naming, since views is still using the underscore way of naming classes (views_plugin_row), following that pattern might be a good idea.
Preparing a patch that adds handlers and some other nice things, stay tuned.
Comment #13
remidewitte commentedsubscribe
Comment #14
fago>However, that file needs to go into plugins/ not into handlers/.
Indeed.
>Also, not sure why you're using non standard class naming, since views is still using the underscore way of naming classes (views_plugin_row), following that pattern might be a good idea.
I just took that over from the search api. While I think this needs to be fixed in views, we should probably follow its pattern for the integration code.
bojanz, would do you think about the idea proposed in #8? Maybe, we could improve Views itself to follow the same pattern for retrieving entities?
Comment #15
bojanz commented> I just took that over from the search api. While I think this needs to be fixed in views, we should probably follow its pattern for the integration code.
Agreed. When Views fixes it, the pattern can change everywhere.
> bojanz, would do you think about the idea proposed in #8? Maybe, we could improve Views itself to follow the same pattern for retrieving entities?
Interesting idea, definitely something to think about.
I'm developing a commerce contrib module today and eating my dogfood (Entity API + my patches for Views integration and smarter UI generation), so once I see how it all works in practice (today), I'll update the issue with the code and results..
Comment #16
Shadlington commentedDid you get anywhere with this bojanz?
I applied the patch in #10 and whilst it made 'Entity View' available as an option, selecting it and clicking apply doesn't actually change the value. Its stuck on Fields.
Possibly a casualty of the new Views UI?
Comment #17
drunken monkeyAfter thinking about it a bit more, it wouldn't harm to use something like ":entity" instead of "entity" as the key in $values. There isn't really any downside, and even if it's rather unlikely that a table has a column "entity", it could mess things up a bit if it does happen.
Comment #18
bojanz commentedSorry, completely forgot about it (last week was though). I'll post my progress today.
Comment #19
reptilex commentedWould this allow to include a profile2 field into a node view?
I really would need that. It seems to me that it is just one step up from including a user field, isn't it?
Comment #20
bojanz commentedI'm very tired so I'm gonna leave further explanations for tomorrow.
This applies both to current Entity code, and the current Entity code with the first patch of this issue applied.
I added support for displaying properties (which do not map to sql columns). Each property handler is the same, modeled after views_handler_field_field.
I also added support for overriding generated Views data, by providing data in the property info (so you can substitute your own handler, or make it not have a filter/field/argument/sort handler at all).
EDIT: I see that I added the docs to the entity info by mistake. That can be changed in a later reroll.
Comment #21
cnsmiles commentedI created a view type of profile and did not see any of my profile fields values under filter which happen to be term referenced. All I see is reference to tid and delta. I also tested this with the user view type. I can pull them as fields but not under filter. I'm doing a profile2 based view. I know this works under the content views. Will what you providing address this?
Comment #22
bojanz commentedNo, what you're describing is totally unrelated to this issue and patch.
Comment #23
joachim commented@bojanz: would adding fieldAPI fields to views for entities then be a follow-up issue to this one?
Comment #24
bojanz commentedThere's nothing stoping you from adding fields to your custom entity, it should work out of the box.
If it doesn't, then yeah, it will be a followup here somewhere :)
Comment #25
joachim commented> it should work out of the box.
Nope, it doesn't -- fields added to Profile2 entities don't show as fields to be added in a view :/
Comment #26
bojanz commentedDefinitely a bug I'll be looking into then.
(Edit: I'm using the Entity API views integration with my patch, and it shows fields attached to my custom entity. Can you try if it works for you with my patch?)
Comment #27
Shadlington commentedUhhhhh... The fields attached to my profile2 entities are showing in views.
No patch involved. Just the latest versions of entity, views and profile2.
Comment #28
Shadlington commentedHmmmm. Actually, this is a bit odd.
If I add an existing field to a profile type, it shows up in views under the 'Fields' filter.
If I add a new field to a profile type, it shows up in views under the 'Profile' filter.
I can't imagine this is intentional. Anyway, that is probably a bug in Profile2 rather than Entity API.
Comment #29
bojanz commentedI think that's normal, Views uses an appropriate group name (entity type) if the field is only attached to one. If it's attached to several, it falls back to "Fields".
Comment #30
Shadlington commentedOh, I see. Well, it works is the main thing.
EDIT: The patch in #20 doesn't apply cleanly (against either beta8 or the latest dev)
patching file entity.api.php
patching file entity.info
Hunk #1 FAILED at 2.
1 out of 1 hunk FAILED -- saving rejects to file entity.info.rej
patching file entity.module
patching file views/entity.views.inc
Hunk #2 succeeded at 52 (offset -26 lines).
Hunk #3 FAILED at 96.
Hunk #4 succeeded at 139 (offset -26 lines).
1 out of 4 hunks FAILED -- saving rejects to file views/entity.views.inc.rej
patching file views/handlers/entity_handler_field_property_boolean.inc
patching file views/handlers/entity_handler_field_property_date.inc
patching file views/handlers/entity_handler_field_property_numeric.inc
patching file views/handlers/entity_handler_field_property_string.inc
patching file views/handlers/entity_handler_field_property_uri.inc
Comment #31
bojanz commentedTry the git checkout.
The info file has additional info in downloaded releases, which is why the patch doesn't apply there.
Rerolled for the entity.views.inc part, but I am not sure I solved the conflicts correctly, I'm a bit sick at the moment. So read it with caution ;)
Comment #32
Shadlington commentedGot the git checkout and applied that patch:
patching file entity.api.php
patching file entity.info
Hunk #1 FAILED at 2.
1 out of 1 hunk FAILED -- saving rejects to file entity.info.rej
patching file entity.module
patching file views/entity.views.inc
Hunk #2 succeeded at 52 (offset -26 lines).
Hunk #3 succeeded at 69 with fuzz 2 (offset -27 lines).
Hunk #4 succeeded at 122 (offset -27 lines).
patching file views/handlers/entity_handler_field_property_boolean.inc
patching file views/handlers/entity_handler_field_property_date.inc
patching file views/handlers/entity_handler_field_property_numeric.inc
patching file views/handlers/entity_handler_field_property_string.inc
patching file views/handlers/entity_handler_field_property_uri.inc
Sorry, doesn't seem to have worked :(
It looks like the patch is expecting a line like this: 'files[] = views/plugins/entity.row_view.inc' to already be in my .info, but it isn't.
Anyway I'll sort out the .info manually.
Will report on findings shortly.
Hope you feel better soon :)
Comment #33
Shadlington commentedWhat exactly should I do to test this?
I'm not clear what has changed from a user's point of view.
Comment #34
bojanz commentedThis is a "frameworkish" patch, so what it needs is a developer's not a user's point of view.
Comment #35
Shadlington commentedOopf. Sorry. I thought I saw a UI change after I applied one of the patches earlier in the issue.
I'll be quiet now :)
Comment #36
drunken monkeyThe Search API already supports part one of this feature (the row display plugin). So that can already be tested.
Comment #37
Shadlington commentedAh, well in that case it didn't appear to work. Unless I need to be applying the patch I applied previously (#10) as well. When I applied that I did seem to get a new row display plugin called 'Entity View' but it wasn't working (see my post #16).
Comment #38
drunken monkeyYes, sorry, phrased that ambiguously. The patch in #10 is what I meant with "part one".
And as for it not working: fago is on vacation until April 26, so don't expect any fixes until then. Except Bojan (or I, or someone else) would fill in.
Comment #39
daften commentedsubscribe
Comment #40
Shadlington commentedAh. I just had another try and I got it to work.
I didn't realise that this patch was in two parts.
#10 followed by #31 (with the .info bit applied manually - still having problems with that) allows me to select 'Entity View' as a row style.
And a very brief initial test shows it works!
EDIT: After much further testing I haven't come across a single problem. I love it :D
Comment #41
amitaibuI've started moving OG's views integration to use entity metadata. Subscribe for now.
Comment #42
joachim commentedHas patch been committed yet? I just cloned the latest EntityAPI and I get relationships both ways and fields on profiles.
Only missing piece for Profile2, I think, is #1114454: Views integration: create an entity-entity relationship that limits by entity type if applicable.
Comment #43
amitaibu> I just cloned the latest EntityAPI and I get relationships both ways and fields on profiles.
If profile2 doesn't declare hook_views_data() entity is doing it for you.
Comment #44
bojanz commentedEntity has Views integration even without this patch(es).
My patch provides property handlers (used in efq_views, search api and other places) and support for displayng properties that don't map to schema fields (database columns).
It also provides a way to override the handler from hook_property_info() (so you can specify "my_handler_field_numeric" instead of the default one, for example).
The other patch provides a style plugin that uses entity_view (similar to the node row plugin for nodes)
Comment #45
404 commentedsubscribe
Comment #46
fangel commentedAfter applying these patches I have a problem exporting views (both using features and views directly). The error I get is
Fatal error: Call to a member function get_option() on a non-object in ../sites/NN/modules/views/plugins/views_plugin_style.inc on line 35
(I only use the entity row-style, so I'm assuming it's related to that)
Comment #47
drunken monkeyI get this error, too, but I'm not sure it's related to this patch. Both before and after reverting the patch, I get this for some views when editing argument handler options. Just checked, and yes, for those views I also get a fatal error when exporting.
It seems, that
views_plugin_style::init(&$view, &$display, $options = NULL)will sometimes be called with the second and third parameter both NULL, which causes the error.Did you use an argument handler in the view in question? Can you please check whether reverting the patch and/or removing the argument handler (if applicable) resolves this?
If it really is a Views bug and we both get it, we should probably create an issue in the Views queue.
Comment #48
fangel commentedI am indeed using a argument (search_api_index_fulltext_search). I will check with and without the patch and the argument tomorrow when I get back to work, and report my findings.
Comment #49
fangel commentedOkay then. The results are in :)
So it's clear that the bug isn't in these set of patches, as they do not affect the bug. However, the SearchAPI fulltext argument is to blame.
@drunkenmonkey, do you want to take it from here or should I start a separate issue on the SearchAPI tracker?
-Morten
Comment #50
drunken monkeyPlease start a new issue in the Search API queue.
I am, however, not sure whether the fault here lies with the Search API or with Views, or in between. Please mention in the Search API issue also whether you have the same problem with all arguments or just with the one. (The three different cases are normal fields, fulltext fields and "Search: Fulltext search".)
Comment #51
amitaibu@bojanz,
I couldn't apply patch with "git apply", it needs a re-roll?
Comment #52
bojanz commentedTried "patch -p1" ?
I'll take a look tomorrow and reroll if needed.
Comment #53
amitaibu> Tried "patch -p1" ?
Yes, didn't work for me
Comment #54
Shadlington commentedIf I wanted to add in some custom code to selectively pick a view mode for each row based on specific criteria (for example, whether a 'featured' check box was ticked or not for a given node) then where would the best place to do this be?
I can quickly achieve this by sticking some code into EntityViewsRowView's render function but I'd rather do this in a less hackish way.
Any advice would be much appreciated.
Comment #55
drunken monkeyYou should then probably subclass the
EntityViewsRowViewclass and add code (and maybe even an options form) for this.The row style could then be added in
hook_views_plugins()with the same code that this module uses.Comment #56
girishmuraly commented@bojanz @Shadlington based on #40, I am able to get entity based build modes (I am using this with search_api). So I have attempted to combine patches #10 and #31 (since #31 had problems applying). Hope this works.
Comment #57
bojanz commentedHere's a rerolled entity row patch. Should be ready for commit.
Comment #58
becw commentedA couple comments:
pre_render()method, this checks whether view results are arrays or objects. Views core assumes that results are objects, so I think this code should work from that assumption as well. This looks like it is written to accommodate Search API Views' query plugin behavior--it returns Views results as arrays--which is being addressed in #1089758: Make use of new flexibility for Views field handlers.Attached is a version of this patch with these changes.
Comment #59
fagoFinally, I've created #1172970: provide a unified way to retrieve result entities. Please add your comments there.
@patch:
I'm not sure about that, isn't it up to the query backend how the results look? Stuff that comes with views is basically meant to work for the default-backend, so it doesn't have to bother with, yes. But I don't think that means everyone has to follow that.
Else, the patch looks good to me. I guess we should go and add this now, so people can start using it + update it based on the solution of #1172970: provide a unified way to retrieve result entities?
Comment #60
JoeMcGuire commentedI was looking at this issue for an issue on the Search API Views module #1109318: Views: Add option to display image fields as image
I am thinking it would be really helpful to have another row style "Entity API Fields" which was able to extend the work Views is doing to add Field API fields extending the existing handlers so we can use their option forms and render methods. But instead get values from an existing entity object instead of the database.
Comment #61
bojanz commentedI'm in the process of moving from France to Serbia so I'm pretty useless at the moment.
Views already works like that, Field API values are fetched from the entity (entity_load), not the db result set. Nothing else is needed.
Comment #62
drunken monkeyYou're probably right. It makes sense to follow Views there, but it needn't be mandatory if there's an easy way to accommodate both variants.
Yes, that would probably be the best way. Can't tell how long the Views issue will take …
Comment #63
fago>I am thinking it would be really helpful to have another row style "Entity API Fields" which was able to extend the work Views is doing to add Field API fields extending the existing handlers so we can use their option forms and render methods. But instead get values from an existing entity object instead of the database.
I think Views is already using the data from an entity-load for fields, so ideally it could be re-used for display only stuff with other back-ends too once #1172970: provide a unified way to retrieve result entities gets fixed. It uses the db for stuff like click-sorting and grouping though, so for the search-api case it would make sense to exchange that parts and to use the search-index for that if the field is in there.
>You're probably right. It makes sense to follow Views there, but it needn't be mandatory if there's an easy way to accommodate both variants.
Ok, let's do so then and support search-api arrays too. That way, our solution also works *now* with search api.
Comment #64
zambrey commentedsubscribe
Comment #65
sonar_un commentedSubscribing
Comment #66
bojanz commentedOkay, I'm back.
I think we should rename the row class from EntityViewsPluginRow to entity_views_plugin_row (or better yet, entity_plugin_row).
That follows the views naming convention as well as the names of the handlers in the #31 patch.
Regarding the #31 patch, any comments on that?
It aimed to do three things:
1) Provide a way to override a handler through entity metadata. Looking back at it, it can be removed. Anyone can use hook_views_data_alter(), no point in doing this.
2) Provide generic entity handlers.
3) Use them for displaying fields that don't map to db columns.
Comment #67
fago>I think we should rename the row class from EntityViewsPluginRow to entity_views_plugin_row (or better yet, entity_plugin_row). That follows the views naming convention as well as the names of the handlers in the #31 patch.
Yes, let's follow views here - it's views integration.
@patch in #31:
That sounds great, but let's care about that in a follow up and finish the view plugin row first, as this is mostly ready. Then I think we should move on with #1172970: provide a unified way to retrieve result entities, as this would help us greatly with #31 too.
Comment #68
bojanz commentedThis one follows the views naming (views_row_plugin_node_view => entity_row_plugin_entity_view).
Comment #69
drunken monkeyIt's also Drupal, so why not follow the Drupal coding standards? I still don't understand why Views insists on having a whole other set of coding standards, in places even diametrical to Drupal's.
But that's just my usual ranting, sorry. If you think it makes sense this way, let's do so.
However, the patch doesn't take into account fago's comment from #63:
While Search API now also uses objects, it still might make sense to not make this mandatory.
Comment #70
fagook, I re-rolled the patch to include support for the array variant too. Updated patch attached.
However, it didn't work with the latest search api any more. It seams like the search api says 'skip load' but then passed only an entity id? :/
Comment #71
becw commentedSearch API Views' has changed slightly, per #1089758: Make use of new flexibility for Views field handlers. Search API Views' query backend now returns view results as objects instead of arrays (following the example set by the default Views query backend), and will only load the full entity if the view is requesting fields that are present on the entity but not on the Search API result. This means that for some views, an
entity_load()will be run for each result, and for others it will not runentity_load()at all (this could possibly even vary per result)--it depends on the view's field configuration.That said, I haven't reviewed this version of the patch.
Comment #72
drunken monkeySorry, my understanding was that this would now only come along with a method for retreiving the results. Now, as Bec explained, the
entitykey on the results are a bit unpredictable.If you want, though, I could modify the patch here so it checks for whether a result contains an entity or just an ID, and then load accordingly, if necessary. (Or anyone else could do that, it's of course a very simple change – the question is whether you want such "magic" there?)
Comment #73
fagothanks, I've just added another fallback checking for ids even if skip-load is defined. Seems to work.
Updated patch attached. Another question, should we check for view access of the current user or just display it? I think views doesn't check access for node-view, but it does for fields. So I'm not sure what's the approach to follow here?
let's clean this mess up asap in #1172970: provide a unified way to retrieve result entities
Comment #74
fagook, talked to dereine on IRC regarding the access-issue. He recommends to leave it out or at least add it as option, so I leave it out for now. And finally I understand how views deals with access checks. :)
also dereine helped to clear the array/object issue, see this commit. Thus, I re-worked the patch again to remove the array checks and committed it. That means, folks need to use the latest search-api for this to work with it.
Attached is the finally committed version.
Comment #76
Anonymous (not verified) commentedHi,
I have a custom entity type and setting up a View for a Search API index of that entity type. I need to style the views-view.tpl.php. When I click on the 'theme: information' I get this error:
It seems that the patch #74 needs one more line in
entity_views_plugins()in entity.views.incTo solve the above error, I needed to add the "theme":
But this doesn't work right, I don't know how to do this right. Somehow it needs to point to a .tpl.php file for views.
Comment #77
Anonymous (not verified) commentedI ended up with the following function:
And copied entity.tpl.php to /views/entity-view-entity.tpl.php
It escapes me what is going on here -- this needs more work.
Comment #78
drunken monkeyFYI: Follow-up for the field handlers (or at least related stuff): #1266036: Add generic Views entity tables with fields and relationships.
Comment #79
fagoplease open separate issues for any troubles with the committed plugin.