You are here

toc_node.module in TOC Node 8

Same filename and directory in other branches
  1. 7 toc_node.module

This is the main module file for TOC Node.

File

toc_node.module
View source
<?php

/**
 * @file
 * This is the main module file for TOC Node.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\node;
use Drupal\Core\Entity\EntityTypeInterface;

/**
 * Implementation of hook_form_FORM_ID_alter().
 *
 * Adds TOC Node options to the node type form.
 *
 * @see NodeTypeForm::form().
 */
function toc_node_form_node_type_form_alter(&$form, FormStateInterface $form_state) {
  $type = $form_state
    ->getFormObject()
    ->getEntity();
  $form['toc_node'] = array(
    '#type' => 'details',
    '#title' => t('Table of contents'),
    '#description' => t('Settings for adding TOC to nodes of this content type.'),
    //'#access' => \Drupal::currentUser()->hasPermission('administer TOC Node'),
    '#group' => 'additional_settings',
  );
  $form['toc_node']['toc_node_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable table of contents'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_node', 'enabled', 0),
  );
  $form['toc_node']['toc_node_styles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Styles available'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_node', 'styles', array(
      'none',
      'bullets',
      'numbers',
    )),
    '#options' => array(
      'none' => t('No TOC'),
      'bullets' => t('Bullets'),
      'numbers' => t('Numbered'),
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="toc_node_enabled"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['toc_node']['toc_node_style_default'] = array(
    '#type' => 'select',
    '#title' => t('Style default'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_node', 'style_default', 'bullets'),
    '#empty_option' => t('- Select -'),
    '#options' => array(
      'none' => t('No TOC'),
      'bullets' => t('Bullets'),
      'numbers' => t('Numbered'),
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="toc_node_enabled"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['toc_node']['toc_node_level'] = array(
    '#type' => 'select',
    '#title' => t('Level'),
    '#description' => t('Up to what heading level to include.'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_node', 'level', 2),
    '#empty_option' => t('- Select -'),
    '#options' => array(
      2 => 'h2',
      3 => 'h3',
      4 => 'h4',
      5 => 'h5',
      6 => 'h6',
      7 => 'h7',
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="toc_node_enabled"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['toc_node']['toc_node_back_to_top_links'] = array(
    '#type' => 'select',
    '#title' => t('Back to top links'),
    '#default_value' => $type
      ->getThirdPartySetting('toc_node', 'back_to_top_links', 1),
    '#empty_option' => t('- Select -'),
    '#options' => array(
      t('Disabled - always'),
      t('Enabled - always'),
      t('Enabled - per node option'),
      t('Disabled - per node option'),
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="toc_node_enabled"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['#entity_builders'][] = 'toc_node_form_node_type_form_builder';
}

/**
 * Entity builder for the node type form with toc_node options.
 *
 */
function toc_node_form_node_type_form_builder($entity_type, NodeTypeInterface $type, &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getValue('toc_node_enabled')) {
    $type
      ->setThirdPartySetting('toc_node', 'enabled', $form_state
      ->getValue('toc_node_enabled'));
    $type
      ->setThirdPartySetting('toc_node', 'styles', array_values(array_filter($form_state
      ->getValue('toc_node_styles'))));
    $type
      ->setThirdPartySetting('toc_node', 'style_default', $form_state
      ->getValue('toc_node_style_default'));
    $type
      ->setThirdPartySetting('toc_node', 'level', $form_state
      ->getValue('toc_node_level'));
    $type
      ->setThirdPartySetting('toc_node', 'back_to_top_links', $form_state
      ->getValue('toc_node_back_to_top_links'));
    return;
  }
  $type
    ->unsetThirdPartySetting('toc_node', 'toc_node_enabled');
}

/**
 * Implements hook_entity_base_field_info().
 */
function toc_node_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = array();

  // Add a 'Highlight' base field to all node types.
  if ($entity_type
    ->id() === 'node') {
    $toc_enabled = $entity_type
      ->getThirdPartySetting('toc_node', 'enabled', 0);
    $style = $entity_type
      ->getThirdPartySetting('toc_node', 'style_default', 'bullets');
    $level = $entity_type
      ->getThirdPartySetting('toc_node', 'level', 2);
    $back_to_top_links_defaults = $entity_type
      ->getThirdPartySetting('toc_node', 'back_to_top_links', 1);
    $options_enabled = $entity_type
      ->getThirdPartySetting('toc_node', 'styles', array(
      'none',
      'bullets',
      'numbers',
    ));

    // Only show options if TOC enabled for this content type.
    if (empty($toc_enabled)) {
      return;
    }
    $options_all = array(
      'none' => t('No TOC'),
      'bullets' => t('Bullets'),
      'numbers' => t('Numbered'),
    );
    $options = array_intersect_key($options_all, array_flip($options_enabled));
    $fields['toc_node_style'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Highlight'))
      ->setDescription(t('Whether or not the node is highlighted.'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDisplayOptions('form', array(
      'type' => 'select',
      'settings' => array(
        'display_label' => TRUE,
      ),
    ))
      ->setDisplayConfigurable('form', TRUE);
  }
  return $fields;
}

/**
 * Implementation of hook_form_FORM_ID_alter().
 */
function toc_node_form_node_form_alter(&$form, FormStateInterface $form_state) {
  $node = $form_state
    ->getFormObject()
    ->getEntity();
  $node_type = $node->type->entity;
  $toc_enabled = $node_type
    ->getThirdPartySetting('toc_node', 'enabled', 0);
  $options_enabled = $node_type
    ->getThirdPartySetting('toc_node', 'styles', array(
    'none',
    'bullets',
    'numbers',
  ));
  $style = $node_type
    ->getThirdPartySetting('toc_node', 'style_default', 'bullets');
  $level = $node_type
    ->getThirdPartySetting('toc_node', 'level', 2);
  $back_to_top_links_defaults = $node_type
    ->getThirdPartySetting('toc_node', 'back_to_top_links', 1);

  // Only show options if TOC enabled for this content type.
  if (empty($toc_enabled)) {
    return;
  }
  $options_all = array(
    'none' => t('No TOC'),
    'bullets' => t('Bullets'),
    'numbers' => t('Numbered'),
  );
  $options = array_intersect_key($options_all, array_flip($options_enabled));
  if (isset($form['nid']['#value'])) {
    $node_settings = db_query('SELECT * FROM {toc_node} WHERE nid = :nid', array(
      ':nid' => $form['nid']['#value'],
    ))
      ->fetchAssoc();

    //$injected_database->query($query, $args, $options);
    if (!empty($node_settings) && isset($options[$node_settings['style']])) {
      $style = $node_settings['style'];
      $back_to_top_links = $node_settings['back_links'];
      $level = $node_settings['level'];
    }
  }
  $form['toc_node'] = array(
    '#type' => 'details',
    '#title' => t('Table of contents'),
    '#group' => 'advanced',
    '#element_validate' => array(
      'toc_node_entity_set_form_value',
    ),
    '#tree' => TRUE,
  );
  $form['toc_node']['toc_node_style'] = array(
    '#type' => 'select',
    '#title' => t('TOC Display'),
    '#default_value' => $style,
    '#empty_option' => t('- Select -'),
    '#options' => $options,
  );
  $form['toc_node']['toc_node_level'] = array(
    '#type' => 'select',
    '#title' => t('Level'),
    '#description' => t('Up to what heading level to include.'),
    '#default_value' => $level,
    '#empty_option' => t('- Select -'),
    '#options' => array(
      2 => 'h2',
      3 => 'h3',
      4 => 'h4',
      5 => 'h5',
      6 => 'h6',
      7 => 'h7',
    ),
  );

  // Check if back to top links should be on as default.
  if (!isset($back_to_top_links) && ($back_to_top_links_defaults == 1 || $back_to_top_links_defaults == 2)) {
    $back_to_top_links = 1;
  }
  elseif (!isset($back_to_top_links) && ($back_to_top_links_defaults == 0 || $back_to_top_links_defaults == 3)) {
    $back_to_top_links = 0;
  }
  $form['toc_node']['toc_node_back_to_top_links'] = array(
    '#type' => 'checkbox',
    '#title' => t('Back to top links'),
    '#default_value' => $back_to_top_links,
  );

  // Check if there is per node options.
  if ($back_to_top_links_defaults < 2) {
    $form['toc_node']['toc_node_back_to_top_links']['#access'] = FALSE;
  }
  foreach (array_keys($form['actions']) as $action) {
    if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
      $form['actions'][$action]['#submit'][] = 'toc_node_form_node_form_submit';
    }
  }
}
function toc_node_entity_set_form_value($element, FormStateInterface $form_state, $form) {
  $values = $form_state
    ->getValues();
  $build_info = $form_state
    ->getBuildInfo();
  $node = $build_info['callback_object']
    ->getEntity();
  $node->toc_node = $values['toc_node'];
}

/**
 * Form submission handler for TOC Node on the node form.
 *
 * @see toc_node_form_node_form_alter()
 */
function toc_node_form_node_form_submit($form, FormStateInterface $form_state) {
  $node = $form_state
    ->getFormObject()
    ->getEntity();
  if (!$form_state
    ->isValueEmpty('toc_node')) {
    $values = $form_state['values']['toc_node'];
  }
}

/**
 * Implements hook_node_update().
 */
function toc_node_node_update(node\NodeInterface $node) {
}

/**
 * Implements hook_node_insert().
 */
function toc_node_node_insert(node\NodeInterface $node) {
}

/**
 * Implements hook_node_delete().
 */
function toc_node_node_delete(node\NodeInterface $node) {
}
function toc_node_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {

  // Only do the extra work if the component is configured to be displayed.
  // This assumes a 'mymodule_addition' extra field has been defined for the
  // entity bundle in hook_entity_extra_field_info().
  //  if ($display->getComponent('mymodule_addition')) {
  //    $build['mymodule_addition'] = array(
  //      '#markup' => mymodule_addition($entity),
  //      '#theme' => 'mymodule_my_additional_field',
  //    );
  //  }
}

Functions

Namesort descending Description
toc_node_entity_base_field_info Implements hook_entity_base_field_info().
toc_node_entity_set_form_value
toc_node_form_node_form_alter Implementation of hook_form_FORM_ID_alter().
toc_node_form_node_form_submit Form submission handler for TOC Node on the node form.
toc_node_form_node_type_form_alter Implementation of hook_form_FORM_ID_alter().
toc_node_form_node_type_form_builder Entity builder for the node type form with toc_node options.
toc_node_node_delete Implements hook_node_delete().
toc_node_node_insert Implements hook_node_insert().
toc_node_node_update Implements hook_node_update().
toc_node_node_view