public function NodeTitleValidationAdminForm::buildForm in Node Title Validation 8
Same name and namespace in other branches
- 2.0.x src/Form/NodeTitleValidationAdminForm.php \Drupal\node_title_validation\Form\NodeTitleValidationAdminForm::buildForm()
- 1.0.x src/Form/NodeTitleValidationAdminForm.php \Drupal\node_title_validation\Form\NodeTitleValidationAdminForm::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- src/
Form/ NodeTitleValidationAdminForm.php, line 65
Class
- NodeTitleValidationAdminForm
- Class NodeTitleValidationAdminForm.
Namespace
Drupal\node_title_validation\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
// Get configuration value.
$node_title_validation_config = $this
->config('node_title_validation.node_title_validation_settings')
->get('node_title_validation_config');
// Get available content types.
$node_types = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
// Variable to display 1st fieldset collapse open.
$i = 0;
// Generate fieldset for each content type along with exclude, min,
// max and unique form elements.
foreach ($node_types as $type) {
// Display First fieldset collapsed open.
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,
];
}
// Increment $i for other fieldsets in collapsed closed.
$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;
}