View source
<?php
namespace Drupal\node_title_validation\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
class NodeTitleValidationAdminForm extends FormBase {
protected $entityTypeManager;
protected $configFactory;
public function __construct(EntityTypeManager $entityTypeManager, ConfigFactoryInterface $config_factory) {
$this->entityTypeManager = $entityTypeManager;
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('config.factory'));
}
public function getFormId() {
return 'node_title_validation_admin_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$node_title_validation_config = $this
->config('node_title_validation.node_title_validation_settings')
->get('node_title_validation_config');
$node_types = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
$i = 0;
foreach ($node_types as $type) {
if ($i == 0) {
$form[$type
->get('type')] = [
'#type' => 'fieldset',
'#title' => $type
->get('name'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
];
}
else {
$form[$type
->get('type')] = [
'#type' => 'fieldset',
'#title' => $type
->get('name'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
}
$i++;
$form[$type
->get('type')]['exclude-' . $type
->get('type')] = [
'#type' => 'textarea',
'#default_value' => isset($node_title_validation_config['exclude-' . $type
->get('type')]) ? $node_title_validation_config['exclude-' . $type
->get('type')] : '',
'#title' => $this
->t('Blacklist Characters/Words'),
'#description' => '<p>' . $this
->t("Comma separated characters or words to avoided while saving node title. Ex: !,@,#,\$,%,^,&,*,(,),1,2,3,4,5,6,7,8,9,0,have,has,were,aren't.") . '</p>' . '<p>' . $this
->t('If any of the blacklisted characters/words found in node title,would return validation error on node save.') . '</p>',
];
$form[$type
->get('type')]['comma-' . $type
->get('type')] = [
'#type' => 'checkbox',
'#default_value' => isset($node_title_validation_config['comma-' . $type
->get('type')]) ? $node_title_validation_config['comma-' . $type
->get('type')] : '',
'#title' => $this
->t('Add comma to blacklist.'),
];
$form[$type
->get('type')]['min-' . $type
->get('type')] = [
'#type' => 'number',
'#title' => $this
->t("Minimum characters"),
'#required' => TRUE,
'#description' => $this
->t("Minimum number of characters node title should contain"),
'#max' => 255,
'#min' => 1,
'#default_value' => isset($node_title_validation_config['min-' . $type
->get('type')]) ? $node_title_validation_config['min-' . $type
->get('type')] : 1,
];
$form[$type
->get('type')]['max-' . $type
->get('type')] = [
'#type' => 'number',
'#title' => $this
->t("Maximum characters"),
'#required' => TRUE,
'#description' => $this
->t("Maximum number of characters node title should contain"),
'#max' => 255,
'#min' => 1,
'#default_value' => isset($node_title_validation_config['max-' . $type
->get('type')]) ? $node_title_validation_config['max-' . $type
->get('type')] : 255,
];
$form[$type
->get('type')]['min-wc-' . $type
->get('type')] = [
'#type' => 'number',
'#title' => $this
->t("Minimum Word Count"),
'#required' => TRUE,
'#description' => $this
->t("Minimum number of words node title should contain"),
'#max' => 20,
'#min' => 1,
'#default_value' => isset($node_title_validation_config['min-wc-' . $type
->get('type')]) ? $node_title_validation_config['min-wc-' . $type
->get('type')] : 1,
];
$form[$type
->get('type')]['max-wc-' . $type
->get('type')] = [
'#type' => 'number',
'#title' => $this
->t("Maximum Word Count"),
'#description' => $this
->t("Maximum number of words node title should contain"),
'#max' => 25,
'#min' => 1,
'#default_value' => isset($node_title_validation_config['max-wc-' . $type
->get('type')]) ? $node_title_validation_config['max-wc-' . $type
->get('type')] : 25,
];
$form[$type
->get('type')]['unique-' . $type
->get('type')] = [
'#type' => 'checkbox',
'#title' => $this
->t("Unique node title for @type content type", [
'@type' => $type
->get('type'),
]),
'#default_value' => isset($node_title_validation_config['unique-' . $type
->get('type')]) ? $node_title_validation_config['unique-' . $type
->get('type')] : 0,
];
}
$form['unique'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Unique node title for all content types'),
'#default_value' => isset($node_title_validation_config['unique']) ? $node_title_validation_config['unique'] : 0,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$node_types = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
foreach ($node_types as $type) {
$max = $form_state
->getValue([
'max-' . $type
->get('type'),
]);
$min = $form_state
->getValue([
'min-' . $type
->get('type'),
]);
if ($min > $max) {
$form_state
->setErrorByName('min-' . $type
->get('type'), $this
->t("Minimum length should not be more than Max length"));
}
$min_wc = $form_state
->getValue('min-wc-' . $type
->get('type'));
$max_wc = $form_state
->getValue([
'max-wc-' . $type
->get('type'),
]);
if (!empty($min_wc) && !empty($max_wc) && $min_wc > $max_wc) {
$form_state
->setErrorByName('max-wc-' . $type
->get('type'), $this
->t("Minimum word count of title should not be more than Maximum word count"));
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = [];
$node_types = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
foreach ($node_types as $type) {
$values['comma-' . $type
->get('type')] = $form_state
->getValue([
'comma-' . $type
->get('type'),
]);
$values['exclude-' . $type
->get('type')] = $form_state
->getValue([
'exclude-' . $type
->get('type'),
]);
$values['min-' . $type
->get('type')] = $form_state
->getValue([
'min-' . $type
->get('type'),
]);
$values['max-' . $type
->get('type')] = $form_state
->getValue([
'max-' . $type
->get('type'),
]);
$values['min-wc-' . $type
->get('type')] = $form_state
->getValue([
'min-wc-' . $type
->get('type'),
]);
$values['max-wc-' . $type
->get('type')] = $form_state
->getValue([
'max-wc-' . $type
->get('type'),
]);
$values['unique-' . $type
->get('type')] = $form_state
->getValue([
'unique-' . $type
->get('type'),
]);
}
$values['unique'] = $form_state
->getValue([
'unique',
]);
$this->configFactory
->getEditable('node_title_validation.node_title_validation_settings')
->set('node_title_validation_config', $values)
->save();
$this->messenger
->addMessage($this
->t('Node Title Validation Configurations saved successfully!'));
}
}