How to prevent Drupal 7 from converting & to & - drupal

I'm adding placeholder to search form like this:
$form['search_block_form']['#attributes']['placeholder'] = ' Search';
But problem is that drupal is printing & sign as & so in HTML output
for placeholder attribute instead of:
placeholder=" Search"
I'm getting:
placeholder=" Search"
How to prevent this behavior or is there another way to print that character by it's hex value? Tried using:
[] and decode_entities('') and some other functions, but none of them helped.

I think you were close with decode_entities(), but you could try the PHP function html_entity_decode().
However, unless the field you are trying to add the place holder to is called 'search_block_form', I think it is your construct which is wrong.
If you are doing this is a hook_form_alter() (which you should be if you are writing it like this) and the field you want to add the placeholder to is called "my_text_field", then the function should look for like this:
function custom_module_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'custom_form') {
$form['my_text_field']['#attributes']['placeholder'] = html_entity_decode(' Search');
}
}
Just remember to replace 'custom_form' with the real ID of your form and 'my_text_field' with the name of your field you want to alter.
/**** EDIT ****/
Finally understood what you are trying to do. To get this working on my site I had to lay my form item out like this:
$form['test'] = array(
'#type' => 'textfield',
'#attributes' => array(
'placeholder' => html_entity_decode(''),
'style' => array('font-family: Arial, FontAwesome;'),
),
);
The important thing to add is the style attribute of font family and make sure it includes FontAwesome.
So the above example for you would become:
function custom_module_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'custom_form') {
$form['my_text_field']['#attributes']['placeholder'] = html_entity_decode(' Search');
$form['my_text_field']['#attributes']['style'] = 'font-family: Arial, FontAwesome;';
}
}
Not sure how you have added the JS for Font Awesome, but I added it in a page alter using drupal_add_js() so that I could make sure it only get loaded on the pages its required.
Let me know how that works out for you, and don't forget the flush your caches too!

Related

Drupal change multi select to checkboxes on views exposed form not working

I needed to have all taxonomy vocabularies available to filter on, without adding all of the taxonomies one at a time.
I did this using the Content: Has taxonomy terms (Multiple) Filter - which renders out as a multiple select list.
I needed to change the select list to checkboxes, but BEF didnt allow me to do that for this type of field, so i did the following...
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'views_exposed_form') {
$options = $form['filter']['#options'];
unset($form['filter']);
foreach($options as $vocab => $terms) {
foreach ($terms as $key => $value) {
$newkey = $options[$vocab][$key]->option;
$termoptions[$vocab][key($newkey)] = $newkey[key($newkey)];
}
$form[$vocab] = array(
'#type' => 'checkboxes',
'#options' => $termoptions[$vocab],
'#title' => $vocab,
'#multiple' => TRUE,
);
}
}
}
The exposed form looks good, but it doesnt work.
I think its because the name of the query is wrong. As i have split up the heirarchy into separate fields, the url used to look like
mysite.com/category?filter[]=123
Now it looks like...
mysite.com/category?Brand[123]=123
So theres how far ive got, any ideas how i can make this exposed form work?
I had a poke around at changing the submit handler views_exposed_form_submit but i dont know what i would need to change.
i am updating my ans once again for more clear view
step one expose filter
settings for this
click on settings text then
then gor for bef
and the result is
hope it make sense now

How to add span tag in to Drupal webform element label?

