How to add attribute class in theme('pager') in drupal 7? - drupal

How to add attribute class in theme('pager') in drupal 7 ?
my code is following:
$directory_table .= theme('pager', array('quantity', $STAFF_COUNT ),array(
'element' => array(
'class' => array('pagination pull-right'))));
but it is not applying the class to the pager.

As the theme_pager function only expects 1 parameter (an array of variables), so your theme function call should look like this:
$directory_table .= theme('pager', array('quantity' => $STAFF_COUNT, 'element' => array('class' => array('pagination pull-right'))));
BUT:
It seems that theme_pager just sets a 'pager' class ignoring any variables which you may send to the function.
So, your best bet is to just override the theme_pager function. Do this by just copying it to your themes template.php file and renaming the function to MYTHEME_pager. Then you can just add the classes you need in that function, Or add the logic you need to add the classes depending on a variable sent to the function.
Remember to clear the theme cache after overriding the function.

Related

WordPress: wp_login_form how to add class name or placeholder text

How can I add placeholder or CSS classes to login fields using wp_login_form?
The function wp_login_form in wp-includes/general-template.php renders the login form from an array of arguments:
$default = array(
'echo' => true,
...
...
);
and then creates the <form>...</form>. There is no way I can add a class name to input fields or the submit button. I want to use bootstrap classes for this purpose. Currently I have to override default classes rendered by WordPress.
I don't want to do that. For example if I want to make username box to look like an input with class form-control, I have to either write additional classes in my CSS for default WP classes or take help of jQuery to remove default classes and add mine.
What is the best way to do it? wp_login_form does not have attributes set for placeholder.
In brief I need to pass the following:
1. Pass class name from outside via an array $args,
2. Pass placeholder text for input fields
This might be a solution, eventhough it isn't perfect.
Wordpress' functon wp_login_form() does not support changing the css class. And I would recommend not to use Javascript for changing the DOM when you do not absolutely have to.
My solution to modify the CSS class was to set the echo property to false so the function returns the result as a string. Then, use str_replace() to find the classes we want to replace and replace it with the class names we want to use.
$args = array(
'echo' => false,
// etc...
And now replacing the class names...
$output = wp_login_form( $args );
$output = str_replace( 'class="input"', 'class="df-input"', $output );
echo $output;
You could also convert $output into a DOMDocument() so you can replace the class names in an more elegant manner.
According to its documentation, wp_login_form() does not receive an argument to set a class name for itself of input elements. Presumably because you wouldn't have more than one login form on the page.
It is possible to add an id to the inputs of the forms like so:
$args = array(
'form_id' => 'loginform',
'id_username' => 'user_login',
'id_password' => 'user_pass',
'id_remember' => 'rememberme',
'id_submit' => 'wp-submit',
);
However, seems that in order to set other attributes such as class and placeholders you would have to use Javascript. This can be done without JQuery. This would look like something along the lines of document.querySelector('#username').setAttribute("class", "username");
See the querySelector documentation.

How do i set a specific template for a specific module in drupal 6 using hook_theme

Is there any way by which i could assign a template to my custom module.I heard it may be possible.I tried out with the hook_theme function.My hook_theme looks something like this
function special_theme() {
return array(
'special' => array(
'template' => 'special',
'arguments' => array('link' => NULL),
),
);
}
I do have a special.tpl.php file in my module folder.But the tpl file is not called.Its my default template that is been shown as output.Could someone please help me in the right direction.would be very helpful.
What you define via hook_theme() is an available template, not one that is automatically used. In order to use that template you need to call theme('special', $link);.
It is also advised to avoid using simple words for theme names to avoid collisions ( try mymodule_special instead ).
Also note (though basic), that you also need to print the return value of theme(), it does not get automatically printed. So for instance,
print theme('special', $link);

Register your theme functions in Drupal

I am new to Drupal and I am working on creating my own theme for our custom module. Can any one please help me understand how to register our theme functions or share any ideas or links that explains the process from scratch.
Register a theme function means implementing hook_theme in your module.
For example, if your module is called "example", then you need to have a function called example_theme in the example.module file. The theme function must return an array or you'll end up with the famous white screen of death.
In example.module:
<?php
// $Id$
// Implements hook_theme
function example_theme(){
return array(
'mydata' => array(
// Optionally, you can make the theme use a template file:
// this line references the file "mydatafile.tpl.php" in the same folder as the module or in the folder of the active theme
'template' => 'mydatafile',
// these variables will appear in the template as $var1 and $var2
'arguments' => array(
'var1' => null,
'var2' => null,
),
),
'myotherdata' => array(
// these variables will appear in the functions as the first and second arguments
'arguments' => array(
'var1' => null,
'var2' => null,
),
)
);
}
// If you don't want to use a template file, use a function called "theme_THEID" to create the HTML.
function theme_myotherdata($var1, $var2){
return "<div>var1= $var1 and var2= $var2</div>";
}
In mydatafile.tpl.php:
<div>mydatafile.tpl.php was called</div>
<ol>
<li>var1: <?php echo $var1; ?></li>
<li>var2: <?php echo $var2; ?></li>
</ol>
You can then later call the theme function manually if needed:
$html = theme('mydata', 'hello world', 123);
$html = theme('myotherdata', 'hello world', 123);
In which case "mydatafile.tpl.php" and "theme_myotherdata" will receive the value "hello world" in $var1 and the value 123 in $var2.
There are many more options, like changing the name of the function, using patterns instead of a fixed name, being able to have the function in another php file or such, check out the link.
Here are a couple more ressources about theming:
The theme guide
Overriding themable output
The Devel module
The Theme developer module (requires the Devel module)
a longuer example that also creates a form/page to use the theme function
By the way, you will need to rebuild the theme registry cache if you add the functions in the .module file after it has been installed, in which case you can do so by clearing the cache (one way is using the button at the bottom of the the Performances page).
NOTE: Slightly different syntax with Drupal 7 (ex. 'arguments' changes to 'variables')
http://www.davidcalculli.com/blog/2012/01/drupal-7-custom-template-only-displaying-the-first-character

Drupal form with custom ID

Correct me if I'm wrong, after reading drupal fapi related articles, I got the impression that fapi generates 'id' attributes by itself. It allows developers to assign 'name' attribute only. If that's the case, is there a way I can set desire 'id' value for elements? Because, I want my elements to have meaningful 'id' so that html/jquery code would be easier to read as well as save my time from going through already written jquery code to change those all 'id's that I've used inside.
P.S:drupal version - 6.x
Ok found the solution. I can use the #attributes key of the $form element to set any additional attributes (such as class, id, etc.). Thanks for your help so far.
I had a similar issue to deal with. I needed to have multiple forms on the same page so I had to change the ids of the form and its elements to prevent duplicate ids. I did something like the following:
function voci_comment_form($form, &$form_state, $cid) {
$form['#attributes']['id'] = 'voci-comment-form-' . $cid;
$form['#attributes']['class'][] = 'voci-comment-form';
$form['body'] = array(
'#title' => 'Post a comment',
'#type' => 'textarea',
'#resizable' => FALSE,
'#rows' => 1,
);
$form['comment'] = array(
'#type' => 'submit',
'#value' => 'Comment',
);
foreach ($form as $k => &$element) {
$k = str_replace('_', '-', $k);
$element['#attributes']['id'] = "edit-$k-$cid";
$element['#attributes']['class'][] = "edit-$k";
}
return $form;
}
This basically sets unique ids based on the $cid that is passed in. The code also adds classes to each element in the form so you can style it easily. I'm sure a more robust solution is possible but this is the basic idea. Tested in Drupal 7.
It's true that you can set $element['#attributes']['id'] and that will apply to the form field. However, it will break labels and #states in Drupal 7 because the rest of the rendering pipeline reads the ID from somewhere else. So for your labels and #states to keep working, use set the ID to $element['#id'] instead (an undocumented property that nonetheless is how the form API watches ID internally).
Make sure to pass your ID through drupal_html_id as well to ensure no conflicts.
This problem doesn't really have much to do with the Drupal-FAPI itself, but more with how Drupal theme forms (create the markup).
If you want to alter all forms on your site, you can overwrite the theming functions that is used for forms and the different type of form fields.
If you just want to overwrite some forms or form fields, you can set the #theme attribute on the form or an element, to change which function should be used for creating the markup.

Drupal: How to theme a module

I'm trying to theme a modules output.
In particular i'm working on http://drupal.org/project/service_links
Any idea how that works?
Thanks in advance!
Generally, if you want to theme a module you have a few options.
Overwrite theme functions. You can overwrite the theme functions that the module uses/implements to change the markup, one example of such a function is theme_service_links_node_format. You change make a function in your theme's template.php called 'your_theme_name_service_links_node_format' and make your custom markup in it instead.
CSS. If you don't need to change the actual markup of a modules output, you only need to add the needed css, to theme it into your liking.
In some cases, it doesn't look like sercive links is such a case, you can also make your own templates, and make Drupal use them instead.
Another way, again it doesn't look like service is service links is such a case, is to implement preprocess functions in your template.php. This is needed if you want to alter how certain template variables are generated.
If you want to implement your own theming function services links defines 3 themables. In your theme you should imlement the following
yourtheme_service_links_build_link()
yourtheme_service_links_node_format()
yourtheme_service_links_node_format()
'service_links_build_link' => array(
'arguments' => array(
'text' => NULL,
'url' => NULL,
'title' => NULL,
'image' => NULL,
'nodelink' => NULL,
),
),
'service_links_node_format' => array(
'arguments' => array('links' => NULL),
),
'service_links_block_format' => array(
'arguments' => array('items' => NULL),
),
Have a look at http://drupalcode.org/viewvc/drupal/contributions/modules/service_links/service_links.module?view=markup line 389 and below
What's the problem? I mean, every module should use a different name for main container and so. You can use css selector in clever way to refer the template pages.
For example, the FAQ module use identificator to all part of html output, like faq-question and faq-answer in the main page.
Just inspect your resulting code and css it, if possible modify the module-related css!
If the module implements its own theme hooks you can use that. You can also use CSS.

Resources