node_title_validation.module in Node Title Validation 7
Same filename and directory in other branches
Node title validation module file.
File
node_title_validation.moduleView source
<?php
/**
* @file
* Node title validation module file.
*/
/**
* Implements hook_help().
*/
function node_title_validation_help($section) {
$output = '';
switch ($section) {
case 'admin/help#node_title_validation':
$output = '<p>' . t('This module helps to validate node title with min/max characters,blacklist special characters,significant words and unique node titles .') . '</p>';
$output .= '<p>' . t('Validate the node title By:') . '</p>';
$output .= '<ul>';
$output .= '<li>' . t('Special Character & blacklisted words.') . '</li>';
$output .= '<li>' . t('Length (optionally specify min and/or max length.)') . '</li>';
$output .= '<li>' . t('Unique node title (for specific content type.)') . '</li>';
$output .= '</ul>';
break;
}
return $output;
}
/**
* Implements hook_menu().
*/
function node_title_validation_menu() {
$items = array();
$items['admin/config/content/node-title-validation'] = array(
'title' => 'Node title validation',
'description' => 'Validating node title',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'node_title_validation_admin_form',
),
'access arguments' => array(
'node title validation admin control',
),
'file' => 'node_title_validation.admin.inc',
);
return $items;
}
/**
* Implements hook_permission().
*/
function node_title_validation_permission() {
return array(
'node title validation admin control' => array(
'title' => t('Node title validation admin control'),
'description' => t('Perform administration tasks for the module.'),
),
);
}
/**
* Implements hook_node_validate().
*/
function node_title_validation_node_validate($node, $form, &$form_state) {
// Get configuration value.
$node_title_validation_config = variable_get('node_title_validation_config', array());
if ($node_title_validation_config) {
// Add a comma if comma is blacklist.
$exclude_comma = array();
if ($node_title_validation_config['comma-' . $node->type]) {
$exclude_comma[] = ',';
}
// Get exclude values for current content type.
$type_exclude = isset($node_title_validation_config['exclude-' . $node->type]) ? $node_title_validation_config['exclude-' . $node->type] : '';
if (!empty($type_exclude)) {
// Replace \r\n with comma.
$type_exclude = str_replace("\r\n", ',', $type_exclude);
// Store into array.
$type_exclude = explode(',', $type_exclude);
$type_exclude = array_merge($type_exclude, $exclude_comma);
// Find any exclude value found in node title.
$findings = _node_title_validation_search_excludes_in_title($node->title, $type_exclude);
if ($findings) {
form_set_error('title', t('The characters/words are not allowed to enter in the title. - @findings', array(
'@findings' => implode(',', $findings),
)));
}
}
// Validating minimum characters in the node title.
$type_min_chars = isset($node_title_validation_config['min-' . $node->type]) ? $node_title_validation_config['min-' . $node->type] : '';
if (!empty($type_min_chars)) {
if (drupal_strlen($node->title) < $type_min_chars) {
form_set_error('title', t("Title should have minimum @num characters", array(
'@num' => $type_min_chars,
)));
}
}
// Validating maximum characters in the node title.
$type_max_chars = isset($node_title_validation_config['max-' . $node->type]) ? $node_title_validation_config['max-' . $node->type] : '';
if (!empty($type_max_chars)) {
if (drupal_strlen($node->title) > $type_max_chars) {
form_set_error('title', t("Title should not exceed @num characters", array(
'@num' => $type_max_chars,
)));
}
}
// Validating Minimum Word Count in the Node Title.
$type_min_wc = isset($node_title_validation_config['min-wc-' . $node->type]) ? $node_title_validation_config['min-wc-' . $node->type] : '';
if (!empty($type_min_wc)) {
if (count(explode(' ', $node->title)) < $type_min_wc) {
form_set_error('title', t("Title should have minimum word count of @num", array(
'@num' => $type_min_wc,
)));
}
}
// Validating Maximum Word Count in the Node Title.
$type_max_wc = isset($node_title_validation_config['max-wc-' . $node->type]) ? $node_title_validation_config['max-wc-' . $node->type] : '';
if (!empty($type_max_wc)) {
if (count(explode(' ', $node->title)) > $type_max_wc) {
form_set_error('title', t("Title should not exceed word count of @num", array(
'@num' => $type_max_wc,
)));
}
}
// Validating Unique node titles.
$unique = $node_title_validation_config['unique-' . $node->type] || $node_title_validation_config['unique'];
$found_node_with_title = _node_title_validation_find_title_exist($node->title, $node->nid);
if ($unique && $found_node_with_title) {
form_set_error('title', t("The title must be unique. Other content is already using this title: @title", array(
'@title' => $node->title,
)));
}
}
}
/**
* Helper function to find any exclude values in node title.
*/
function _node_title_validation_search_excludes_in_title($input, $find) {
$findings = array();
// Finding characters in the node title.
foreach ($find as $char) {
// Check for single character.
if (drupal_strlen($char) == 1) {
if (strpos($input, trim($char)) !== FALSE) {
$findings[] = trim($char);
}
}
}
// Finding words in the node title.
$words = str_word_count($input, 1);
foreach ($words as $word) {
if (in_array($word, $find)) {
$findings[] = $word;
}
}
return $findings;
}
/**
* Helper function to find node title exist in the node table.
*/
function _node_title_validation_find_title_exist($title, $nid) {
$result = db_select('node', 'n')
->fields('n', array(
'nid',
))
->condition('title', $title, '=')
->execute();
$node_id = $result
->fetchField();
if ($nid == $node_id) {
return FALSE;
}
else {
return $result
->rowCount();
}
}
Functions
Name | Description |
---|---|
node_title_validation_help | Implements hook_help(). |
node_title_validation_menu | Implements hook_menu(). |
node_title_validation_node_validate | Implements hook_node_validate(). |
node_title_validation_permission | Implements hook_permission(). |
_node_title_validation_find_title_exist | Helper function to find node title exist in the node table. |
_node_title_validation_search_excludes_in_title | Helper function to find any exclude values in node title. |