You are here

function toc_js_form_node_type_form_alter in Toc.js 8

Same name and namespace in other branches
  1. 2.0.x toc_js.module \toc_js_form_node_type_form_alter()

Implements hook_form_FORM_ID_alter().

File

./toc_js.module, line 160
Contains toc_js.module.

Code

function toc_js_form_node_type_form_alter(&$form, FormStateInterface $form_state) {

  /** @var \Drupal\node\NodeTypeInterface $type */
  $type = $form_state
    ->getFormObject()
    ->getEntity();
  $form['toc_js'] = [
    '#type' => 'details',
    '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
    '#title' => t('Table of content'),
    '#access' => \Drupal::currentUser()
      ->hasPermission('administer toc_js') || \Drupal::currentUser()
      ->hasPermission('administer nodes'),
  ];
  $form['toc_js']['toc_js_active'] = [
    '#type' => 'checkbox',
    '#title' => t('Enable Table of content'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'toc_js_active', 0),
  ];
  $form['toc_js']['title'] = [
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#description' => t('the title to use for the table of contents.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'title', 'Table of contents'),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['selectors'] = [
    '#type' => 'textfield',
    '#title' => t('Selectors'),
    '#description' => t('Elements to use as headings. Each element separated by comma.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'selectors', 'h2, h3'),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['selectors_minimum'] = [
    '#type' => 'number',
    '#title' => t('Minimum elements'),
    '#description' => t('Set a minimum of elements to display the toc. Set 0 to always display the TOC.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'selectors_minimum', 0),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['container'] = [
    '#type' => 'textfield',
    '#title' => t('Container'),
    '#description' => t('Element to find all selectors in.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'container', '.node'),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['list_type'] = [
    '#type' => 'select',
    '#title' => t('List type'),
    '#description' => t('Select the list type to use.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'list_type', 'ul'),
    '#options' => [
      'ul' => t('Unordered HTML list (ul)'),
      'ol' => t('Ordered HTML list (ol)'),
    ],
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['prefix'] = [
    '#type' => 'textfield',
    '#title' => t('Prefix'),
    '#description' => t('Prefix for anchor tags and class names.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'prefix', 'toc'),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['back_to_top'] = [
    '#type' => 'checkbox',
    '#title' => t('Back to top link'),
    '#description' => t('Add a back to top link on heading.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'back_to_top', 0),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['back_to_top_label'] = [
    '#type' => 'textfield',
    '#title' => t('Back to top label'),
    '#description' => t('The back to top link label.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'back_to_top_label', 'Back to top'),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
        ':input[name="back_to_top"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['smooth_scrolling'] = [
    '#type' => 'checkbox',
    '#title' => t('Smooth scrolling'),
    '#description' => t('Enable or disable smooth scrolling on click.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'smooth_scrolling', 1),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['scroll_to_offset'] = [
    '#type' => 'number',
    '#title' => t('ScrollTo offset'),
    '#description' => t('Offset to trigger with smooth scrolling enabled.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'scroll_to_offset', 0),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
        ':input[name="smooth_scrolling"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['highlight_on_scroll'] = [
    '#type' => 'checkbox',
    '#title' => t('Highlight on scroll'),
    '#description' => t('Add class to heading that is currently in focus.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'highlight_on_scroll', 1),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['highlight_offset'] = [
    '#type' => 'number',
    '#title' => t('Highlight offset'),
    '#description' => t('Offset to trigger the next headline.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'highlight_offset', 100),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
        ':input[name="highlight_on_scroll"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['sticky'] = [
    '#type' => 'checkbox',
    '#title' => t('Sticky'),
    '#description' => t('Stick the toc on window scroll.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'sticky', 0),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['sticky_offset'] = [
    '#type' => 'number',
    '#title' => t('Sticky offset'),
    '#description' => t('The offset (in px) to apply when the toc is sticky.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'sticky_offset', 0),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
        ':input[name="sticky"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['sticky_stop'] = [
    '#type' => 'textfield',
    '#title' => t('Sticky Stop'),
    '#description' => t('Selector to use as a sticky stop. Leave empty if you don\'t need a stop for the sticky toc.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'sticky_stop', ''),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
        ':input[name="sticky"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['toc_js']['sticky_stop_padding'] = [
    '#type' => 'number',
    '#title' => t('Sticky Stop padding (px)'),
    '#description' => t('The padding to apply before the sticky stop (in pixels).'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_js', 'sticky_stop_padding', 0),
    '#states' => [
      'visible' => [
        ':input[name="toc_js_active"]' => [
          'checked' => TRUE,
        ],
        ':input[name="sticky"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['#entity_builders'][] = 'toc_js_form_node_type_form_builder';
}