Form constructor for the text format add/edit form.
$format: A format object having the properties:
filter_admin_format_form_validate()
filter_admin_format_form_submit()
function filter_admin_format_form($form, &$form_state, $format) {
$is_fallback = $format->format == filter_fallback_format();
$form['#format'] = $format;
$form['#tree'] = TRUE;
$form['#attached']['library'][] = array(
'filter',
'drupal.filter.admin',
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $format->name,
'#required' => TRUE,
'#weight' => -30,
);
$form['format'] = array(
'#type' => 'machine_name',
'#required' => TRUE,
'#default_value' => $format->format,
'#maxlength' => 255,
'#machine_name' => array(
'exists' => 'filter_format_exists',
'source' => array(
'name',
),
),
'#disabled' => !empty($format->format),
'#weight' => -20,
);
// @todo Remove once moved to FilterFormatFormController.
$form['langcode'] = array(
'#type' => 'value',
'#value' => !$format
->isNew() ? $format->langcode : language_default()->langcode,
);
// Add user role access selection.
$form['roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Roles'),
'#options' => array_map('check_plain', user_role_names()),
'#disabled' => $is_fallback,
'#weight' => -10,
);
if ($is_fallback) {
$form['roles']['#description'] = t('All roles for this text format must be enabled and cannot be changed.');
}
if (!empty($format->format)) {
// If editing an existing text format, pre-select its current permissions.
$form['roles']['#default_value'] = array_keys(filter_get_roles_by_format($format));
}
elseif ($admin_role = config('user.settings')
->get('admin_role')) {
// If adding a new text format and the site has an administrative role,
// pre-select that role so as to grant administrators access to the new
// text format permission by default.
$form['roles']['#default_value'] = array(
$admin_role,
);
}
// Create filter plugin instances for all available filters, including both
// enabled/configured ones as well as new and not yet unconfigured ones.
$filters = $format
->filters()
->sort();
// Filter status.
$form['filters']['status'] = array(
'#type' => 'item',
'#title' => t('Enabled filters'),
'#prefix' => '<div id="filters-status-wrapper">',
'#suffix' => '</div>',
// This item is used as a pure wrapping container with heading. Ignore its
// value, since 'filters' should only contain filter definitions.
// @see http://drupal.org/node/1829202
'#input' => FALSE,
);
// Filter order (tabledrag).
$form['filters']['order'] = array(
'#type' => 'table',
// For filter.admin.js
'#attributes' => array(
'id' => 'filter-order',
),
'#title' => t('Filter processing order'),
'#tabledrag' => array(
array(
'order',
'sibling',
'filter-order-weight',
),
),
'#tree' => FALSE,
'#input' => FALSE,
'#theme_wrappers' => array(
'form_element',
),
);
// Filter settings.
$form['filter_settings'] = array(
'#type' => 'vertical_tabs',
'#title' => t('Filter settings'),
);
foreach ($filters as $name => $filter) {
$form['filters']['status'][$name] = array(
'#type' => 'checkbox',
'#title' => $filter
->getLabel(),
'#default_value' => $filter->status,
'#parents' => array(
'filters',
$name,
'status',
),
'#description' => $filter
->getDescription(),
'#weight' => $filter->weight,
);
$form['filters']['order'][$name]['#attributes']['class'][] = 'draggable';
$form['filters']['order'][$name]['#weight'] = $filter->weight;
$form['filters']['order'][$name]['filter'] = array(
'#markup' => $filter
->getLabel(),
);
$form['filters']['order'][$name]['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight for @title', array(
'@title' => $filter
->getLabel(),
)),
'#title_display' => 'invisible',
'#delta' => 50,
'#default_value' => $filter->weight,
'#parents' => array(
'filters',
$name,
'weight',
),
'#attributes' => array(
'class' => array(
'filter-order-weight',
),
),
);
// Retrieve the settings form of the filter plugin. The plugin should not be
// aware of the text format. Therefore, it only receives a set of minimal
// base properties to allow advanced implementations to work.
$settings_form = array(
'#parents' => array(
'filters',
$name,
'settings',
),
'#tree' => TRUE,
);
$settings_form = $filter
->settingsForm($settings_form, $form_state);
if (!empty($settings_form)) {
$form['filters']['settings'][$name] = array(
'#type' => 'details',
'#title' => $filter
->getLabel(),
'#weight' => $filter->weight,
'#parents' => array(
'filters',
$name,
'settings',
),
'#group' => 'filter_settings',
);
$form['filters']['settings'][$name] += $settings_form;
}
}
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
return $form;
}