Drupal: include small form in page.tpl.php - drupal

In my template.php i have a function_name($form) with some basic html markup and a drupal_render($form['mail']) and drupal_render($form) that returns $output;
I'd like to include this small snippet & form in my page.tpl.php. Everything else in page.tpl.php is printed out as a single variable, so I'd like to do that for the $output of the function above.
What is the best way to do this?

In theory you could append the output of the function to the $content variable.
However, It sounds like you have something which may be better off as a block, rather than something that exists in your template.php.
You can quite easily create a module with a single block (which would be your function). You could add this where ever you wanted and easily turn off and on (and all the other block admin goodness) without having to change the code.

The other thing you could try out is the webform module. It's a nice module that has a large feature set, saving you the trouble of having to create a module. You can have a webform block which you can stick on that particular page (restrict to page).
I also see that your form is called mail. Webform also allows you to email form submissions. In addition, it provides a nice interface to see what users have submitted.

Related

Output CCK items in custom template

I am working with a Drupal 6 site which relies heavily on CCK and views and I want to use the content in a webapp for iPhone. The webapp is built in HTML and CSS and will ultimately be converted to native using PhoneGap or similar and I want to import the formatted HTML using a JQuery ajax call. The plan is to create a view in the website which returns the data that I want for each page and then create a template for that view which outputs the data in the correct format for including.
I need to be very prescriptive with the HTML output which is where I am struggling. I'm not overly familiar with Drupal templating and what I have done seems like a lot of HTML is created when outputting a node, and it is all also contained within the site template. I need to be able to output exactly what I want e.g.
<div class="customclass">
<?php echo($cckfield) ?>
</div>
<?php foreach($cckarray as $key -> $value) {
echo(some html using $key and $value);
}?>
Hopefully the pseudocode above gives an indication of the level of control required. Is this possible using either core or with a module? Or do I need to roll my own pages and use the API rather than going for Views?
The problem with this is that there are many files collected together and merged into the output file. If you want clean HTML, you need to go through the different files that make up the final output (e.g. page.tpl, different modules files,...).
The best way to change the base HTML-output is to filter everything that you don't need out of the $node object that every site gets passed. It also holds all the CCK values in formated and unformated form as well as all other fields. SO creating a custom template with no HTML and the bare values of $node should give you what you want.

Help with Drupal Search Form Display

