Drupal 7: Populate node fields with values from $form - drupal

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.

Related

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 - Adding data in specific content type field

Currently I am building an installation profile for Drupal 7.
I make multiple fields and create instances for the fields at a specific content type.
Now I add a new node to the content type but I don't know how to add data into the fields I generated for this content type. This has to happen all in the installation profile so in code and no explanation for Drupal it self.
The node is generated like this:
$node = new stdClass();
$node->title = 'Test title';
node_save($node);
This is not the whole code of course but is just to give you an idea. Currently I add the type, status, uid, title, promote, created, timestamp, sticky, format, language, teaser, body and revision to the $node. Now I want to add my custom field data, anybody has an idea on how to do this?
Something like...
$node = new stdClass();
$node->type = 'article';
$node->title = 'Test title';
$node->language = LANGUAGE_NONE;
node_object_prepare($node);
// Other properties...
// Single cardinality
$node->field_some_text_field[$node->language][0]['value'] = 'Some value';
// Multiple cardinality
$node->field_some_entity_reference_field[$node->language][]['target_id'] = 123;
$node->field_some_entity_reference_field[$node->language][]['target_id'] = 456;
node_save($node);

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

Edit link in theme_table 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.

retrieve cck field for form_alter

I would like to retrieve a cck field "field_info" in my form alter to insert into another table when user is submitting. This doesn't seem to work.
//mymodule_form_alter() implemented
function mymodule_form_mysubmit{
$test = $form['field_info']['#value'];
//insert stuff code
}
Is there any mistake in the code?
You say module_form_alter() is implemented, but just to confirm, you need to have the following in it:
$form['#submit'][] = 'mymodule_form_mysubmit';
Assuming you do, to get the value of field_info, your submit function should look like:
function mymodule_form_mysubmit($form, &$form_state) {
$test = $form_state['values']['field_info'][0]['value'];
}
$form_state contains the current state of the form being submitted. CCK always assumes that there could be multiple values for a field, so it always puts things in an array (hence ['field_info'][0]).
I found the solution
$test = $form['field_info'][0]['#value'];

Resources