You are here

node_title_validation.module in Node Title Validation 1.0.x

Node title validation module file.

File

node_title_validation.module
View source
<?php

/**
 * @file
 * Node title validation module file.
 */
use Drupal\Core\Entity\EntityTypeInterface;

/**
 * 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;
}

/**
 * Function for adding constraint.
 *
 * @param array $fields
 *   The fields array.
 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
 *   The entity type.
 */
function _node_title_validation_add_constraint(array &$fields, EntityTypeInterface $entity_type) {
  if ($entity_type
    ->id() == 'node' && !empty($fields['title'])) {
    $fields['title']
      ->addConstraint('NodeTitleValidate', []);
  }
}

/**
 * Implements hook_entity_base_field_info_alter().
 */
function node_title_validation_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  _node_title_validation_add_constraint($fields, $entity_type);
}

/**
 * Implements hook_entity_bundle_field_info_alter().
 */
function node_title_validation_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  _node_title_validation_add_constraint($fields, $entity_type);
}