Hi,

does anyone know how/where to set
Minimum number of charaters for title?

I dont want people to post page/story/forum topic
with too short title.

Thank you very much.

Comments

geodaniel’s picture

There's no way to do this out of the box, though it could be done through a simple module that checked the length of the text when a new node was created and threw an error if it wasn't long enough.

DriesK’s picture

Exactly. Here's some code that does what you want. Copy-paste the code in a new file, name it longtitle.module, put it in your modules directory and enable it.

You can set the number of characters in admin->settings->longtitle, and you can choose to which content types the validation applies in admin->settings->content types.

Yes, I was a little bored :)


function longtitle_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Allows to set a minimum title length');
  }
}

function longtitle_nodeapi(&$node, $op, $form = NULL, $page = NULL) {
  switch ($op) {
    case 'validate':
      if (strlen(trim($node->title)) < variable_get('node_title_length', 10) && variable_get('longtitle_'. $node->type, 1)) {
        form_set_error('title', t('Your title is too short'));
      }
      break;
  }
}

function longtitle_settings() {
  $form['node_title_length'] = array(
    '#type' => 'select',
    '#title' => t('Minimum title length'),
    '#default_value' => variable_get('node_title_length', 10),
    '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
    '#description' => t('Minimum length of node titles (in characters)'),
  );
  return $form;
}

function longtitle_form_alter($form_id, &$form) {
  if (isset($form['type'])) {
    if ($form['type']['#value'] .'_node_settings' == $form_id) {
      $form['workflow']['longtitle_'. $form['type']['#value']] = array(
        '#type' => 'radios',
        '#title' => t('Check title length'),
        '#default_value' => variable_get('longtitle_'. $form['type']['#value'], 1),
        '#options' => array(t('Disabled'), t('Enabled')),
      );
    }
  }
}

DriesK’s picture

I added this code to the handbook as a mini module.

winman1’s picture

thx dude DriesK a lot!

your nick ... looks like from Filandia? :)

silurius’s picture

This is excellent code, thanks. Have bookmarked it.

Is there anything equivalent for maximum title length? Particularly for uncustomized pages. I have a content admin on one site who wants to exceed what seems to be a 128 character limit (including spaces). I think 150 is about right, but I can't locate where this can be set in the settings or in the code. This is a 4.7 site that may get upgraded in the next few months.

Delta Bridges’s picture

Yes, it would be nice to ba able to set a maximum title lengh for some content type / views.
Very long titles can destroy the way my theme is supposed to look :)
Thanks,
Expat9