You are here

function better_formats_field_settings_form in Better Formats 8

Same name and namespace in other branches
  1. 7 better_formats.module \better_formats_field_settings_form()

Build the settings form for Field API fields.

Parameters

array $bf_form: The existing better formats settings form from the form element.

Return value

array The array of better formats form items.

1 call to better_formats_field_settings_form()
better_formats_form_field_config_edit_form_alter in ./better_formats.module
Implements hook_form_FORM_ID_alter().

File

./better_formats.module, line 323
Enhances the core input format system by managing input format defaults and settings.

Code

function better_formats_field_settings_form($bf_form = []) {
  $formats = filter_formats();

  // Plain Text Format should not be an option, that is a separate field type.
  unset($formats['plain_text']);
  $form = [];
  $form['better_formats'] = [
    '#tree' => TRUE,
    '#type' => 'fieldset',
    '#title' => t('Text Formats'),
    '#weight' => 0,
  ];
  $allowed_options = [];
  foreach ($formats as $format) {
    $allowed_options[$format
      ->id()] = $format
      ->label();
  }
  $allowed_toggle_default = isset($bf_form['allowed_formats_toggle']) ? $bf_form['allowed_formats_toggle'] : FALSE;
  $allowed_defaults = isset($bf_form['allowed_formats']) ? $bf_form['allowed_formats'] : [];
  if (empty($allowed_defaults)) {
    $allowed_defaults = array_keys($allowed_options);
  }
  $form['better_formats']['allowed_formats_toggle'] = [
    '#type' => 'checkbox',
    '#title' => t('Limit allowed text formats'),
    '#description' => t('Check the allowed formats below. If checked available text formats can be chosen.'),
    '#weight' => 1,
    '#default_value' => $allowed_toggle_default,
  ];
  $form['better_formats']['allowed_formats'] = [
    '#type' => 'checkboxes',
    '#title' => t('Allowed formats'),
    '#options' => $allowed_options,
    '#description' => t('Select the text formats allowed for this field. Note that not all of these may appear on the form if a user does not have permission to use them. <strong>Warning:</strong> This affects existing content which may leave you unable to edit some fields. If that happens you must allow the format that field was saved in here.'),
    '#weight' => 2,
    '#default_value' => $allowed_defaults,
    '#states' => [
      'visible' => [
        ':input[name="third_party_settings[better_formats][allowed_formats_toggle]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $order_toggle_default = isset($bf_form['default_order_toggle']) ? $bf_form['default_order_toggle'] : FALSE;
  $form['better_formats']['default_order_toggle'] = [
    '#type' => 'checkbox',
    '#title' => t('Override default order'),
    '#description' => t('Override the global order that will determine the default text format a user will get <strong>only on entity creation</strong>.'),
    '#weight' => 3,
    '#default_value' => $order_toggle_default,
  ];
  $form['better_formats']['default_order_wrapper'] = [
    '#tree' => TRUE,
    '#type' => 'container',
    '#weight' => 4,
    '#states' => [
      'visible' => [
        ':input[name="third_party_settings[better_formats][default_order_toggle]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Formats ordering table.
  $form['better_formats']['default_order_wrapper']['formats'] = [
    '#type' => 'table',
    '#header' => [
      t('Format'),
      t('Weight'),
    ],
    '#empty' => t('There are no items yet.'),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'format-order-weight',
      ],
    ],
  ];
  foreach ($formats as $id => $format) {
    $default = isset($bf_form['default_order_wrapper']['formats'][$id]) ? $bf_form['default_order_wrapper']['formats'][$id] : NULL;
    $weight = isset($default['weight']) ? $default['weight'] : $format
      ->get('weight');

    // TableDrag: Mark the table row as draggable.
    $form['better_formats']['default_order_wrapper']['formats'][$id]['#attributes']['class'][] = 'draggable';

    // TableDrag: Sort the table row according to its existing/configured weight.
    $form['better_formats']['default_order_wrapper']['formats'][$id]['#weight'] = $weight;

    // Some table columns containing raw markup.
    $form['better_formats']['default_order_wrapper']['formats'][$id]['label'] = [
      '#markup' => $format
        ->label(),
    ];

    // TableDrag: Weight column element.
    $form['better_formats']['default_order_wrapper']['formats'][$id]['weight'] = [
      '#type' => 'weight',
      '#title' => t('Weight for @title', [
        '@title' => $format
          ->label(),
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $weight,
      // Classify the weight element for #tabledrag.
      '#attributes' => [
        'class' => [
          'format-order-weight',
        ],
      ],
    ];
  }

  // Sort formats according to weight.
  Element::children($form['better_formats']['default_order_wrapper']['formats'], TRUE);
  return $form;
}