You are here

function node_title_validation_node_validate in Node Title Validation 7

Implements hook_node_validate().

File

./node_title_validation.module, line 57
Node title validation module file.

Code

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,
      )));
    }
  }
}