How to add warning text in Drupal comment form - drupal

I want a warning message displayed in the comment form when people try to add comments:
"Please write comments in correct
grammatical English, otherwise they
will not published"
How can I do it?

Here is how you can do it by using hook_form_alter in your own module:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case "comment_form":
$form['#prefix'] .= "<div><p>Show some text before the comment form.</p></div>";
break;
}
}

You can alter the comment form so that your guidelines are added to it. There are a handful of ways to alter forms in Drupal. You can do it in your theme's template.php file (which I prefer for simple changes) or in a custom module. This article describes both methods, in Drupal 5 and 6, however not for the form you're interested in. However, the method used is the same that leads to the solution below. This is how you can make the change via template.php:
The following PHP code can be added to your theme's template.php file:
function YOURTHEME_theme() {
return array(
'comment_form' => array(
'arguments' => array('form' => NULL),
),
);
}
function YOURTHEME_comment_form($form) {
$output = '';
$output .= '<div class="comment-help">' . t('Please write comments in correct grammatical English, otherwise they will not published.') . '</div>';
$output .= drupal_render($form);
return $output;
}
Replace YOURTHEME with the name of your theme. If you already have a YOURTHEME_theme function you will need to add the 'comment_form' key to the array it is already returning. I doubt you do, but it's worth mentioning just in case.
A note: you should not be editing any of the themes in /themes, but you should have made a new theme or copied and renamed any of those themes into /sites/default/themes or /sites/all/themes.
The above code is based on code from this page.

Once you are inside a hook_form_alter function you can use the Development module (http://drupal.org/project/devel) dpm() function in place of var_dump to help view and isolate which properties to change in the big form arrays. I find this is a must-have when trying to figure out changes to an existing form. It puts all the elements of the form array into clickable rows.

In Drupal 7 go to
admin/structure/types/manage/mycontenttype/comment/fields/comment_body
There you can add your text. It will be shown below the field as usual. If you want the warning displayed above the field, you'd have to go the form_alter way.

Related

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 7 - Theme the user profile form

I'm trying to theme my Drupal site's user profile form at the moment. I'm using hook_form_alter in the theme's template.php file.
The code is the same as the code I've used to edit another form but for some reason I can't spot it's not working.
function THEME_NAME_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_profile_form') {
$form['current_pass']['#prefix'] = '<div class="loginFormBlock">';
$form['current_pass']['#suffix'] = '</div>';
$form['current_pass']['#size'] = '500';
//$form['actions']['submit'] = array('#type' => 'image_button', '#src' => base_path() . path_to_theme() . '/images/Login.png');
}
}
Now the commented out submit button part works when it's un commented but the current_pass bits don't do anything. Current_pass is the name of the field I'm trying to theme. THEME_NAME has been replaced by the theme's name.
ANSWER:
As suggested I looked into the $form array.
echo '<pre>';
print_r($form);
echo '</pre>';
Saw that current_pass was in the account array and amended the code to the following, which works fine.
$form['account']['current_pass']['#size'] = '500';
Hopefully this can help someone else.
I was just going to say same thing - do a dump of $form and make sure that the field actually exists - if it does and it still doesn't work then there may be another hook being called after yours and changing your changes.
ps: I am still using D6 but are you sure your putting your form_alter function in the right place? yours says THEME_NAME_form_alter - I know in D6 you have to put them in module layer not the theme layer (unless this has changed in D7???) - could this be your issue?

Why wouldn't these theme functions work with Drupal D6?

I'm at my wits end with Drupal 6 right now, and I'm hoping a second pair of eyes might be able to point out some syntax sublety I was missing earlier this morning.
Lets say I have a theme called my_theme.
This is a theme that subthemes from Ginkgo, a theme which which in turn subthemes from
Rubik, whith in tun subthemes from Tao, a fairly common CSS reset theme.
I'm trying to understand how to declare my own theme functions, so I can
clean up the mark up on a fee page I'm workign on for site.
Now my understanding of theming is along the lines of ' if register a
theming function with hook_theme, passing it into an array, with the
function's name, you'be able to call it in future with
theme_function_name from within your theme.
For example, if have a module called my, I'd immplent the hook_theme
like below, defining the name of the function, and defining which
argument should be passed into it:
<?php
function my_theme() {
$items = array();
$items['randomtext'] = array('arguments' => array('element' => NULL));
$items['button_links'] = array('arguments' => array( '$links' => NULL, '$attributes' => NULL));
return $items;
};
Now I have the functions registered, I just need to implement them like
this somewhat contrived example:
function theme_randomtext($element) {
$output = ' <h1>This is some very random text with' . ' this text concatenated: ' . $element . '</h1>';
return $output;
}
I can now use this new function by calling
<php
print theme('randomtext', 'some arbitrary words')
?>
And expect the following content to be returned:
<h1>This is some very random text with this text concatenated: some arbitrary words</h1>
I'm getting nada.
And I don't know why. When I look at the theme registry, I see my
function listed as my_randomtext - I've tried calling both of these
options, in case I should have been adding the theme prefix:
theme('my_randomtext', 'getting desparate');
theme('randomtext', 'really losing my rag now');
Still no luck.
Why might these theme functions not be working? And what advantage does
this give you over simply declaring a function like so in a theme?
function manual_random_text($element) {
$output = ' <h1>This is gives me everything I aleady need,
'without relying on the weird themeing sytem. It's perfect for' . $element . '</h1>';
return $output;
}
Ah, after a bit of distance from the problem, I see the issue.
For this to work, I need to add the prefix of the theme I declare the function in, to make it available to me for use.
So if my module name was my, the desired theme function name would be:
function my_random_text($element) {
$output = ' <h1>,
'This is finally outputting content. Now I can: ' . $element . '</h1>';
return $output;
}
The thing is, if I'm creating an override to a theme function that doesn't already exist, like random_text, there are no functions for it to override anyway.
The term theme override suggests there being a default theming function to override - in many themes, we're not making explicit overrides of functions, but creating alternatives to use in specific cases.
I'm now assuming that I'd need to define this in a module if I wanted to define a function as theme_function_name rather than themename_theme_function_name.
This page here about theming on Drupal.org was a useful quide in finding out some of this info.

Drupal 7: how to add template file to /node/add/content-type

In Drupal 7.
I want to themming /node/add/content-type by template file as page--node--add--content-type.tpl.php.
Please help me.
You need to use
page--node--add--content_type.tpl.php
instead of
page--node--add--content-type.tpl.php
underscore(_) instead of dash(-)
You will need to add the template as a suggestion in a preprocess function or you can create a custom theme function for the form, that will use the desired template. This is much similar to how it was done in Drupal 6, only you can do form_alters in your theme now.
you can add this function to your template.php:
you can print_r() your form and see what are the fields.
function yourThemeName_form_yourFormID_alter(&$form, &$form_state, $form_id) {
// Modification for the form with the given form ID goes here. For example, if
// FORM_ID is "user_register_form" this code would run only on the user
// registration form.
print_r($form);
}
You are adding tpl file for node form. For this you have to define tpl file name in template.php under your theme.
First create your_theme_name_theme()
Print content on tpl form.
Example: In below example contact_site_form is the form id of content type.
your_theme_name_theme() {
$items['contact_site_form'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'your_theme_name') . '/templates',
'template' => 'contact-site-form',
);
}
now create a file contact-site-form.tpl.php in your path location as specify above.
<?php echo render($form['name']); ?>
<?php echo render($form['mail']); ?>
.
.
.
<?php print drupal_render_children($form); ?>
You can render each element separately in given method.

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.

Resources