Preferences and nice admin interface in Drupal - php

I created a custom Drupal module and now want to save some preferences for it. Those are essentially 3 arrays of strings that should be saved and also easily edited by a (administrative) user.
What would be the best way to do that in Drupal? I've read about variable_set and variable_get, are they appropriate to store module-specific data like this?
Is there some way to easily create an admin form to edit those variables, or do I have to write that from scratch?

If you want to do it fast, use variable_get/set and system_settings_form().

Need more space than a comment. #yorirou has the correct answer. However, to store/retrieve an array of strings each entered by different widgets, you have an easy path:
Wrap everything in a fieldset with the #tree property. All fields within that fieldset will be folded into an array structured based on the fieldset name for the variable name, and the individual field names for the key value. Array serialization is automatically handled on variable_get/set.
$form['custom_settings'] = array(
'#title' => t('My settings'),
'#type' => 'fieldset',
'#tree' => TRUE,
);
$form['custom_settings']['first_setting'] = array(
'#type' => 'textfield',
// etcetera
);
$form['custom_settings']['second_setting'] = array(
'#type' => 'textfield',
// etcetera
);
On output of the saved array, you would get something like this:
custom_settings = Array(
'first_setting' => 'some string',
'second_setting' => 'some other string',
)
You could get the same affect with a #validate handler, or using the #parents element in the forms arrays, but those would require extra work to wrangle the default value.

Related

How to add dynamically-created sub-forms as a collection in Symfony/Silex FormBuilder?

