Altering the text after a checkbox using the Drupal Form Api - php

I got the following issue. A clients wants that the text after checkboxes are links to other pages and thus between ...
I have the following code:
$form['boxes_brands'] = array(
'#type'=>'checkboxes',
'#title'=>'<div id="title-container">Merken</div>',
'#options'=>$brandArr,
'#default_value'=>$_SESSION['filter_brands_cat'],
);
=> $brandArr is an array of brands.
I looked in the Form Api of Drupal but I did not find an option to do this. I could alter the values in $brandArr but of course that changes the value of the value attribuut of the input object too.
Using the prefix and suffix options won't do it either because I don't want the checkboxes in the tags too.
Is there a clean way to do this?
Thanks!

If you created the form with the UI, then you should be able to specify something like this in as the options and links would be rendered as links:
google|This is a link to google
yahoo|Yahoo
bing|Bing!
See example:
Otherwise, you should be able to modify the $brandArr accordingly to create links in the label. Doing this should NOT change the value of the attribute as it should be a $value->$label associative array. You just need to change the $label not the $value.

Related

How to assign multilingual values to multilingual form element in ojs modal?

I’m using a custom plugin for custom metadata field. When I try to edit the field in metadata modal, the field of the language other than the current active language is empty. I would like to modify the code so it shows values of all supported languages in their corresponding fields.
The edit function of the plugin contains the following code:
$additional=$metadatafieldDAO->getAdditionalFieldValue($articleId, ‘additional’);
$templateMgr->assign(‘additional’,$additional);
It is easy to modify the getAdditionalFieldValue function to return the values in all supported languages, but I don't know how to assign these values to the field and display them.
Following is the template for the additional metadata field:
{fbvFormSection label="plugins.generic.articleMetadata.additional" }
{fbvElement type="textarea" rich="extended" multilingual=true name="additional" id="additional" value=$additional }
{/fbvFormSection}
I just found the answer after some search and experimenting.
So, a multilingual field can be updated by simply passing an associative array to the function $templateMgr->assign().
The array takes the form $array[$locale]=$value_for_that_locale. For example:
$additional[‘en_US’]=“additional metadata”;
$additional[‘ar_IQ’]=“بيانات وصفية اضافية”;
$templateMgr->assign(‘additional’,$additional);

PHP - Get value from saved dropdown

I have a plugin in WordPress in which I can make custom field like dropdown etc. Now I need to get the saved value from the dropdown.
I have this:
$vt_city = get_post_meta($cs_job_id, 'cs_post_loc_city', true);
But that displays it meta instead of the value like this: not_filled
And what I need it to show is: Not filled
How can I achieve such thing?
I can't see nothing about it in the doc, but you can use a regex at least
$clean_meta = preg_replace("/^(\w{1})([a-z]*)_([a-z]*)$/","$1$2 $3",$vt_city);

How to add custom field in the content type of Drupal 7

I want to add a custom field which value will come from external API. As example values may be:
array(
'v1'=>'value1'
'v2'=>'value2'
'v3'=>'value3'
)
So, I want to display them in a dropdown menu as field in the content-type article.
I don't it is right answer, but as per my view it is not better to store all values of third party DB.
To achive your goal I will do like this.
Content type Article which include another two fields:
Number field: Id (api_db_id) : v1, v2, v3....
Text Field: Value (api_db_value) E.g: value 1, value 2 ...
These above field will be hidden.
Before render form E.g: by using hook_form_alter we can connect to third party API and get values and populate in select list
E.g:
function myhook_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'article'){
/*
Third Party API connection code which will generate $dropdown_array.
*/
$options = $dropdown_array;
// Alter form
$form['title'] = array(
'#type' => 'select',
'#default_value' => '1',// set default value.
'#options' => $options,
);
}//endif
}
And Final step will before submit form we will assign selected ID and value to our new field .
You should create a custom module to implement your field.
A good tutorial can be found HERE.
You can check out version 7 of the examples module, specifically the field_example which has very good documentation in the code.
Once you get your head around how it (the hooks) works, there really isn't all that much code to write.
Doing it this way is probably much better than trying to alter other types of fields, and it should automatically integrate with views as expected.

