You are here

function toc_js_per_node_form_node_form_alter in Toc.js 8

Same name and namespace in other branches
  1. 2.0.x modules/toc_js_per_node/toc_js_per_node.module \toc_js_per_node_form_node_form_alter()

Implements hook_form_BASE_FORM_ID_alter().

This module adds a simple checkbox to the node form labeled social_share. If the checkox is unchecked then the social share inks are not displayed for this node.

File

modules/toc_js_per_node/toc_js_per_node.module, line 125
Contains toc_js_per_node.module.

Code

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

  /** @var \Drupal\Node\NodeInterface $node */
  $node = $form_state
    ->getFormObject()
    ->getEntity();

  /** @var \Drupal\node\NodeTypeInterface $node_type */
  $node_type = $node->type->entity;
  $toc_override = $node_type
    ->getThirdPartySetting('toc_js_per_node', 'override', 0);
  $toc_override_default = $node_type
    ->getThirdPartySetting('toc_js_per_node', 'override_default', 1);

  // Default value for displaying the Toc from the node type.
  $toc_active = $node_type
    ->getThirdPartySetting('toc_js', 'toc_js_active', 0);

  // Use default override value if new or not set.
  if (NULL == $node
    ->id() || $node->toc_js_active->value == NULL) {
    $node->toc_js_active->value = $toc_override_default;
  }
  if ($toc_override && $toc_active) {

    // If a value has been set on the node, get it as the default value.
    if (isset($node->toc_js_active)) {
      if ($node->toc_js_active->value !== NULL) {
        $toc_active = $node->toc_js_active->value;
      }
    }
    if (\Drupal::currentUser()
      ->hasPermission('administer toc_js per node') || \Drupal::currentUser()
      ->hasPermission('administer nodes')) {
      $form['toc_js'] = [
        '#type' => 'details',
        '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
        '#title' => t('Table of contents'),
        '#weight' => 7,
        '#access' => TRUE,
      ];
      $form['toc_js']['toc_js_active'] = [
        '#type' => 'checkbox',
        '#title' => t('Display a table of contents'),
        '#attributes' => [
          'title' => t('When unchecked, the table of contents no longer displays.'),
        ],
        '#description' => t("Generates an automatic table of contents based on this node's content."),
        '#group' => 'toc_js',
        '#default_value' => $toc_active,
      ];
    }
    else {
      $form['toc_js_active'] = [
        '#type' => 'value',
        '#value' => $toc_active,
      ];
    }
  }
  else {
    $form['toc_js_active'] = [
      '#type' => 'value',
      '#value' => NULL,
    ];
  }
}