Edit link in theme_table drupal - drupal

I have created a table in drupal to display the records. How can i add the edit link to each record so that it goes to an input form corresponding to the id for that record
function display($nid){
$query = db_query("select * from {contactus}");
$data = array();
$i = 0;
while($row = db_fetch_array($query)){
$data[$i] = $row;
$i++;
}
$output = theme_table(array('id','email','comment'),$data);
return $output;
}

You must implement the full CRUD-range, Create Read Update Delete. Right now you only have an index. For Drupal7 there is a good example in dbtng (from the examples)
For Drupal 6 I am not aware of such an example.
Basically the pattern is:
make hook_menu-items with callbacks, one for the index, Read, Update, Delete, Create.
The Read item simply shows the item (item/%id)
The Update shows a form to update the item (item/%id/edit). Form is pre-filled. See FormApi in Drupal for more information on forms.
The Delete shows a confirm_form() with a callback to delete the entry from the database.
The Create shows a form to create a new item. Form is empty.
But to answer your exact quesion, in Drupal you create a link with l(). l('foo', 'item/bar') Will create a foo.

Related

Drupal 7: Populate node fields with values from $form

I would like to know if it's possible to populate a node fields with the values from form_state or something like that.
Basically what I do is to show a register form merged with a form from a Content Type. I do that by using field_attach_form(). Now on submit I create a node using:
$node = new stdClass();
$node->type = 'company';
$node->uid = 1;
node_object_prepare($node);
and now I would like to get all values from form_state and put them into the node.
Many thanks!
Hook your form, add new submit handler and place your code there.

Populate username in the select list ( CCK )

How do populate username's in the select list. i added below code in the Allowed list textbox. But select list showing the PHP code instead of usernames.
global $user;
$sql = "SELECT name FROM users";
$res = db_query($sql);
while($row = db_fetch_array($res)){
$rows[] = $row['name'];
}
return $rows;
Note: Application running in D7.
As well i just followed below steps also.
Enable Core module PHP Filter.
Set/Check user permissions: admin/people/permissions#module-php.
Set/Check Text Formats: admin/config/content/formats.
Enable CCK Module. This step is often overlooked. However without CCK Module enabled you wont be able to enter (and thus execute) PHP-code in the Allowed-Values-List window
You may want to try Entity Reference - It works with CCK and allows you to reference Users: https://drupal.org/project/entityreference

How to enable/disable revision in drupal 7

What I am trying to do is to enable/disable revision according to the selected taxonomy term in the content type that I have created i.e. when a user add the content the user can select the taxonomy term field(may be a select field) according to the selected option I want to enable/disable the revision. How can I do this?
Turn off the create new revision setting for the content type.
Then in hook_form_alter add a new submission handler before the main one:
function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {
//drupal_set_message("Form ID is : " . $form_id);
switch($form_id) {
case 'CONTENT_TYPE_node_form':
//dpm($form);
$form['actions']['submit']['#submit'][] = 'revision_control_node_form_submit';
$form['actions']['submit']['#submit'] = array_reverse($form['actions']['submit']['#submit']); // reverse array to put our submit handler first
break;
}
}
Then in the new submit handler check if the taxonomy term has the correct value to save a new revision. I've not tried this next bit but according to this page putting
$node->revision = 1;
before node save will create a new revision.
node_save is called in node_form_submit and the node object is built in node_form_submit_build_node.
Looking at the other attributes like vid that belong to $form_state I would say an good educated guess would be to put $form_state->revision = 1; and see if that comes out as a property of the node after node_form_submit_build_node.
So you final new submit handler will look something like:
function revision_control_node_form_submit($form, &$form_state) {
if($form_state['values']['your_taxonomy_field'] == 'your_value') {
$form_state->revision = 1;
}
}
Now I've not actually tried any of this but even if it doesn't work I'm sure you will be on the right track... Good luck!

Drupal 7 node_save not saving computed fields during cron

I have a Drupal content type which contains a number of computed fields. Some (but not all) items are being added to this content type via a cron-triggered RSS feed importer. I'm trying to trigger computed field generation for new items in hook_cron. The following code grabs all items that haven't been tagged as 'submitted', loads and re-saves the node, and then marks the node as 'submitted'.
$query = db_select('node', 'n');
$query->fields('n', array('nid'));
$table_alias = $query->join('field_data_field_submitted', 'r', 'n.nid = r.entity_id AND r.field_submitted_value = 0');
$result = $query->execute();
foreach ($result as $record){
$q = $record->nid;
$n = node_load($q);
node_save($n);
$query = db_update('field_data_field_submitted')
->fields(array('field_submitted_value' => 1))
->condition('entity_id', $q)
->execute();
}
This code works the way I expect it to if I call it from a module-generated page (created using hook_menu with a page callback function). Nodes are resubmitted, and the computed field data is generated. When I put this code in my hook_cron function, the query works, it loops through the records and updates the 'submitted' value, but the computed fields are not computed. I'm confused as to why this would not get triggered in cron. Any help?
Doh! Finally realized that this was completely my own doing. Due to the nature of this content type, where we allow anonymous users to create new content, but explicitly do not trigger the computed fields when they create the content (long story, but short form is that authenticated users then verify & enhance this content, which is where the computed fields come in). So, as I was setting up the initial content, I disabled the computed fields for anonymous users (if $user->uid > 0), and completely forgot about that. Once I tweaked that logic to allow computed fields to be processed on import (triggering it with a field that has a value for the imported content, but not for other content), the problem was solved.
The cron run has access to the full bootstrap so there's no logical reason why your code would produce different results in that context.
That said, you're only updating the field_data_field_submitted table when you also need to update the field_revision_field_submitted table, so that might somehow account for the discrepancy.
Drupal provides an API for the field system so that these sorts of problems can be avoided completely. The same code you've used, rewritten the 'Drupal' way, would be:
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
->fieldCondition('field_submitted', 'value', 0);
$results = $query->execute();
if (!empty($results['node'])) {
$nodes = node_load_multiple(array_keys($results['node']));
foreach ($nodes as $node) {
$node->field_submitted[$node->language][0]['value'] = 1;
node_save($node);
}
}
I can't think of a good reason why the above code would fail on cron either so it might be worth giving it a whirl.

How to list a form's field-names

I have a form, and want to generate a list of the form's field-names. Here is how I currently do it:
$fieldnames = array();
foreach ($form as $key=>$val){
if (substr($key, 0, 6) === 'field_'){
$fieldnames[] = $key;
}
}
Is there a better way to do this?
UPDATE:
Just to clarify ... I am wondering whether there is a less "kludgey" way of doing this. For example, does the content module provide an api function that loops through fields. (I couldn't find one.)
the field that you added by cck...or from UI field system are begin with "field_"
and this fields are usually in the nodes...so if you are talkin about nodes form
and fields that added by cck....you are in the correct way... but if this fields are added programmatically....so you are in the wrong way
sorry im not 100% sure but i don't think you can get all the fields that added programatically..but if you added this fields from cck or from '/admin/content/node-type/stores/fields' where {stores} is your content type that you are working with then you can get this fields name from {content_node_field_instance} table as the following
$result_handle = db_query("select field_name from {content_node_field_instance} where
`type_name` = '%s'","yourContentTypeName") ;
while($result_object = db_fetch_object($result_handle)){
$fields[] = result_object->field_name ;
}
now you have the array $fields which hav all the fields of your content type...i hope that will help you

Resources