Symfony embedded form - possible to NOT show embedded form's "parent" label?

When you embed a form in Symfony, a label for the embedded form is injected into the parent form. For example: If I have a PersonForm and add this code $this->embedForm('Address', $addressForm), my PersonForm will now have an 'Address' label within it, in addition to the labels for the fields that make up the AddressForm. I'd like to keep the labels for the individual fields, but get rid of the 'Address' label, thereby making it appear that the two forms are really one.
It's possible to override the form template and manually iterate over the form elements and echo them one by one, but I run into this situation frequently and I'd prefer to have Symfony handle this automatically.
The following code will allow you to set the label to another an empty string but I suspect it would still appear anyway.
$this->embedForm('Address', $addressForm)
$this->widgetSchema['Address']->setLabel('');
however I suspect the best thing to use is to look at point 6 (embedMergeForm) on this page and use that http://itsmajax.com/2011/01/29/6-things-to-know-about-embedded-forms-in-symfony/
Given your situation, iterating manually over the widgets is the only option. The other option is extending sfWidgetFormSchemaFormatter, but that won't allow hiding a label for an embedded form, while at the same time not hiding it for any other widget.
If you do run into this situation often, you could consider creating a partial just for rendering your form in this specific way.
Here is a simple way of disabling all labels. Add this method to BaseForm if you do it frequently.
public function disableLabels()
{
$fields = $this->getWidgetSchema()->getFields();
$this->getWidgetSchema()->setLabels(array_combine(array_keys($fields), array_fill(0, count($fields), false)));
}
If you only want to disable labels in the embedded form, disable them before embedding:
$form = new FormToEmbed();
$form->disableLabels();
$parent->embedForm('child', $form);

Zend Framework - Static form elements

I have got a form which a user can use to create a new store and to edit an existing one. When this form is being used to edit a store there are certain fields that I want the user to see but not edit eg. store_id. I have explored the different Zend_Form_Elements hoping to find some kind of static element but with no luck.
So my question is, how do I display information using Zend_Form that a user can't edit?
Thanks.
readonly alone is not enough, because users will still be able to edit it if they really want. You should use $element->setIgnore(true) that will ensure that Zend_Form_Element won't try to populate the element from POST/GET, and I'd double check that also. You have to make sure the values you are getting into the databases can never contain this element.
Finally, if you would like your element to be displayed in a different way than just with readonly, you can do that by changing the element decorators.
I just managed to work this one out myself. The solution was to change the view helper on the elements to the formNote helper eg. $element->helper = 'formNote'. The result of this was that the value gets displayed as straight text instead of being inside a form element.
Thanks for your answers.
That's very good solution when you don't need to populate the element value when the form is submitted.
It's equivalent solution is to use the Form Element method setAttrib() and disable the form element
$formElement->setAttrib('disable','disable')
which will only freeze the element.
But if you need to populate the field, using the previous solutions you will probably need additional hidden field added, which will pass the value. Developing custom form element will be good style but that's not welcomed by each developer so you can use some tricky way to set a form element as a text only but populate its value. That way is when you create the element as a hidden field, set its value and use the Form Element method setDescription() to set and display the element text value.
$formElement = new Zend_Form_Element_Hidden( 'elName',
array( 'label' => 'elLabel', 'value' => 'elValue' ) );
$formElement->setDescription( 'elValue' );
Then you can render that hidden element and display the value with the
$formElement->getDescription().
$element->setAttrib('readonly', 'true');
http://www.w3.org/TR/html401/interact/forms.html#adef-readonly
According to Amr Mostafa, if you use:
$element->setAttrib('readonly', 'true');
OR
$element->setAttribs(array('disabled' => 'disabled'));
User still send values by POST/GET and they are stored in DB.
The only way for me to don't taking into account the values from POST/GES is:
$element->setIgnore(true)
Example:
$element = new Zend_Form_Element_Text('element');
$element->setIgnore(true);

Categories