retrieve cck field for form_alter - drupal

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'];

Related

Displaying field collections loop in Content Type TPL file for drupal 7

I have a content type "about" created in Drupal 7. I have a field collection named "field_usf_projects" which is set to unlimited and contains 2 fields, "usf_title" and "usf_description". Now I want to run a for loop which retrieves the field_usf_projects and then displays 2 fields namely ("usf_title" and "usf_description") inside a ul - li structure.
I have gone through many links but cannot find a working solution. Please help me with this.
Thanks in advance.
Here is my solution, on hook_node_view you can use the entity wrapper to get the fields
function mymodule_node_view($node, $view_mode, $langcode) {
// Check if the node is type 'about'.
if ($node->type != 'about') {
return;
}
// Get the contents of the node using entity wrapper.
$node_wrapper = entity_metadata_wrapper('node', $node);
// Get the contents of the field collection.
$values = $node_wrapper->field_usf_projects;
// Loop field_usf_projects.
foreach ($values as $item) {
// Print the values of the fields.
var_dump($item->usf_title->value());
var_dump($item->usf_description->value());
}
}
Instead of dumping, you can add the markup for your
A nicer thing to do would be use the hook_preprocess_node to add the markup straight into the $variables, and print them via template.
https://api.drupal.org/api/drupal/modules!node!node.module/function/template_preprocess_node/7
I can tell you how I'm handling this, event it's a bit dirty and I'm risking to be crucified, but it works for me. If you are inside node template you have $node object. Print it with print_r or similar way and just follow the structure of output to get to your data. It will probably be something like:
$node->field_usf_title['und']...
If you don't have that $node object find node id and load the node with
$node = node_load($nid);
first.
I was finally fed up with Field collection. I cannot get any data. I have used Multifield which is way too much better than Field collection. Please see it at https://www.drupal.org/project/multifield
Let me know what is better multi field or Field Collection.
Thanks!

How to extract the variables out from "add_shortcode" function in Wordpress?

In Wordpress (now in Template Fiile), lets say i have a custom Shortcode function, like:
In the PAGE:
[myshortcode id='1234']
Then, in the Template File:
function parseUserID_func( $atts ){
//parse the $atts here...
$userID = 1234; //now i get the userID is, 1234
}
add_shortcode( 'myshortcode', 'parseUserID_func' );
//now .. echo the value of $userID here? <----------
printout_UserDetails( $userID );
As you can see, how can i get the processed value of $userID variable out from the shortcode function please? (as in the last line)
The $userID is not touchable from the outside.
Actually what i'm trying to do is something like:
Pass an ID from the Page, using [myshortcode id="1234"]
Parse out the id from the shortcode's function. (parseUserID_func above)
When i get the id, i need to pass it to another custom function printout_UserDetails($id){} function (written in the Template File) .. to continue another processing works.
(Something like) then i will print out the USER DETAILS on the Page, from the printout_UserDetails() function.
This is why i need to pass a value from Shortcode, then parse it, then pass the id to my another custom function.
Any help please?
From what I'm understanding you're not trying to generate output with the shortcode, but to save data instead. I would try using custom fields to handle this.
If you need to output the "USER DETAILS" block within the post content (right where the shortcode is written), you should just merge the printout_userDetials logic into the actual shortcode handler function itself (in this case parseUserID_func).

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

Drupal 6 getting the node title from a submitted form

I am using form_alter to edit the submit function when editing content. In my custom function I wish to edit a custom message to the screen with the title name. I thought a way I could do this is something as follows
function mymodule_myfunction(&$form) {
drupal_set_message(t('Some text ' . $form['#node']->title));
}
The title is not being joined to the 'Some text'
I am calling my function by using the following line in my form_alter:
$form['#submit'][] = 'mymodule_myfunction';
All submit functions get two parameters passed to them: $form, which is the final form array after all of the adjustments for hook_form_alter and the like, and $form_state which among other values contains the submitted values, which have been cleaned and checked for ranges. (For instance, if you have three items in a select box, the data in $form_state['values'] already has made sure that the value for that input is one of the three legal values.)
Generally, you shouldn't use $form['#post'] - it's not part of the published way to get at values, and an update to the core to handle some problem with FAPI could conceivably break your code.
Try this:
function mymodule_myfunction($form, &$form_state) {
drupal_set_message(t('Some Message #title'),
array('#title' => $form_state['values']['title'])));
}
Note the corrected use of the t() function - the intent of that function is to allow other users to translate text, and so by using 'Some Message #title' the translator knows more about what is going on. Additionally you get the advantage that text fed through the t function in this way also is fed through check_plain(), which prevents someone from doing something malicious with the input.
DKinzer recommended using dsm($form)to see the variables. The Node title is not populated. It can be found in the Post array. The following line allowed me to do what I wanted.
drupal_set_message(t('Some Text '.$form['#post']['title']));
Try changing the signature of your
function mymodule_myfunction(&$form) {
drupal_set_message(t('Some text ' . $form['#node']->title));
}
To:
function mymodule_myfunction($form, &$form_state) {
drupal_set_message(t('Some text ' . $form['#node']->title));
}
Also try installing the devel module so you can do things like
dsm($form);
dsm($form_state);
And see exactly what you are dealing with.
Also, if all you want to do is give a message when a new node of type 'X' is created a better way is to use hook_nodeapi;
It could look something like this;
function modulename_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($op == 'insert' && $node->type == 'my node type') {
drupal_set_message($node-title . ' is cool.');
}
}

Form submit handlers with additional arguments

For some requirement I need to pass additional information to form submit handler. In form api, while defining custom submit handler as
$additional_args = array();
$form['#submit'][] = 'my_submit_handler'
I expect to submit handler as
function my_submit_handler($form, &$form_state, $additional_args){
The submit handler is called by the drupal fapi, so you can't do something like that. Instead what you can do, is to add what you need, either to the $form, or to the $form_state. The usual approaches is to:
Added a field to the form, type value to store the value. Don't do this if you have the value in the form definition.
$form['store'] = array(
'#type' => 'value',
'#value' => $value
);
This will be available in $form_state['values']['store'].
Add the value to $form_state['storage'], done if you variables in your validation handle you want to transfer to your submit handler:
// Validation.
$form_state['storage']['value'] = $value;
...
// Submit
$value = $form_state['storage']['value'];
// Need to unset stored values when not used anymore.
unset($form_state['storage']['value']);
Drupal 7: Custom arguments are automatically propagated troug $form_state['build_info']['args']
This is said in http://api.drupal.org/api/drupal/includes!form.inc/function/drupal_get_form/7
Ex:
hook_form($form, &$form_state, $myAdditionnalArg) {...}
Then in
hook_form_submit($form, &$form_state) {
...
//$form_state['build_info']['args'] is an array containing at index 0 the value of argument $myAdditionnalArg
...
As reported in $form['#submit'] and $form['#validate'] and $form['#process'] no longer support custom parameters, the suggested way to pass parameters to a submission handler set as in the shown code is to use code similar to the following:
$form['#first_paramater'] = $value;
$form['#submit'][] = 'my_submit_handler';
The handler would retrieve the value as $form['#first_paramater'].
To notice that, instead of #first_paramater, the code can use a different string, but it must start with #.
Normally it's not necessary to set a submission handler like the code does, but there are some cases where it is necessary, like to alter a form created by another module, or to set a different submission handler for each of the submission buttons present in a form.
drupal_retrieve_form() saves the parameters passed to the form build handler in $form['#parameters'] which contains:
$form_id
$form_state
parameters passed to the form builder

Resources