You are here

collapse_text.module in Collapse Text 8

It is an input filter that allows text to be collapsible.

File

collapse_text.module
View source
<?php

/**
 * @file
 * It is an input filter that allows text to be collapsible.
 */
use Drupal\Component\Utility\Html;

/**
 * Implements hook_theme().
 */
function collapse_text_theme($existing, $type, $theme, $path) {
  return [
    'collapse_text_details' => [
      'render element' => 'element',
      'function' => 'theme_collapse_text_details',
    ],
    'collapse_text_form' => [
      'render element' => 'element',
      'function' => 'theme_collapse_text_form',
    ],
  ];
}

/**
 * Theme the collapse text details.
 *
 * Theme a section of collapsible text. By default, this function calls the
 * default 'theme_details' implementation, but this function can be overridden
 * to implement a custom theme just for collapsed text.
 */
function theme_collapse_text_details($element) {
  return drupal_render_children($element['element']);
}

/**
 * Theme the outer form. This is required for the detail(s) to validate.
 */
function theme_collapse_text_form($element) {
  return drupal_render_children($element['element']);
}

/**
 * Implements hook_preprocess_page().
 *
 * Add the collapse.js file.
 */
function collapse_text_preprocess_page(&$variables) {
  $variables['#attached']['library'][] = 'core/drupal.form';
}

/**
 * Implements hook_help().
 */
function collapse_text_help($route_name) {
  switch ($route_name) {
    case 'help.page.collapse_text':
      $readme_file = __DIR__ . '/README.md';
      if (!file_exists($readme_file)) {
        return NULL;
      }
      $text = file_get_contents($readme_file);

      // If the Markdown module is installed...
      if (\Drupal::moduleHandler()
        ->moduleExists('markdown') === TRUE) {

        // Uses the Markdown filter to render the README.
        $filter_manager = \Drupal::service('plugin.manager.filter');
        $settings = \Drupal::configFactory()
          ->get('markdown.settings')
          ->getRawData();
        $config = [
          'settings' => $settings,
        ];
        $filter = $filter_manager
          ->createInstance('markdown', $config);
        $output = $filter
          ->process($text, 'en');
      }
      else {

        // Outputs the escaped README in plain text (watchout for html tags).
        $output = '<pre>' . Html::escape($text) . '</pre>';
      }

      // Adds a link to the Drupal.org documentation pages.
      $output .= t('<p>See the <a href=":documentation">documentation pages</a> on Drupal.org for more information.</p>', [
        ':documentation' => 'https://git.drupalcode.org/project/collapse_text',
      ]);
      return $output;
  }
}

Functions

Namesort descending Description
collapse_text_help Implements hook_help().
collapse_text_preprocess_page Implements hook_preprocess_page().
collapse_text_theme Implements hook_theme().
theme_collapse_text_details Theme the collapse text details.
theme_collapse_text_form Theme the outer form. This is required for the detail(s) to validate.