Field api, defining new fields and getting/setting values - drupal

I'm able to create a field, using
$form['title'] = array( '#type' => 'text', '#value' => 'Drupal');
1) But I want to be able to get/set the value from the database. How would I do it?
2 )P.S. what's the point of having a '#" in front of type and value ?

First of all I don't know what Drupal version are you using.
Secondly try to get some more info from the my answer:
1) Use Database API in order to get values from database.
2) Read more about Form API.
Hope this helps.

Related

doctrine join column joined on other entity

I have 3 tables:
Post -> Type -> Category
I need to get the category on the Post entity passing per Type to use this on a filter form
This is possible?
like a join and subjoin
If I understand correctly, you want to be able to filter Post's by Category.
Like with any other field you wish to filter by, you have to add a Form to the filter's FormBuilder. The problem in this case is that the Entity bound to the form doesn't have the property category. It's its property type who does.
Thus, you need to tell the Form how to access the right property. This is achieved by using the property_path option. Here's the documentation for it.
You would do something like this in your filter's Type:
$builder
->add('category', 'entity', array(
'label' => 'Category',
'data_class' => 'Category',
'property_path' => 'type.category',
))
;
The property_path option is very powerful. It will accept any path that the PropertyAccess component does. Read its documentation here.
Multiple joins are possible in doctrine. Please, read this section in doctrine documentation.

How to get custom variable info from Google Analytics via API

I have data stored in the Second Dimension Google Analytics Events. The field is called Custom Variable (Value 01).
How can I use the GA api to get the values from Custom Variable (Value 01).
I'm using the GA explorer http://ga-dev-tools.appspot.com/explorer/ and can't figure out how to get the Custom Variable.
I see these options:
ga:dimensionXX
ga:customVarNameXX
ga:customVarValueXX
I tried replacing the xx with 01 but I had no luck. Anyone know how I can get the info for GA via API?
Thanks to #Blexy. This was my code at the end:
$params = array(
'metrics' => 'ga:visitors',
'dimensions' => 'ga:customVarValue1,ga:eventLabel,ga:deviceCategory,ga:operatingSystem',
'max-results' => 1000,
'start-date' => $start_date,
'end-date' => $end_date,
);
Try removing the 0.
For example, I have a query like this:
ga:customVarName4=~Previous-Purchases;ga:customVarValue4==1
This gets my custom variable name that matches regex Previous-Purchases AND the custom variable value is equal to 1.

Symfony2 Form entity

I have a problem, I want to create a simple search form with a filter assembly. These filters are attributes that belong to attribute groups.
group 1
[] Attribute 1
[] Attribute 2
[] Attribute 3
group 2
[] Attribute 1
[] Attribute 2
[] Attribute 3
But the problem is that I can not do (graphic aspect)
$builder->add('attribut', 'entity', array(
'class' => 'RestoFrontBundle:Attribut',
'group_by' => 'groupeAttribut.id',
'expanded' => true,
'multiple' => true,
'query_builder' => function(AttributRepository $er) {
return $er->createQueryBuilder('a')
->join("a.groupeAttribut", 'g')
->where("a.statut = 1");
}
))
->getForm();
Also I can not manage the game if the checkbox has been checked.
You note that the graphic aspect is the hard part. That is due to the way checkboxes are used in HTML. Unlike select inputs there is no notion of an optgroup. The closest analog for checkboxes would be a fieldset with a legend.
You may want explore using a Choice Field type rather than an entity type. Provide your choices via some provider function within which you format the options array(s) whether or not you retrieved them from a database. ( For the record, that's exactly how I populate the select at http://stayattache.com which has multiple places to retrieve options from. ) You may even want to explore creating your own form field type and template to format the output. Checkout the documentation on creating your own field type.
I hope that helps a bit. There are probably plenty of other ways to approach this as well.

Drupal form validation functions

Is there anyway say Drupal to validate form elements like email fields, passwords, numeric fields validate automatically lets say bind a system validator
$form['email] = array(
'#title' => t('Email'),
'#type' => 'textfield',
'#validate_as' => array('email', ...),
...
);
To validate a numeric field in Drupal use:
'#element_validate' => array('element_validate_number')
No need to create a custom validation function.
http://api.drupal.org/api/drupal/includes%21form.inc/function/element_validate_number/7
Rimian is both correct and wrong.
The good thing as Rimian points out, is that you can attach any validation function to your form fields using the #element_validate.
However I'm not aware of a set of form api validation functions you can call to test most common things like, if the value is:
a int
a positive int
a valid date (such a function exists in the date module though)
a email-address (you can use valid_email_address to check the email, but you need a function to raise validation error)
So while you can do this, it's a bit more work than you were hoping for, as you will need to create these validation functions yourself. But once you have done this, you can reuse them with #element_validate.
The use of #element_validate is mostly centered around complex validation fx date validation, location validation and such, as it requires some work to create these validation functions. Most of the time, you don't need to validate that many numbers etc (which you quite easily could do within a normal validation function using a loop). So I'm not sure how much this will be of help to you, but it's definitely a possibility.
The Form API Validation module does exactly what you request: http://drupal.org/project/fapi_validation
For client-side validation there is also http://drupal.org/project/clientside_validation (which can use rules provided by Form API Validation).
Yep!
Though I have not experimented with it much.
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#element_validate
$form = array(
'#type' => 'fieldset',
'#title' => t('Input format'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => $weight,
'#element_validate' => array('filter_form_validate'),
);

Drupal Form API: Create Form Elements based on the database values (Dynamic Form Creation)

I am creating a form where i have to populate "X" form elements (text-fields to be specific) based on values in the database (X number of elements, properties of each element etc). Is there a way to do this using the Drupal Form API or should i start from somewhere else.
I tried using a for() loop in the form generating function but this doesn't work. Please post you suggestions and any related links.
sure you can, just put the #default_value with the value coming from the DB.
$form['text'] = array(
'#type' => 'textfield',
'#default_value' => $db->val,
);
EDIT
say you have a questionnaire object ($questionnaire) that contains a param named "question_list" where you have stored all your questions; what you can do is run a foreach on the list like:
$form['questionnaire'] = array('#type'=>'fieldset','#title'=>$questionnaire->title,);
foreach($questionnaire->question_list as $id=>$question_obj){
$form['questionnaire']['question_'.$id]=array(
'#type'=>'textfield',
'#title'=>$question->question,
'#default_value'=>$form_state['values']['question_'.$id],
);
}
At the end you will have an associative array where each question is identified by 'question_'.$id
I hope this is what you're looking for. :)

Resources