I am trying to style my webform checkboxes and radio buttons differently. For this reason I want to insert span element in to the label and use it instead of original checkboxes.
Found this module : https://www.drupal.org/project/webform_optionsmarkup , but non of the listed options appear in my webform field settings.
Does any body know good solution to accomplish this ?
function bigdaddy_preprocess_webform_element(&$variables) {
if ($variables['element']['#type'] == 'textfield' || $variables['element']['#type'] == 'textarea') {
$variables['element']['#title_display'] = 'after';
}
if ($variables['element']['#type'] == 'checkboxes') {
foreach ($variables['element']['#options'] as $key => $one) {
$variables['element']['#options'][$key] = "<span>jpnas</span>" . $one;
}
}
I was able to achieve what you want by using the hook_form_alter() function. In my module's .inc file, I created a function like MY_MODULE_form_alter. I'll share my code...
function MY_MODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'MY_FORM_ID') {
foreach($form['ELEMENT_#NAME']['#options'] as $key => $value) {
// $form['ELEMENT_#NAME'][$key]['#theme_wrappers'] = array();
$form['ELEMENT_#NAME'][$key]['#prefix'] = '<span>jpnas</span>';
$form['ELEMENT_#NAME'][$key]['#suffix'] = '<span>jpnas</span>';
}
}
}
If you want to place the span before the checkbox, use ['#prefix'].
Obviously replace MY_MODULE with the name of your module, MY_FORM_ID with your actual $form_id name, and ELEMENT_#NAME with the name of the form element you want to alter. Remember you can do a var_dump($form) if you need to see any of these values. I am doing pretty much the same thing it sounds like you would like to do, except I am not putting this into my label as I haven't found a way to attach directly to the label. The input element on the other hand is a different story. Furthermore, if you uncomment the first line in the loop, the wrapper will be removed from around each checkbox, but it will take with it the label. You can then add the label yourself along with your own wrapper element using #prefix and #suffix accordingly.

Drupal 7 Views custom view template fields

I've successfully created a custom view template for my Drupal 7 site but am having issues adding attributes to the content which is outputted. I've searched high and low for the answer to this but to no avail.
I have a view called: views-view-fields--homepage-articles.tpl.php
I am printing content like :
$fields['title']->content
This is fine and expected, and outputs:
Title
But I want to add classes to it - how? I'm thinking I need to write a hook, but I cannot find this documented anywhere. At the moment my solution is a string replace:
<?php print str_replace('<a ', '<a class="brand-blue uppercase nodecoration"', $fields['title']->content); ?>
As you can imagine, this is not a satisfactory or long-term solution.
Many thanks!
You should be able to add the classes to the field using template_preprocess_views_view_fields().
Edit: Couldn't do it the way I thought, but you can overwrite the output of the field like so:
function MY_THEME_preprocess_views_view_fields(&$vars) {
$view = $vars['view'];
if ($view->name == 'node_listing') {
foreach ($vars['fields'] as $id => $field) {
if ($id == 'title') {
$field_output = l($view->result[$view->row_index]->node_title, 'node/'. $view->result[$view->row_index]->nid, array('attributes' => array('class' => 'brand-blue uppercase nodecoration')));
$vars['fields'][$id]->content = $field_output;
}
}
}
}
Have you tried using Semantic Views? https://drupal.org/project/semanticviews - that way you can override the classes within the UI instead of template files, may suit your needs better.

Drupal, template.php where do the $form names come from?

I want to customize my Drupal back-end forms.
I'm using template.php file.. i.e.
$form['menu']['#collapsed'] = true;
$form['author']['#collapsed'] = true;
$form['buttons']['#weight'] = 100;
But I was wondering from where the section names (menu, author, buttons), come from. (They are not id or classes in html code, so I guess there is an index with all names stored somewhere.
Where can I get the complete list of section names ?
For example, what are the names for revision and publishing sections ? 'revision', 'publish', 'publishing' don't work.
thanks
If I am not mistaken, you want to see structure of some forms. Each form in drupal has an Id. First, you need to know the form_id. You can do this with a custom module and implementation of hook_form_alter:
function mymodule_form_alter(&$form, $form_state, $form_id) {
drupal_set_message($form_id);
}
When you have found the Id, alter the snippet to prints out the form structure:
function mymodule_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'a_form_id') {
drupal_set_message(print_r('<pre>'. $form .'</pre>', true));
// If you have installed Devel module, following line is much more readable:
// dpm($form);
}
}
Now when you go to the page containing the form, you see it's structure.Each form element is represented as an array, for example, a text field can be like this:
$form['name'] = array(
'#type' => 'textarea',
'#title' => t('Username')
);
Look for Form API in Drupal website for more info.
I don't think there actually is a naming system with forms like that. The names is most likely the same used when defining the form, which could be anything really. Drupal core might be consistent, but if you want to add contrib modules, you can't be sure of anything.

Drupal Views2 Exposed Form how to change

