How to list a form's field-names - drupal

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

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 use setAttribute on a form collection in ZF2?

I haven't found any documentation for setting field attributes to for Zend Framework 2 form collection. I can set the value of a single input field like this:
$form->get('title')->setAttribute('value', $value);
What I can't figure out is how to set the values for a collection.
$form->get('sample_collection') returns a Zend\Form\Element\Collection Object
It seems like I need to go one layer deeper and select the specific field so that I can use the ->setAttribute on it.
Thank you in advance for your help in solving this.
I had some real trouble with this, the only way I could actually get access to a fieldset inside a collection was with the following. (If the collection has more then one fieldset you would have to add an if statement inside the foreach loop to get the fieldset you want.)
$array = array('keys'=>'values');
$collection = $form->get('name_of_collection');
foreach ($collection as $coll)
{
$fieldset = $coll;
}
$element = $fieldset->get('name_of_element');
$element->setValueOptions($array);
I expected the following to work, which does not. I am not sure if this is a bug in the Zend framework or if I am doing something wrong.
$collection = $form->get('name_of_collection');
$fieldset = $collection->get('name_of_fieldset');
$element = $fieldset->get('name_of_element');
If you just want to access a single element inside a fieldset NOT inside a collection. The following has worked fine for me.
$fieldset = $form->get('name_of_fieldset');
$element = $fieldset->get('name_of_element');
$element->setAttribute('id', 'name_of_element');
I hope this can be of help to someone.
Use form collection as an array:
$elements = $form->get('sample_collection');
foreach($elements as $element){
$element->setAttribute('value', $value);
}

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.

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

Theme CCK fieldset

I am attempting to use the CCK theme_fieldgroup_fieldset($elements) hook to convert the fieldset to a two column layout.
I thought that this shouldn't be to hard because the individual fields are in the $elements variable so all I have to do is iterate over them and print them individually. the problem is that I have no way to tell if they have been excluded from display on the "Display Fields" tab of the content type.
Does anyone have any ideas for me? Am I trying to do this the hard way or what am I missing?
Following is the solution that I came up with. The biggest problem is that it requires a database query for every field. These isn't the greatest, but it works so what can you say?
function _brioratheme_include_cck($field) {
$query = "SELECT display_settings AS ds FROM {content_node_field_instance} WHERE field_name = '%s' LIMIT 1";
$result = db_query($query, $field);
if ($result) {
$row = db_fetch_object($result);
$display_settings = unserialize($row->ds);
return !$display_settings['full']['exclude'];
}
}

Resources