function maxlength_field_widget_third_party_settings_form in Maxlength 8
Implements hook_field_widget_third_party_settings_form().
@todo: add the settings in the field schema and also test the edge values, like 0 or negative.
File
- ./
maxlength.module, line 48 - Limits the number of characters in textfields and textareas and shows the amount of characters left.
Code
function maxlength_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) {
$plugin_id = $plugin
->getPluginId();
// Depeding on the widget, we may have different settings.
$widget_settings = \Drupal::service('maxlength.widget_settings');
$allowed_settings = $widget_settings
->getAllowedSettings($plugin_id);
if (!empty($allowed_settings)) {
$element = array();
if (!empty($allowed_settings['maxlength_setting'])) {
$element['maxlength_js'] = array(
'#type' => 'number',
'#title' => t('Max length'),
'#description' => t('The maximum number of characters in the field.'),
'#default_value' => $plugin
->getThirdPartySetting('maxlength', 'maxlength_js'),
);
$element['maxlength_js_label'] = array(
'#type' => 'textarea',
'#rows' => 2,
'#title' => t('Count down message'),
'#default_value' => $plugin
->getThirdPartySetting('maxlength', 'maxlength_js_label', t('Content limited to @limit characters, remaining: <strong>@remaining</strong>')),
'#description' => t('The dynamic message under the input, where "@limit", "@remaining" and "@count" are replaced by the appropriate numbers.'),
);
}
if (!empty($allowed_settings['summary_maxlength_setting'])) {
$element['maxlength_js_summary'] = array(
'#type' => 'number',
'#title' => t('Summary max length'),
'#description' => t('The maximum number of characters in the field.'),
'#default_value' => $plugin
->getThirdPartySetting('maxlength', 'maxlength_js_summary'),
);
$element['maxlength_js_label_summary'] = array(
'#type' => 'textarea',
'#rows' => 2,
'#title' => t('Summary count down message'),
'#default_value' => $plugin
->getThirdPartySetting('maxlength', 'maxlength_js_label_summary', t('Content limited to @limit characters, remaining: <strong>@remaining</strong>')),
'#description' => t('The dynamic message under the input field, where "@limit", "@remaining" and "@count" are replaced by the appropriate numbers.'),
);
}
if (!empty($allowed_settings['truncate_setting'])) {
$element['maxlength_js_enforce'] = array(
'#type' => 'checkbox',
'#title' => t('Force truncate'),
'#description' => t('Check this option if you want the html (or the text) inserted into the field to be truncated.'),
'#default_value' => $plugin
->getThirdPartySetting('maxlength', 'maxlength_js_enforce'),
);
$element['maxlength_js_truncate_html'] = array(
'#type' => 'checkbox',
'#title' => t('Safe truncate html'),
'#description' => t('Use this option if the input field may contain html and you want to exclude the html tag characters from the character count. This will also overwrite the maxlength validation from core, so that it will strip the tags before checking the length.'),
'#default_value' => $plugin
->getThirdPartySetting('maxlength', 'maxlength_js_truncate_html'),
);
}
return $element;
}
}