I have a View with an exposed form . I am trying to a few things on it. Ideally I would like to have a dropdown that fires the form with no button. If that is not possible then I would like to have the button text something different than apply.
I hacked it for now and change views_form in views.module but that does not seem like the right way to do it. I only have one exposed form right now, but what if I add more?
Please see http://www.wiredvillage.ca/News for my example.
I am poking around drupal.org and seeing others with the same problem but no solutions so far. Not sure where the best place to get Drupal help is.
Here is the change I made so far:
function views_exposed_form(&$form_state) {
// Make sure that we validate because this form might be submitted
// multiple times per page.
$form_state['must_validate'] = TRUE;
$view = &$form_state['view'];
$display = &$form_state['display'];
$form_state['input'] = $view->get_exposed_input();
// Let form plugins know this is for exposed widgets.
$form_state['exposed'] = TRUE;
$form['#info'] = array();
if (!variable_get('clean_url', FALSE)) {
$form['q'] = array(
'#type' => 'hidden',
'#value' => $view->get_url(),
);
}
// Go through each filter and let it generate its info.
foreach ($view->filter as $id => $filter) {
$view->filter[$id]->exposed_form($form, $form_state);
if ($info = $view->filter[$id]->exposed_info()) {
$form['#info']['filter-' . $id] = $info;
}
}
// I CHANGED The VALUE OF THIS SUBMIT BUTTON TO GO
$form['submit'] = array(
'#name' => '', // prevent from showing up in $_GET.
'#type' => 'submit',
'#value' => t('go'),
);
$form['#action'] = url($view->get_url());
$form['#theme'] = views_theme_functions('views_exposed_form', $view, $display);
$form['#id'] = views_css_safe('views_exposed_form-' . check_plain($view->name) . '-' . check_plain($display->id));
// $form['#attributes']['class'] = array('views-exposed-form');
// If using AJAX, we need the form plugin.
if ($view->use_ajax) {
drupal_add_js('misc/jquery.form.js');
}
views_add_js('dependent');
return $form;
}
Or, you could use a preprocess function to alter the form even before it is build. I wanted to change the text on the button, so I did this:
function MYTHEME_preprocess_views_exposed_form(&$vars, $hook) {
// only alter the jobs search exposed filter form
if ($vars['form']['#id'] == 'views-exposed-form-jobs-search-page-1') {
// Change the text on the submit button
$vars['form']['submit']['#value'] = t('Search');
// Rebuild the rendered version (submit button, rest remains unchanged)
unset($vars['form']['submit']['#printed']);
$vars['button'] = drupal_render($vars['form']['submit']);
}
}
If you want the drop-down to fire, I'd use JavaScript instead of hacking the module as Eaton suggests.
Basically, you can modify the text with hook_form_alter as Eaton suggests, then use in the same hook_form_alter, add a call to drupal_add_js with your custom JS which hides the button and submits the form on the onChange handler of the select drop-down. You want that submit button there for those 10% of users for whom the JS fails.
Both of the above are fine but I found out that altering the form might not always lead to desirable results, mainly because exposed filters are themed using a specifc theme template. The proper way of changing the theme would be to override the views-exposed-form.tpl file in your theme's folder. Bear in mind that this will apply to all exposed filter forms, to theme a specific one, you will need to use a different name for that filename, like:
views-exposed-form--TITLE--DISPLAY.tpl.php
views-exposed-form--TITLE.tpl.php
and some others, you can check the Theme: Information section of your views for template naming conventions.
This module provides an auto-submit among other things http://drupal.org/project/views_hacks
This module is great to improving exposed filters http://drupal.org/project/better_exposed_filters
You should be able to use hook_form_alter() (http://api.drupal.org/api/function/hook_form_alter) to change the form as it's built, modifying the fields in question when that particular view is being displayed. You can nuke the submit button, add a #theme function that calls the drupal_add_js() function, and so on.
As long as the GET params come in the way views expect them, everything will work fine -- it was designed that way to allow bookmarking of pages with exposed filter settings, etc. The important part is to make sure you're doing the form mangling in your own module's hook_form_alter() function, so that it won't make other views driven stuff choke.

Resources