public function TocTypeForm::buildForm in TOC API 8
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 EntityForm::buildForm
File
- src/
TocTypeForm.php, line 79
Class
- TocTypeForm
- Base form for TOC type add and edit forms.
Namespace
Drupal\toc_apiCode
public function buildForm(array $form, FormStateInterface $form_state) {
$toc_type = $this->entity;
$options = $toc_type
->getOptions();
// An associative array of HTML header tags keyed by level.
$header_options = [
1 => 'h1',
2 => 'h2',
3 => 'h3',
4 => 'h4',
5 => 'h5',
6 => 'h6',
];
// An associative array of HTML list style types used for numbering.
$numbering_options = [
'decimal' => 'decimal (1, 2, 3...)',
'lower-alpha' => 'lower-alpha (a, b, c...)',
'upper-alpha' => 'upper-alpha (A, B, C...)',
'lower-roman' => 'lower-roman (i, ii, iii...)',
'upper-roman' => 'upper-roman (I, II, III...)',
'circle' => 'circle',
'disc' => 'disc',
'square' => 'square',
'none' => 'none',
];
$form = parent::buildForm($form, $form_state);
$form['#attached']['library'][] = 'toc_api/toc_type';
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $toc_type
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $toc_type
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\toc_api\\Entity\\TocType::load',
],
'#disabled' => !$toc_type
->isNew(),
];
$form['options'] = [
'#type' => 'container',
];
$form['options']['general'] = [
'#type' => 'details',
'#title' => $this
->t('General settings'),
'#open' => TRUE,
'#tree' => TRUE,
];
$form['options']['general']['template'] = [
'#title' => $this
->t('Table of contents type'),
'#type' => 'select',
'#options' => $this
->getTemplates(),
'#default_value' => $options['template'],
];
$form['options']['general']['title'] = [
'#title' => $this
->t('Table of contents title'),
'#type' => 'textfield',
'#default_value' => $options['title'],
];
// Hide block option since it is up to TOC submodule to decide how to
// support it.
$form['options']['general']['block'] = [
'#title' => $this
->t('Display table of contents in a block.'),
'#type' => 'checkbox',
'#default_value' => $options['block'],
'#access' => FALSE,
];
$form['options']['header'] = [
'#type' => 'details',
'#title' => $this
->t('Header settings'),
'#open' => TRUE,
'#tree' => TRUE,
];
$form['options']['header']['header_count'] = [
'#title' => t('Number of headers required to generate a table of contents'),
'#type' => 'number',
'#size' => 10,
'#maxlength' => 10,
'#default_value' => $options['header_count'],
];
$form['options']['header']['header_min'] = [
'#title' => $this
->t('Header minimum level'),
'#type' => 'select',
'#options' => $header_options,
'#default_value' => $options['header_min'],
'#attributes' => [
'class' => [
'js-toc-type-options-header-min',
],
],
];
$form['options']['header']['header_max'] = [
'#title' => $this
->t('Header maximum level'),
'#type' => 'select',
'#options' => $header_options,
'#default_value' => $options['header_max'],
'#attributes' => [
'class' => [
'js-toc-type-options-header-max',
],
],
];
$form['options']['header']['header_allowed_tags'] = [
'#title' => $this
->t('Header allowed tags'),
'#type' => 'textfield',
'#default_value' => $options['header_allowed_tags'],
];
$form['options']['header']['header_id'] = [
'#title' => $this
->t('Header id type'),
'#type' => 'select',
'#options' => [
'title' => 'title',
'key' => 'key',
'number_path' => 'number_path',
],
'#default_value' => $options['header_id'],
];
$form['options']['header']['header_id_prefix'] = [
'#title' => $this
->t('Header id prefix'),
'#type' => 'textfield',
'#default_value' => $options['header_id_prefix'],
];
$form['options']['top'] = [
'#type' => 'details',
'#title' => $this
->t('Back to top settings'),
'#open' => TRUE,
'#tree' => TRUE,
];
$form['options']['top']['top_min'] = [
'#title' => $this
->t('Back to top minimum level'),
'#type' => 'select',
'#options' => $header_options,
'#default_value' => $options['top_min'],
];
$form['options']['top']['top_max'] = [
'#title' => $this
->t('Back to top maximum level'),
'#type' => 'select',
'#options' => $header_options,
'#default_value' => $options['top_max'],
];
$form['options']['top']['top_label'] = [
'#title' => $this
->t('Back to top label'),
'#type' => 'textfield',
'#default_value' => $options['top_label'],
];
$form['options']['numbering'] = [
'#type' => 'details',
'#title' => $this
->t('Numbering settings'),
'#open' => TRUE,
'#tree' => TRUE,
];
$form['options']['numbering']['default'] = [];
$form['options']['numbering']['default']['number_type'] = [
'#title' => $this
->t('Numbering type'),
'#type' => 'select',
'#options' => $numbering_options,
'#default_value' => $options['default']['number_type'],
];
$form['options']['numbering']['default']['number_prefix'] = [
'#title' => $this
->t('Numbering prefix'),
'#type' => 'textfield',
'#size' => 10,
'#maxlength' => 10,
'#default_value' => $options['default']['number_prefix'],
];
$form['options']['numbering']['default']['number_suffix'] = [
'#title' => $this
->t('Numbering suffix'),
'#type' => 'textfield',
'#size' => 10,
'#maxlength' => 10,
'#default_value' => $options['default']['number_suffix'],
];
$form['options']['numbering']['number_path_separator'] = [
'#title' => $this
->t('Numbering separator'),
'#type' => 'textfield',
'#size' => 10,
'#maxlength' => 10,
'#default_value' => $options['number_path_separator'],
];
$form['options']['numbering']['number_path'] = [
'#title' => $this
->t('Display entire numbering path in each header.'),
'#type' => 'checkbox',
'#default_value' => $options['number_path'],
];
$form['options']['numbering']['number_path_truncate'] = [
'#title' => $this
->t('Truncate the numbering path to only display parents.'),
'#type' => 'checkbox',
'#default_value' => $options['number_path'],
];
foreach ($header_options as $header_tag) {
$header_options = isset($options['headers'][$header_tag]) ? $options['headers'][$header_tag] : [];
$header_options += [
'custom' => $header_options ? TRUE : FALSE,
'number_type' => '',
'number_prefix' => '',
'number_suffix' => '',
];
$states = [
'invisible' => [
".js-toc-type-options-headers-{$header_tag}-custom" => [
'checked' => FALSE,
],
],
];
$form['options']['numbering']['headers'][$header_tag] = [
'#type' => 'details',
'#title' => $header_tag,
'#open' => $header_options['custom'],
'#attributes' => [
'class' => [
"js-toc-type-options-headers-{$header_tag}",
],
],
];
$form['options']['numbering']['headers'][$header_tag]['custom'] = [
'#title' => $this
->t('Customize @tag numbering', [
'@tag' => $header_tag,
]),
'#type' => 'checkbox',
'#default_value' => $header_options['custom'],
'#attributes' => [
'class' => [
"js-toc-type-options-headers-{$header_tag}-custom",
],
],
];
$form['options']['numbering']['headers'][$header_tag]['number_type'] = [
'#title' => $this
->t('Numbering type'),
'#type' => 'select',
'#options' => $numbering_options,
'#default_value' => $header_options['number_type'],
'#states' => $states,
'#attributes' => [
'class' => [
"js-toc-type-options-headers-{$header_tag}-number-type",
],
],
];
$form['options']['numbering']['headers'][$header_tag]['number_prefix'] = [
'#title' => $this
->t('Numbering prefix'),
'#type' => 'textfield',
'#size' => 10,
'#maxlength' => 10,
'#default_value' => $header_options['number_prefix'],
'#states' => $states,
'#attributes' => [
'class' => [
"js-toc-type-options-headers-{$header_tag}-number-prefix",
],
],
];
$form['options']['numbering']['headers'][$header_tag]['number_suffix'] = [
'#title' => $this
->t('Numbering suffix'),
'#type' => 'textfield',
'#size' => 10,
'#maxlength' => 10,
'#default_value' => $header_options['number_suffix'],
'#states' => $states,
'#attributes' => [
'class' => [
"js-toc-type-options-headers-{$header_tag}-number-suffix",
],
],
];
}
return $form;
}