I created add and view function on my module already. but I have problem to do edit and delete function.
here is my add and view function
//submit function
function schedule_form_submit($form, &$form_state) {
$subm = db_insert('schedule')
->fields(array(
'day' => $form_state['values']['day'],
'time' => $form_state['values']['time'],
'content' => $form_state['values']['content'],
))
->execute();
drupal_set_message(t('Schedule added'));
}
//view function
function schedule_submissions() {
$result = db_query("SELECT * FROM {schedule} ORDER BY day ASC, time ASC");
$header = array(t('Day'), t('Time'), t('Programs'));
$rows = array();
foreach($result as $rl) {
$rows[] = array (
$rl->day,
$rl->time,
$rl->content,
);
}
return theme('table', array('header' => $header, 'rows' => $rows));
}
but how do I add "Edit/Delete" links to each record on view page?
and do we have Edit and Delete function template?
Thanks
Comments
Why not use a content type
Why not use a content type and views to create the table view.
Here is a stripped down
Here is a stripped down example:
mymodule.install:
mymodule.module:
This gives the whole scope of having your own item with a database table, creating a page for it, 'view', 'edit' and 'delete' tabs, and tying the functionality into those tabs.
Commenting it is a little more than I want to do, so if you have any specific questions, please feel free to ask and I (or someone else) will be happy to answer.
thanks so much for your help,
thanks so much for your help, but I don't get how you pass item's id to edit and delete page. user should has right to choose which record they wanna delete/edit. so how you do this part?
thanks
There are three pieces of
There are three pieces of code that relate to this. I'll show you how an item is passed to the edit page, and delete page works in the same manner. First, look at my hook_menu() definition for the edit page (I've stripped it down to the relevant lines):
First is the menu path. You'll notice the %mycontent element of the path. This means that any value can be used in this element, and that value will be passed to the function mycontent_load() (add _load() to the part after the % sign). If I had written it like this:
Then the value in that element of the path would be directly passed to the edit page, without going through the function mycontent_load(). However, my form function is set up to require that an $item is passed to the form function, so I need to load that $item first. This is what happens in mycontent_load().
The next line is here:
I am passing two arguments to the callback function drupal_get_form(). The first argument is the name of the function that defines the form, in this case my_module_item_form(). The second argument is an integer, 1. When passing an integer, that index of the menu path is passed to the callback function. Indexes start at zero, so with the above path our indexes are:
We want to pass the value from the wildcard, which is why 1 is passed.
The thing to look at is mycontent_load():
You can see this function receives one argument, $rid. This is the value that was entered into the path. So if someone accesses the path
mycontent/3/edit
, then the value of $rid would be 3. This value is then used in the db query to load the $item with an $rid of 3.Note that if a value is not returned here, or if the returned value is false, then the form is never called and the user receives an 'access denied' page (or 'page not found'? I can't remember).
Now that we have generated an $item, it is passed to the form definition here:
All form definitions (in D7) receive $form, and $form_state. If you pass additional arguments to the function when calling drupal_get_form(), they are also passed to the form definition. Since I passed a loaded $item from the menu path, via mycontent_load(), the form definition receives $item. This is the item to be edited, and you can see I've used it to pass the default value (the value to be edited) to the form element here:
If there were more form elements, I would also populate them from the $item object.
This requirement for the $item is why for the item add page, I created an empty $item, then passed it to the form definition here:
You can see I've called drupal_get_form(), passing it the form definition name, mymodule_item_form(), and the empty $item I created.
So to review:
1) The item ID is passed in the URL. Ex:
mycontent/5/edit
2) This item ID is used to load an $item in mycontent_load()
3) The $item created in step 2 is passed to the form definition, as is the $item that will be edited.
help for delete a item from table
Hi Jaypan
Thanks so much for your help. I was out of town for while, so sorry not to reply you back in time. Thanks very much again
mycontent/%mycontent in this
mycontent/%mycontent in this menu what is %mycontent?
And mymodule_item_form($form, &$form_state, $item) How data is fetched in $item
I am totally confuse i need to make a edit function ad trying to understand your concept but it is complex for me.
Please respond to clear my doubt
_
one other method to consider is using entities which is sort of the best of both worlds-- you get a custom item that's not a node, but get lots of good stuff (like views) automatically.
Yeah, the example I've given
Yeah, the example I've given here is for something really stripped down, that needs no interaction with anything else on the site.
Now that said, the code I've given here is essentially the same skeleton I use for my own custom entities. This code can be extended to provide custom entities, by tying into the Entity API with hook_entity_info() and the other relevant hooks.
_
On an only tangentially related note, since you work with custom entities a lot, are they any faster in general? I'm working on upgrading a site from d6 to d7 and I'm trying to decide if it's worthwhile to redo my content types as custom entities. If I do them as entities, I can lose the field api fields and simply add them directly to the entity. I'm thinking that between shedding field api and the overhead typically incurred with node_load and node_save it could be a performance boost, but I have no data to back that up.
I would guess it would be a
I would guess it would be a very small savings since the node functions are basically wrappers around the entity functions.
I agree with nevets - the
I agree with nevets - the node API adds a fair bit of overhead with all the options associated with node entities. Unless you need everything that comes with nodes - authoring info, comments, revision etc, the entity api is somewhat overkill. So you will see small savings.
I work purely with my own custom entities now, even for comments. I create my own comment entity type, and attach it with my own API. The Drupal comment API is near impossible to tie to other entity types unfortunately.
This all said, I actually created my own module that lets me create entity types through the admin interface, same as one creates content types with nodes. I just added an extra layer, so first the entity type is created, then the bundle type. I've thought about putting it up on DO, but I just don't have the time to maintain it, and since my client sites are all built around it, if I bring in other maintainers, and it goes off in a direction I'm not prepared for, it could cause me a lot of troubles! So we just use it in-house.
Question about permissions
I have the something simmilar to what you posted Jaypan. In mine users input data into a form (picture a online manual for a specific machine). Now the data is stored and then it is displayed with a view/edit/delete tab(simmilar to yours) so everyone can see it. Next to login link i have My submissions link where you have in a table view everything you posted.
My problem is next i need to make it so that a user who created the input can edit and delete his input (Like edit own article, delete own article permission). I save a global $user to table so i have the uid of the user. Now i am using if($curent_user == $saved_user) {delete code}, same for edit.
Now the user who did not create the input can't view the edit form od the delete form but they see delete and edit in tab. You know they have View|Edit|Delete . They can see View but under Edit and delete it is just blank space. Any tips ???
You want to set up an access
You want to set up an access callback for this. In my original code, I had this:
I'm going to add an access callback, and change the access arguments:
Now we need to define the access callback:
The 'return' of this function will be either true or false, which will determine whether or not to render the tab.
Hi Jaypan,
Hi Jaypan,
Building entity types is what I'm looking into next, are you willing to share (or sell) this module that creates entity types from the admin with me so I can learn from it?
Thanks
I'd love to sell you mine,
I'd love to sell you mine, but you can get one for free on this site! Check out the Entity Construction Kit module.
Setup this code using Entities
Hi Jaypan,
I have seen your example code that gives a very clear way how to manage custom schema using custom module. I want to implement this example code by Entity API because I need to use this schema inside views also.
Can you please share any example or suggestion regarding this, it would help me in correct implementation.
Thanks
Pushpinder Rana #pushpinderdrupal
Acquia Certified Drupal Expert
Hello Pushpinder I'm sorry, I
Hello Pushpinder
I'm sorry, I don't have an example with how to tie this into entities or the Entity API module. I'll be writing one in a month or so, but at the time I have nothing.
But, if you look at a tutorial on how to create your own custom entities, it will likely require some forms, and you can use the ideas I've shown in this thread to tie in with your custom Entity.
Thanks Jaypan for quick reply
Thanks Jaypan!!!
I got one example from drupal forums and it is good for newcomers. entity_example
I will look into this and do correlate with your code.
Thanks
Pushpinder Rana #pushpinderdrupal
Acquia Certified Drupal Expert
Add, edit and delete records in an external database
Hi,
I have only started learning how to build Drupal custom modules a few weeks ago and am really a coding newbie. I would like to view, edit etc records in an external mssql database. I have built a module using hook_views_api to expose the tables to views and have successfully created views, but I am simply finding it impossible to edit records in the external database using example forms that i have come across.
Please help!!
Did you ever get around to doing this
Hi Jaypan, Did you ever get around to doing this (example with how to tie this into entities or the Entity API module), I'm enjoying learning Drupal and very interested in this if you ever did get around to an example
Many Thanks
Hmm, I don't actually
Hmm, I don't actually remember ever writing that comment, but I didn't ever write that tutorial, sorry!
No problem, You have already
No problem, You have already helped me massively so thankyou