You are here

function maxlength_form_field_ui_field_edit_form_alter in Maxlength 7.3

Implements hook_form_FORM_ID_alter().

File

./maxlength.module, line 64
Limit the number of characters in textfields and textareas and shows the amount of characters left.

Code

function maxlength_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {

  // Add an optional maxlength for teaser field.
  if ($form['#instance']['widget']['type'] == 'text_textarea_with_summary') {
    $form['instance']['widget']['settings']['maxlength_js_summary'] = array(
      '#type' => 'textfield',
      '#title' => 'Summary Maxlength JS',
      '#description' => t('The maximum length of the field in characters.'),
      '#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js_summary']) ? $form['#instance']['widget']['settings']['maxlength_js_summary'] : NULL,
    );
    $form['instance']['widget']['settings']['maxlength_js_label_summary'] = array(
      '#type' => 'textarea',
      '#rows' => 2,
      '#title' => t('Summary count down message'),
      '#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js_label_summary']) ? $form['#instance']['widget']['settings']['maxlength_js_label_summary'] : MAXLENGTH_DEFAULT_JS_LABEL,
      '#description' => t('The text used in the Javascript message under the input, where "@limit", "@remaining" and "@count" are replaced by the appropriate numbers.'),
    );
  }

  // Add settings for textarea widgets.
  $fields = array(
    'text_textarea_with_summary',
    'text_textarea',
  );
  if (in_array($form['#instance']['widget']['type'], $fields)) {
    $form['instance']['widget']['settings']['maxlength_js'] = array(
      '#type' => 'textfield',
      '#title' => 'Maxlength JS',
      '#description' => t('The maximum length of the field in characters.'),
      '#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js']) ? $form['#instance']['widget']['settings']['maxlength_js'] : NULL,
    );
    $form['instance']['widget']['settings']['maxlength_js_enforce'] = array(
      '#type' => 'checkbox',
      '#title' => t('Force text truncate'),
      '#description' => t('Check this option if you want that the html (or the text) that the user inserts into the field to be truncated.'),
      '#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js_enforce']) ? $form['#instance']['widget']['settings']['maxlength_js_enforce'] : NULL,
    );
    $form['instance']['widget']['settings']['maxlength_js_truncate_html'] = array(
      '#type' => 'checkbox',
      '#title' => t('Truncate html'),
      '#description' => t('Check this option if the input field may contain html text and you want to truncate it safely. This will also overwrite the maxlength validation from core, so that it will strip the tags before checking the length.'),
      '#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js_truncate_html']) ? $form['#instance']['widget']['settings']['maxlength_js_truncate_html'] : NULL,
      '#states' => array(
        'enabled' => array(
          ':input[id=edit-instance-widget-settings-maxlength-js-cut-text]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
  }

  // Add settings for textfield widgets.
  $fields = array(
    'text_textfield',
  );
  if (in_array($form['#instance']['widget']['type'], $fields)) {
    $form['instance']['widget']['settings']['maxlength_js'] = array(
      '#type' => 'checkbox',
      '#title' => 'Maxlength JS',
      '#description' => t('Limit the maximum length of the field in characters using the "<strong>Maximum length</strong>" value set in the <strong>field settings</strong> using Javascript.'),
      '#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js']) ? $form['#instance']['widget']['settings']['maxlength_js'] : NULL,
    );
  }

  // Add settings for the maxlength widget label, only for allowed fields.
  $fields = array(
    'text_textfield',
    'text_textarea_with_summary',
    'text_textarea',
  );
  if (in_array($form['#instance']['widget']['type'], $fields)) {
    $form['instance']['widget']['settings']['maxlength_js_label'] = array(
      '#type' => 'textarea',
      '#rows' => 2,
      '#title' => t('Count down message'),
      '#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js_label']) ? $form['#instance']['widget']['settings']['maxlength_js_label'] : MAXLENGTH_DEFAULT_JS_LABEL,
      '#description' => t('The text used in the Javascript message under the input, where "@limit", "@remaining" and "@count" are replaced by the appropriate numbers.'),
    );
  }
}