I'm trying to dynamically generate a form based on user-provided field definitions. This is fairly straightforward for the basic form:
$builder = $app['form.factory']->createBuilder('form', $my_data);
foreach ($my_user_provided_field_definitions as $field) {
$builder->add($field->handle, $field->type, $field->options);
}
$form = $builder->getForm();
But I also want to have sub-forms using the collection field type, and while it is so easy to dynamically generate the top-level form as I've done above, it does not seem possible to dynamically generate a sub-form for the collection field because all of the docs and sample code I've come across utilize either a single field like so:
$builder->add('my field', 'collection', array(
'type' => 'text',
'allow_add' => true,
));
...or a custom "Type" object, like so:
$builer->add('my field', 'collection', array(
'type' => new TagType(),
'allow_add' => true,
));
The TagType class in the above example must be defined in code (see http://symfony.com/doc/master/cookbook/form/form_collections.html )... but my problem is that I cannot have the class defined in code because these sub-forms are dynamically generated based on user data -- so I do not know what fields the sub-form will contain until run-time!
Is there a way to dynamically generate the sub-forms that can then be passed in to the top-level form, or is this a use case that is not handled by the Symfony FormBuilder?

Form in Symfony 2 whose fields change in number and purpose according to external data

I am building an application using Symfony 2, and one of the pages contains a large form, which has a number of fixed fields but also a number of fields whose presence and number depends entirely on related data taken from the DB. More specifically, the form poses a number of questions, and the questions themselves come from the DB, having different text and IDs. The answers need to be stored in the DB, linked to the questions.
I've read up on how Symfony handles forms (Basic Symfony Forms), including the relationship between a Form object and the data object backing it, creating Fields from the Form object, and validating input according to rules applied to the Form object.
Since I don't want a fixed Form object I looked at how you're meant to dynamically modify Forms (Dynamic Symfony Forms), but the examples there seem to be about choosing whether to show certain pre-defined fields, or controlling the options in the fields, not completely generating new fields from scratch, which is what I want.
I had a look at the Symfony page on Data Transformers, and it seemed relevant, but I couldn't quite get my head around how it would help me. They seem more about mapping a Form object to an Entity than mapping complex form submissions to a Form object.
I did see this SO answer where the object backing the Form object is just an array, which also seemed relevant: I thought that maybe the answer was to generate some anonymous object with the dynamic fields in, and then make a form from that, but I don't see how I'd set up Symfony validation on it, and my form is not completely different each time, so doesn't need to be entirely anonymous.
What is the best way to achieve this using Symfony 2, ideally without giving up such framework benefits as easy validation etc?
EDIT I hadn't heard of Symfony Form Collections before, and it looks like this might do what I want - each Question in my case would map to an individual subform object. I'll update again if I try it out successfully.
Your question is pretty broad but this might help focus.
I have what I call a DynamicFormType which can add elements to a form based on a configuration items array.
class DynamicFormType extends AbstractType
{
protected $name;
protected $items;
public function getName() { return $this->name; }
public function __construct($name, $items)
{
$this->name = $name;
$this->items = $items;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
foreach($this->items as $name => $item)
{
switch($item['type'])
{
case 'radio':
$attr = array('class' => 'radio-medium');
$builder->add($name,'choice',array(
'label' => $item['label'],
'required' => false,
'attr' => $attr,
'empty_value' => false,
'expanded' => true,
'multiple' => false,
'choices' => $item['choices'],
));
break;
case 'select':
$attr = array();
$builder->add($name,'choice',array(
'label' => $item['label'],
'required' => false,
'attr' => $attr,
'empty_value' => false,
'expanded' => false,
'multiple' => false,
'choices' => $item['choices'],
));
break;
case 'text':
$attr = array();
if (isset($item['size'])) $attr['size'] = $item['size'];
$builder->add($name,'text',array(
'label' => $item['label'],
'required' => false,
'attr' => $attr,
));
break;
case 'textarea':
$attr = array();
if (isset($item['rows'])) $attr['rows'] = $item['rows'];
if (isset($item['cols'])) $attr['cols'] = $item['cols'];
$builder->add($name,'textarea',array(
'label' => $item['label'],
'required' => false,
'attr' => $attr,
));
break;
case 'collection':
$dynamicType = new DynamicFormType($name,$item['items']);
$builder->add($name,$dynamicType,array(
'label' => false,
'required' => false,
));
break;
}
}
return; if ($options);
}
}
I trust you can see how the items array is used to create form elements. In my application I use this with:
$basicType = new DynamicFormType('basic',$project->getPlan());
$builder->add('basic',$basicType, array('label' => false));
$project->getPlan() returns the items array which describes the various elements I want to add.
Hopefully, you can start with something like this and figure out what elements need to be added from your data structure. I didn't need validation here but it should be easy enough to add constraint information to you elements once you get things working.
OK, in the end what I wanted wasn't a truly dynamic form (but thanks to #Cerad for that code), but one using Collections where the number of certain child form items was variable.
So I essentially followed the Symfony Docs about Collections

How do you arrange PHP objects into arrays for display

I'm a PHP developer who typically uses arrays in lieu of objects, and I'd like to know if there are any best practices for object manipulation based on key values.
Take the following multi-dimensional array of events, typical of something you'd pull directly out of the database.
$events = array(
0=>array(
'eventid'=>1,
'title'=>'First Event',
'date'=>'20131010'
),
1=>array(
'eventid'=>2,
'title'=>'Second Event',
'date'=>'20131022'
),
2=>array(
'eventid'=>3,
'title'=>'Third Event',
'date'=>'20131010'
),
),
For display purposes in my template, I want to make this a multi-dimensional array based on the date first. This is fairly easy transformation that I use a helper function for.
assoc($events,'date','eventid');
Resulting in:
$events = array(
20131010=>array(
1 => array(
'eventid'=>1,
'title'=>'First Event',
'date'=>'20131010'
),
3 => array(
'eventid'=>3,
'title'=>'Third Event',
'date'=>'20131010'
),
),
20131022=>array(
2 => array(
'eventid'=>1,
'title'=>'First Event',
'date'=>'20121010'
),
),
),
In this way, I can easily run through the array on the template side for each date, create a header and for that date, and then display events for that day underneath.
I could go ahead and try to make a similar assoc() function for Objects, but it strikes me this should be a fairly common thing and there may be a standard way of doing it. If you had an object instead of an array for $events, how would you go about formatting it the second way I've described, or would you?
One additional piece of information: I am using CodeIgniter, in case it has some helper functions that I'm not aware of that could be useful.
In your assoc function you could do an is_object check and cast to an array if it is an object.
function your_assoc_func($events){
if(is_object($events)){
$events = (array)$events
}
// If the object is more complex cast any containing objects you may run across
foreach($events as $event){
if(is_object($event)){
$event = (array)$event
}
}
}

Access custom error messages for InArray validator when using Zend_Form_Element_Select

I'm using Zend Framework 1.62 (becuase we are deploying the finished product to a Red Hat instance, which doesn't have a hgih enough PHP version to support > ZF1.62).
When creating a Form using Zend Form, I add a select element, add some multi options.
I use the Zend Form as an in-object validation layer, passing an objects values through it and using the isValid method to determine if all the values fall within normal parameters.
Zend_Form_Element_Select works exactly as expected, showing invalid if any other value is input other than one of the multi select options I added.
The problem comes when I want to display the form at some point, I cant edit the error message created by the pre registered 'InArray' validator added automatically by ZF. I know I can disable this behaviour, but it works great apart from the error messages. I've tryed the following:
$this->getElement('country')->getValidator('InArray')->setMessage('The country is not in the approved lists of countries');
// Doesn't work at all.
$this->getElement('country')->setErrorMessage('The country is not in the approved lists of countries');
// Causes a conflict elswhere in the application and doesnt allow granular control of error messages.
Anyone have any ideas?
Ben
I usually set validators as per my example below:
$this->addElement('text', 'employee_email', array(
'filters' => array('StringTrim'),
'validators' => array(
array('Db_NoRecordExists', false, array(
'employees',
'employee_email',
'messages' => array(Zend_Validate_Db_Abstract::ERROR_RECORD_FOUND => 'A user with email address %value% already exists')
))
),
'label' => 'Email address',
'required' => true,
));
The validators array in the element options can take a validator name (string) or an array.
When an array is passed, the first value is the name, and the third is an array of options for the validator. You can specify a key messages with custom messages for your element in this array of options.
If your using Zend_Form_Element_Select (or any of the Multi subclasses), on validation the InArray validator will only be automatically added if there is not one present.
You can set a validator as so:
$options = array(...);
$this->addElement('select', 'agree', array(
'validators' => array(
array('InArray', true, array(
'messages' => array(
Zend_Validate_InArray::NOT_IN_ARRAY => 'Custom message here',
),
'haystack' => array_keys($options),
)),
'multiOptions' => $options,
));
and then your validator will be used instead of the automatically attached one.
$el = $this->addElement($name, $label, $require, 'select');
$validator = new Zend_Validate_InArray(array_keys(AZend_Geo::getStatesList()));
$validator->setMessage('Invalid US State.');
$el
->setMultiOptions(AZend_Geo::getStatesList())
->setRegisterInArrayValidator(false)
->addValidator($validator)
->addFilter(new Zend_Filter_StringToUpper())
->addFilter(new T3LeadBody_Filter_SetNull())
->setDescription('US State. 2 char.');

Zend Form Edit and Zend_Validate_Db_NoRecordExists

I am slowly building up my Zend skills by building some utility websites for my own use. I have been using Zend Forms and Form validation and so far have been happy that I have been understanding the Zend way of doing things. However I am a bit confused with how to use Zend_Validate_Db_NoRecordExists() in the context of an edit form and a field that maps to database column that has to be unique.
For example using this simple table
TABLE Test
(
ID INT AUTO_INCREMENT,
Data INT UNIQUE
);
If I was simply adding a new row to the Table Test, I could add a validator to the Zend Form element for the Data field as such:
$data = new Zend_Form_Element_Text('Data');
$data->addValidator( new Zend_Validate_Db_NoRecordExists('Test', 'Data') )
At form validation this validator will check that the contents of the Data element does not already exist in the table. Thus the insert into Test can go ahead without violating the Data fields UNIQUE qualifier.
However the situation is different when editing an existing row of the Test table. In that case the validator needs to check that the element value meets one of two mutually exclusive conditions conditions:
The user has changed the element value, and the new value does not currently
exist in the table.
The user has Not changed the element value. Thus the value does currently exist in the table (and this is OK).
The Zend Validation Docs talk about adding a parameter to the NoRecordExists() validator for the purpose of excluding records from the validation process. The idea being to "validate the table looking for any matching rows, but ignore any hits where the a field has this specific value". Such a use case is what is needed for the validating the element when editing a table. The pseudo code to do this in 1.9 is like so (actually I got this from the 1.9 source code - I think the current docs may be wrong):
$data = new Zend_Form_Element_Text('Data');
$data->addValidator( new Zend_Validate_Db_NoRecordExists('Test', 'Data',
array ('field'=>'Data', 'Value'=> $Value) );
The problem is that the value that is to be excluded ($Value) is bound to the validator at the time it is instantiated (also when the form is instantiated). But when the form is editing a record, that value needs to be bound to the contents of the $data field when the form was initially populated with data - IE the Data value initially read from the Test table row. But in typical Zend patterns a form is instantiated and populated in two separate steps which precludes binding the exclude value to the desired element value.
The following Zend psuedo code marks where I would like the binding of $Value to the NoRecordExists() validator to occur (and note that this is a common Zend controller pattern):
$form = new Form()
if (is Post) {
$formData = GetPostData()
if ($form->isValid($formData)) {
Update Table with $formData
Redirect out of here
} else {
$form->populate($formData)
}
} else {
$RowData = Get Data from Table
$form->populate($RowData) <=== This is where I want ('value' => $Value) bound
}
I could sub-class Zend_Form and override the populate() method to do a one-shot insertion of the NoRecordExists() validator on initial form population, but that seems like a huge hack to me. So I wanted to know what other people think and is there some pattern already written down that solves this problem?
Edit 2009-02-04
I've been thinking that the only decent solution to this problem is to write a custom validator and forget about the Zend version. My form has the record ID as hidden field, so that given the table and column names I could craft some SQL to test for uniqueness and exclude the row with an ID of such an such. Of course this started me thinking about how I would be tying the form to the dB layer that the Model is supposed to hide!
This is how it's done:
I your FORM, you add this validator (for example email field):
$email->addValidator('Db_NoRecordExists', true, array('table' => 'user', 'field' => 'email'));
Don't add custom error message for this since after that it didn't work for me, e.g.:
$email->getValidator('Db_NoRecordExists')->setMessage('This email is already registered.');
In your Controller add this:
/* Don't check for Db_NoRecordExists if editing the same field */
$form->getElement('email')
->addValidator('Db_NoRecordExists',
false,
array('table' => 'user',
'field' => 'email',
'exclude' => array ('field' => 'id', 'value' => $this->request->get('id'))));
And after this you do verifications, e.g.:
if ($this->getRequest()->isPost())
{
if($form->isValid($this->getRequest()->getPost()))
{
....
That's it!
This will also work :
$this->addElement('text', 'email', array(
'label' => 'Your email address:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'EmailAddress',
array('Db_NoRecordExists', true, array(
'table' => 'guestbook',
'field' => 'email',
'messages' => array(
'recordFound' => 'Email already taken'
)
)
)
)
));
After reviewing the overwhelming response I've decided that I'm going with a custom validator
Look at this one:
Answer raised by me and well-solved by Dickie
private $_id;
public function setId($id=null)
{
$this->_id=$id;
}
public function init()
{
.....
if(isset($this->_id)){
$email->addValidator('Db_NoRecordExists', false, array('table' => 'user', 'field' => 'email','exclude' => array ('field' => 'id', 'value' => $this->_id) ));
$email->getValidator('Db_NoRecordExists')->setMessage('This email is already registered.');
}
Now u can use:
$form = new Form_Test(array('id'=>$id));
You could just call $form->getElement('input')->removeValidator('Zend_Validator_Db_NoRecordExists'); instead of supplying the exclusion.
I have just tried this example for email address uniqueness and it works perfectly with below stuffs :
1] In my form:
// Add an email element
$this->addElement('text', 'email', array(
'label' => 'Email :',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'EmailAddress',
)
));
Here's something special that I needed to add for unique email address to work:
$email = new Zend_Form_Element_Text('email');
$email->addValidator('Db_NoRecordExists', true, array('table' => 'guestbook', 'field' => 'email'));
2] In my controller:
$form->getElement('email')
->addValidator('Db_NoRecordExists',
false,
array('table' => 'guestbook',
'field' => 'email',
'exclude' => array ('field' => 'id', 'value' => $request->get('id'))));
if ($this->getRequest()->isPost()) {
if ($form->isValid($request->getPost())) {
Hope it helps you people !
Thanks

Categories