How to insert this javascript code inside my template.tpl.php to modify the comment form? (function XX_comment_form DRUPAL)

StackOverflow https://stackoverflow.com/questions/8099806

سؤال

I´ve got a Drupal site and I need to limit comments length. So, I´ve found this code:

Inside ghead I should add this:

<script language=”javascript”>
function limit(what,chars,counter) {
if (what.value.length > chars) {
what.value=what.value.substr(0,chars);
alert(‘You exceed to ‘ + chars + ‘chars!’);
}
counting = (chars – what.value.length);
c = document.getElementById(counter);
c.innerHTML = counting;
}
</script>

And I should add this stuff where I put the comment body:

<p><label for=”text”><strong>Text</strong></label> ¦ Chars left: <span id=”count1″>500</span></p>
<textarea name=”[1][2][t]” rows=”10″ cols=”50″ onkeyup=”limit(this,500,’count1′);” onkeydown=”limit(this,500,’count1′);” onblur=”limit(this,500,’count1′);” onfocus=”limit(this,500,’count1′);”></textarea>

My problem is that I have no idea on where to put this actually. I think this goes inside template.tpl.php file.

I´ve found a custom example, but don´t know how to insert the code above:

/**
* Theme the output of the comment_form.
*
* @param $form
*   The form that  is to be themed.
*/
function mytheme_comment_form($form) {

  // Rename some of the form element labels.
  $form['name']['#title'] = t('Name');
  $form['homepage']['#title'] = t('Website');
  $form['comment_filter']['comment']['#title']  = t('Your message');

  // Add some help text to the homepage field.
  $form['homepage']['#description'] = t('If you have your own website, enter its address here and we will link to it for you. (please include http://).');
  $form['homepage']['#description'] .= '<br/>'. t('eg. http://www.kirkdesigns.co.uk');

  // Remove the preview button
   $form['preview'] = NULL;

  return drupal_render($form);
}

Hope anyone can help me out here, because I´m pretty much clueless.

Thanks!

Rosamunda

هل كانت مفيدة؟

المحلول

Put your javascript code to comment.tpl.php or other template file. And in mytheme_comment_form($form) function add attributes to your textarea:

$form['textareaname']['#attributes'] = array('onkeyup' => 'limit(this,500,’count1′)';
$form['textareaname']['#title'] = "<label for=”text”><strong>Text</strong></label> ¦ Chars left: <span id=”count1″>500</span>"
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top