When displaying the search form in my header, what is the preferred method to use if it cannot fit into my header region??
should I...
create a custom region?
use some kind of 'print $search_form'
drupal_get_form()??
use the theme() function??
please help! I'm new to Drupal and trying to figure out the best 'Drupal Way' of doing things.
Depends on how you want to define or maintain the search form. If you think of it as part of the theme (that is, it's as static as the page background or colors), consider just using the following in page.tpl.php:
<?php if ($search_box): ?>
<?php print $search_box ?>
<?php endif; ?>
This is de-facto the Drupal Way: if your theme didn't modify the page variables, you get $search_box for free in page.tpl.php. Adding the conditional also lets site maintainers turn it off in the theme settings and specify permissions for it.
If you want to give site maintainers the ability to move it around from region to region, consider using the Search block. This way it can be utilized just as any other block on your site. This would also be considered Drupal Way-ish: you get the block for free if you enable the Search module.
If you want theme the forms, override search-theme-form.tpl.php if you use the first method, and search-box-form.tpl.php if you used the second. Both templates can be found in modules/search.
You could do any of those. You could create a custom region, and use CSS to change the display. You could alter the form with preprocessing hooks or hook_form_alter.
Depends on whatever is easiest for you.

Show secondary links in views only

I'm building a site in Drupal and I only want to show the secondary links on the
pages that use the Views I've created. I tried using the $secondary_links variable
in the views-view.tpl.php but the variable is null. How can I achieve this?
The secondary links are as mac correctly writes only available in page.tpl.php, but if I understand you correctly, the best solution is not getting the secondary links into your view.
With your theme, the secondary links, will most likely be printed out where they should, regardless of what is being displayed, be it your views, nodes, the front page etc. Views are displayed and everything else you render, is wrapped in the page template, that controls where menus are located, regions and other fun stuff.
Now, if you don't want to alter this, the location of the menus, their styling and this stuff, you shouldn't be printing the secondary menu in your views template, you shouldn't be doing anything with it at all.
The solution is simple
It's using something that mac mentioned but in a different way: preprocess function. These functions are used to in your template.php file, to add some logic to your variables. You can alter variables or remove them altogether. What I would do, would simply be to remove the primary links, by setting the value of $primary_links to an empty text string.
This would effectively remove the primary links, so only the secondary links are displayed. You could also display the secondary links as the primary, but this might cause confusing to your users. You just need to add some logic to control when this should happen and you are set.
Have you activated the secondary links from the theme settings? That would be:
http://example.com/admin/build/themes/settings/name_of_your_theme
I believe once you have activated the option, the variable will be populated.
EDIT: Thinking a second more, I would also comment that I am not sure if the primary and secondary links are passed to the views templates. I believe those are passed to the page.tpl.php file instead. If I am right, and for some reason you want to add that variable to those passed to the views template, you will have to use a preprocess function, like explained here.
EDIT #2: Indeed if you only need the secondary menu used in a specific views template, another approach would be to simply call menu_secondary_links() from within the template. This is not the most elegant solution ever, as it puts in a theming element something that should belong somewhere else, but it's up to you to make the call whether that menu in the views is a core functionality or a styling element.
HTH!
You can use the following code to show secondary menu on any view
function YourTheme_preprocess_views_view(&$vars)
{
$menu_sec = menu_navigation_links('menu-secondary');
$vars['custom_menu'] = theme('links__menu-secondary', array('links' => $menu_sec));
}
or you can even use other preprocess function depending upon your needs.
Further you can call it on .tpl.php file using:
<?php
$menu_sec = menu_navigation_links('menu-secondary');
print theme('links__menu-secondary',
array(
'links' => $menu_sec,
'attributes'=>array(
'class' => array('nav', 'nav-pills', 'p-f-subfilter'),
)
)
);
?>

Change size of user/password login box

I don't know how to change the size of the login username/password boxes on the drupal site that I'm trying to build. I'm stumbling through the theming, and don't know where to find the file that needs to be changed in order to have boxes that fits the aesthetic (so a file path would be very helpful).
I'm hoping it's a css solution. You can see the site first hand at innovatefortomorrow[dot]org and my firebug screenshot http://www.jonrwilson.com/user-login-form.png (I don't have enough reputation points to attach an image or two hyperlinks).
Thanks!
read this as well! This is an alternative answer!
Ok... you are about to enter one of the most exciting and complex features of Drupal: the form API or - for brevity - FAPI. Some theory first, and then the solution! :)
All forms in Drupal are built by the drupal_get_form() function, that accepts an array as parameter. Each field in the array is basically a field of your form, and each field has a number of proprieties, each of them define additional characteristics of the the field, like for example its default value, if it is required or optional and - yes - in the case of textfields... how large they have to be! You can find a detailed explanation of the structure of form arrays here on the drupal site.
The beauty of the form API is that the function that renders the form invokes a number of hooks at various moments during its building process, so you can implement these hooks in order to "alter" a form before it is finalised and sent to the browser.
The most commonly hooks for form alteration are hook_form_alter() and hook_form_FORM_ID_alter(). The first is executed for any form processed by the drupal engine, the latter only for the specific form named "FORM_ID". I will not get into any more details on the internal working of the form API, but here you can read more.
As for your specific case, I assume you are using the standard "user block" shipping with Drupal. In this case I suggest you implement hook_form_FORM_ID_alter() in a form similar to this one:
mymodule_form_user_login_block_alter(&$form, $form_state) {
$form['pass']['#size'] = 43;
}
Hope this helps! :)
I think in this case you have to go to the your html ( or tpl), not your css file to edit it.
One quick way is to search the relevant string (i.e., size="43" name="name" etc) in order to find the correct part.
Here is the way to theme a user login form in drupal 6 with the preprocess function in a template file and not in a module.
in template.php put this code:
function yourThemename_preprocess_user_login(&$variables) {
$variables['form']['name']['#size'] = 15;
$variables['form']['pass']['#size'] = 15;
$variables['rendered'] = drupal_render($variables['form']);
}
create a new file user-login.tpl.php (if it's not already there) and just paste this:
<?php print $rendered; // this variable is defined in the preprocess function user_login and print the login form ?>
Don't forget to clear theme cache or system cache in the performance settings.

How do you remove the default title and body fields in a CCK generated Drupal content-type?

When you create a new content type in Drupal using the Content Creation Kit, you automatically get Title and Body fields in the generated form. Is there a way to remove them?
If you're not a developer (or you want to shortcut the development process), another possible solution is to utilize the auto_nodetitle module. Auto nodetitle will let you create rules for generating the title of the node. These can be programmatic rules, tokens that are replaced, or simply static text. Worth a look if nothing else.
To remove the body edit the type, expand "Submission form settings" and put in blank for body field label. For title you can rename it to another text field. If you really have no need for any text fields you can create a custom module, say called foo, and create function foo_form_alter() which replaces $form['title'] with a #value when $form['type']['#value'] is your node type.
No need to install anything:
when editing the content type, press "Edit"
(on the menu of Edit | Manage fields | Display fields )
click on the Submission form settings
on the Body field label:
Leave it blank, it would remove the Body field.
If you're not a developer (or you want
to shortcut the development process),
another possible solution is to
utilize the auto_nodetitle module.
Auto nodetitle will let you create
rules for generating the title of the
node. These can be programmatic rules,
tokens that are replaced, or simply
static text. Worth a look if nothing
else.
And to add on to William OConnor's solution...
The module is poorly documented unfortunately. It's really only effective if you use PHP with it in my opinion. Check off the "Evaluate PHP in Pattern" and type into the "Pattern for the title" field something like:
<?php echo $node->field_staff_email[0]['email']; ?>
or:
<?php echo $node->field_staff_name[0]['value'] . '-' . gmdate('YmdHis'); ?>
...where I had a field with an internal name of "field_staff_email" and was using the CCK Email module -- thus the 'email' type was used. Or, I had a field with an internal name of "field_staff_name" and was just an ordinary text field -- thus the 'value' type was used. The gmdate() call on the end is to ensure uniqueness because you may have two or more staff members named the same thing.
The way I discovered all this was by first experimenting with:
<?php print_r($node); ?>
...which of course gave crazy results, but at least I was able to parse the output and figure out how to use the $node object properly here.
Just note if you use either of these PHP routines, then you end up with the Content list in Drupal Admin showing entries exactly as you coded the PHP. This is why I didn't just use gmdate() alone because then it might be hard to find my record for editing.
Note also you might be able to use Base-36 conversion on gmdate() in order to reduce the size of the output because gmdate('YmdHis') is fairly long.
The initial answers are all good. Just as another idea for the title part... how about creating a custom template file for the cck node type. You would copy node.tpl.php to node-TYPE.tpl.php, and then edit the new file and remove where the title is rendered. (Dont forget to clear your cache).
Doing it this way means that every node still has a title, so for content management you aren't left with blank titles or anything like that.
HTH!

Resources