Reviewed & tested by the community
Project:
Data
Version:
7.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
24 Aug 2014 at 13:21 UTC
Updated:
3 Nov 2020 at 08:06 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
betoaveigaComment #2
betoaveigaComment #3
joachim commentedChanging this to a feature request.
Comment #4
betoaveigaThanks Joachim, I just wanted to understand if I had understood everything about the data module.
I think that one option to create instances of entities (add records to a table) could be the Inline Entity Form widget inside a Entity Reference field (I haven't tested yet). However that doesn't solve the problem when someone wants to create these values previously (for selection in a dropdown).
Comment #5
joachim commentedWell there's already a form, just that it expects to have an existing Data entity to edit.
Most entity types use the same form for add and edit, and just wrap it up for the add page with something that creates a new entity (but doesn't save it) for the form to work with.
The same pattern should be doable here.
Comment #6
jcam88 commentedAnyone working on this? We have situations where we would like to avoid importing via feeds when we only have a few rows to add.
Comment #7
joachim commentedThe way things usually work is that if someone were working on this, they'd probably post here to say so. If you need this feature, perhaps you can work on the patch?
Comment #8
jcam88 commentedYes building a patch or custom module is what's next. Just asking if anyone has found a way to insert rows using another modules UI.
Comment #9
asanchez75 commentedI made a patch to provide a form to add items. See attachment.
Comment #10
jcam88 commentedGreat, we are working on a custom module which uses Sheetnode to manage the table. Using the Data Module view we can filter the table and with the Sheetnode display we are able to manage the data within the table. Edit,Delete,Modify,Add to Data table. I will report back if anyone finds this interesting.
Comment #11
joachim commentedThanks for the patch (remember to set to 'needs review'). A few unrelated changes though -- maybe you should try using a git local branch to keep separate the changes for different issues you're working on?
I'm not sure why we need a wrapper form here. Can't we just use data_entity_entity_edit_form as the form builder in hook_menu(), and pass it the $table param?
It's more readable to avoid a negative if() clause if you also have the else -- flip them round. Though you can also just say if($data_entity) ad rely on PHP type juggling and keep the two clauses in the same order.
Either way, a comment at the top of each block to explain which scenario we're in (editing vs add) would be good.
Please avoid whitespace changes!
I don't understand this change.
Why is this being commented out?
Don't really like using form_id to change logic.
I forget, are we depending on Entity API? That adds an $entity->is_new property to use for this sort of thing.
Or you can check for the presence of an entity ID to see if this is an insert or an update.
Not sure what this change is about.
Comment #12
asanchez75 commentedI have modified the code according your suggestions.
The last part (7) adds a link for "adding items". See attachment.
Comment #13
asanchez75 commentedNeeds review
Comment #14
joachim commentedThat's looking much better thanks!
I'm still not quite sure this is the best way to be handling both cases in the form.
Still got this unrelated change, and a whitespace change too.
I suspect that leaving the entity as NULL will cause problems with things like field_attach_form().
It would be better, and more consistent with other entity type modules, to use entity_create() and create a new, blank entity. For that you need to define an entity create callback.
How will $pseudo_entity get its ID? field_attach_insert() needs it to save field data. Surely you need to be passing that to drupal_write_record(), so it then gets the ID populated.
Comment #15
imclean commentedThis would be a great feature for a lightweight database without the usual field overheads.
Would it be worth hooking into Entity API's CRUD UI? #920546: Entity CRUD generic UI
Related D6 Data issue: #524270: CRUD UI for DataTable data
Comment #16
imclean commentedEntity Admin UI Documentation
Comment #17
jamesrgaede commentedperhaps its just me, but I can't get the patch in #12 to apply correctly. It tells me that Hunks 1 and 3 fail, at 107 and 191 respectively.
Comment #18
imclean commentedI'm having a look at this and have refactored the original plan a bit, but am having problems with the Edit link in the view. Currently the base_field is the primary key when in this case it really should be the entity ID.
These are separate and can be different.
Comment #19
imclean commentedHere it is. There are a couple of changes to current behaviour. The entity ID and primary key(s) can be different and because we're dealing with entities here we should probably use the entity ID.
Our PK and Entity ID are different and it didn't work until we made these changes. I'm not sure what ramifications these have but it seems to be working well for us.
Future feature requests
Comment #20
imclean commentedComment #21
imclean commentedUse empty() to properly check for is_new.
Comment #22
qstraza commentedGuys,
Could you please explain how can I get the form which is seen on 3.png (#12)?
I have applied the patch and created a table via data table gui...
BR
Comment #23
imclean commentedGo to Content -> Data Tables and click on the table name.
There's also a handy content link in the text above the list of tables under Structure -> Data Tables.
Comment #24
qstraza commentedThanks, but still nothing... I am using Commerce Kickstarter, but I do not see how could this affect it.
I have applied the patch to data-7.x-1.x-dev.
Are there any other requirements, such as, does the new created table has to be marked as an entity?
Comment #25
theorichel commentedThe 'edit item' link was not present on my View so I added it, but alas it doesnt link to a form, only to the page with data tables.
Sorry, I think I understand what to do, but I still need the path and name where I should link to (and combine it with a replacement to identify the row).
Comment #26
theorichel commentedThis is a notice I get:
Notice: Undefined property: stdClass::$IDAccess in data_entity_views_handler_field_edit_link->render() (line 28 of /home/theorich/public_html/sites/all/modules/data/data_entity/views/data_entity_views_handler_field_edit_link.inc).As said , I have added the 'edit link'- field myself in the View, but I do not see that field on the Configure Views page (with all the handlers).
Comment #27
joachim commented> Notice: Undefined property: stdClass::$IDAccess in data_entity_views_handler_field_edit_link->render() (line 28 of /home/theorich/public_html/sites/all/modules/data/data_entity/views/data_entity_views_handler_field_edit_link.inc)
It would help if you pasted the code at that line. I don't see any mention of IDAccess in the patch, so as such I'm not sure how this is relevant here.
Comment #28
theorichel commentedPatch? I havent patched. IDAccess is the primary-key field of my adopted table. I know this thread is for a large part about a patch, but also about the 'edit item'- link in the View which is my problem.
Comment #29
joachim commentedAh, I've only just see that you changed the title of this issue back in December. I'm changing it back, as that changed the scope of this issue. This issue is about adding a form to create new entities. If you have problems with the edit link, that's not related to this and should not be discussed here.
Comment #30
theorichel commentedSorry. I wasnt aware that I changed the title of this thread. I have no idea how to do it, but apparently it is very easy. I'll start another thread.
Comment #31
ar-jan commentedThe patch in #21 applies cleanly on 1.0-alpha7 (but not on 1.x-dev).
Couple of notes:
- Re #22-24: if the table (and therefore the view) already existed, you have to revert the view so the "Add item" link is added to it. Not sure if this should be done in the patch, someone might have customized the view already?
- The table must be declared as entity. Then you have to also "Configure entity form" (newly added tab after declaring as entity type).
- The primary key of the table must be set to auto_increment (which you can't do from the UI directly, I think).
Thanks, it's working nicely!
Comment #32
joachim commentedThanks for the review! Sounds like this needs work.
Comment #33
imclean commentedThanks @ar-jan.
The link could potentially be added to an existing view with a views alter hook.
The primary key doesn't need to be auto_increment, the entity ID field does. These can be different fields.
The easiest way to get this working is to create a Serial field within the UI and set it as the entity ID field.
Comment #34
ar-jan commentedAh you're right of course, I had made the primary key the entity ID (and used integer because it uses less storage space than serial). Just wanted to document this for others.
Comment #35
imclean commented@ar-jan, I misread the existing view problem in #31. I'm not sure the module should force revert the view. Any changes made to the view outside the module would be overwritten.
The change is already in an alter hook, and from memory I didn't need to revert anything. Clear cache perhaps?
Comment #36
imclean commentedI've added an update hook to add the link to existing views. I haven't tested it yet.
Comment #37
imclean commentedComment #38
imclean commented2 minor style fixes.
Comment #39
BarisW commentedThanks, the patch works as expected.
Comment #40
alansaviolobo commented+1 works
Comment #41
alansaviolobo commentedCorrecting 1 line that was causing the entity to not save.
Comment #42
alansaviolobo commentedThere appears to be an issue with regards to saving "fields" attached to the custom table entity.
They do not get saved when creating a new record. however, on editing the same record and saving, they are saved.
Comment #43
alansaviolobo commentedupdated patch.
Comment #44
alansaviolobo commentedAnother patch for the remaining functionality - delete entity.
Comment #45
roderikArguably minor, but: data_entity_update_7001() in #43 (note #43 and #44 are separate patches) needs to end with a call to views_invalidate_cache(), to make the link actually show up.
I'll see if I can come back to this later and do a proper review, but for now this is a quick hit-and-run partial review while testing. (Otherwise I'll forget this. Current workload / number of patches I'm juggling doesn't allow for being more organized, sorry.)
Comment #46
roderikHmmm that may not be true. Reverting status.
The reason an "add" link was not showing up in the table (which is still empty, which I have just created an hour ago and done nothing with except turning "entity-ness" on in admin/structure/data/edit/TABLE/entity-type)... is that CTools/Views has saved the definition to the database. So we'd need to actively revert the view.
I'm not sure if it is the business of this module/update hook to automatically revert views. OH! this discussion has already been had in #35 and before...
Nope, doesn't work (for me). It will only show up if the view is reverted, or explicitly edited - because somehow the CTools/Views definition already ended up in the database.
I am also not sure if it is the business of the update hook to revert the view (ONLY if there is no difference between the database/overridden version and the code version before-addding-the-link).
Need to think it over. We might need a warning in a README / update hook text about reverting your views. However, that warning is not unique to this patch... the same issue is there if you enable the data_entity module after having created tables already: then the "edit" links don't show.
Comment #47
roderikPlease ignore my previous messages. (Default views vs. saved views vs. cached default views and other things were confusing me.)
The actual reason that an "add" link was not showing up on my view, was that the so-far-untested update function (which is a good idea, and I opened #3144433: Entity-fying a table should add edit/add/delete links to the view to expand on it)... wasn't working. This is fixed now.
I've properly reviewed the patch now, and it's fine except... maybe the edit link changes. I believe all Joachim's earlier concerns are addressed. What I've changed (in the interdiff) is:
...Plus, I backed out the changes to the edit link:
Re. #19:
We should likely do the reverse: make sure the base_field is equal to the entity ID.
For the rest:
I thought a little about the naming of "Add" vs "Create" and "Item" vs "Record", and I think it's fine as-is.
We still have the separate patch in #44, for the "delete" link. I didn't want to roll it into this one, because it's big enough. That patch is good, except it needs a similar field addition as we have in data_entity_update_7001(), and (if my changes get reviewed/approved/RTBC'ed) an extra "delete" permission.
Comment #48
roderikOh, also:
Comment #49
roderikOne more change: we should not use $entity->entity_type where we can avoid it, because it's not standard. (The data module 'invented' it with a reference to its absence being a WTF, but there's consensus in #1042822: Developers need an $entity->entity_type property that this is just the way it is.)
Also: the submit function's usage is now consistent with the validate function's usage.
Comment #50
roderikFYI: ...now... the work done in this issue is also in #2744843: Remove Entity ID field requirement.
Because that re-does much of the edit screen (in order to be able to edit non-entities), it didn't make sense to not include the code to make "Add item" work.
Comment #51
alansaviolobo commentedpatch appears to work for creating new entities