You are here

beautytips_ui.module in BeautyTips 8

Adds includes to Beautytips settings page and provides some built in functionality. TODO: hook_theme invoke - maybe Invoked include hooks: admin_info, menu_change, theme_change, form_change, menu_items ex beautytips_textinput_info.

File

beautytips_ui/beautytips_ui.module
View source
<?php

/**
 * @file
 * Adds includes to Beautytips settings page and provides some built in
 *  functionality. TODO: hook_theme invoke - maybe Invoked include hooks:
 *  admin_info, menu_change, theme_change, form_change, menu_items
 *  ex beautytips_textinput_info.
 */
function beautytips_ui_admin_settings(&$form) {
  $items = beautytips_ui_include_invoke('beautytips', 'admin_info', []);
  if (is_array($items)) {
    foreach ($items as $form_item) {
      if (count($form_item)) {
        $keys = array_keys($form_item);
        if (count($keys)) {
          foreach ($keys as $menu_item) {
            $form[$menu_item] = $form_item[$menu_item];
          }
        }
      }
    }
  }
}

/**
 * Clear menu cache and theme registry in case beautytips help tips turned on
 *  or off
 */
function beautytips_ui_admin_submit($form, $form_state) {
  \Drupal::moduleHandler()
    ->invoke('menu', 'rebuild');
  \Drupal::cache('menu')
    ->deleteAll();
  drupal_theme_rebuild();
}

/**
 * Implements hook_menu().
 */
function beautytips_ui_menu() {
  $items = [];
  $menu_items = beautytips_ui_include_invoke('beautytips', 'menu_items', []);
  if (is_array($menu_items)) {
    foreach ($menu_items as $menu_item) {
      if (is_array($menu_item)) {
        $path = key($menu_item);
        $items[$path] = $menu_item[$path];
      }
    }
  }
  return $items;
}

/**
 * Determine whether an include implements a hook, cf. module_hook.
 *
 * @param $tooltip
 *   The name of the tooltip file (without the .inc extension), such as
 *   'youtube' or 'google'.
 * @param $hook
 *   The name of the hook (e.g. "thumbnail", "settings", etc.).
 *
 * @return
 *   TRUE if the tooltip is loaded and the hook is implemented.
 */
function beautytips_ui_include_hook($module, $tooltip, $hook) {
  return function_exists($module . '_' . $tooltip . '_' . $hook);
}

/**
 * Invoke hook in a particular include.
 *
 * @param $module
 *  the helper module
 * @param $tooltip
 *   The name of the tooltip (without the .inc extension).
 * @param $hook
 *   The name of the hook (e.g. "menu_change", "form_change", etc.).
 * @param ...
 *   Arguments to pass to the hook implementation.
 *
 * @return
 *   The return value of the hook implementation.
 */
function beautytips_ui_include_invoke($module, $hook, $args) {
  $includes = beautytips_ui_include_list();
  foreach ($includes as $tooltip) {
    $function = $module . '_' . $tooltip . '_' . $hook;
    $params[] = beautytips_ui_include_hook($module, $tooltip, $hook) ? call_user_func_array($function, $args) : NULL;
  }
  return $params;
}

/**
 * Maintains a list of all loaded include files.
 *
 * @return $files array.
 */
function beautytips_ui_include_list() {
  $files = [
    'drupal_help.inc' => 'drupal_help',
    'textinput.inc' => 'textinput',
  ];
  foreach ($files as $name) {
    module_load_include('inc', 'beautytips', 'includes/' . $name);
  }
  return $files;
}

/**
 * Implements hook_form_alter().
 *
 * Adds beautytips for textareas and textfields.
 */
function beautytips_ui_form_alter(&$form, $form_state, $form_id) {
  beautytips_ui_include_invoke('beautytips', 'form_change', [
    'form' => &$form,
    'form_state' => $form_state,
    'form_id' => $form_id,
  ]);
}

/**
 * Implements hook_menu_alter().
 */
function beautytips_ui_menu_alter(&$items) {
  beautytips_ui_include_invoke('beautytips', 'menu_change', [
    'items' => &$items,
  ]);
}

/**
 * Implementation of hook_theme_registry_alter().
 */
function beautytips_ui_theme_registry_alter(&$theme_registry) {
  beautytips_ui_include_invoke('beautytips', 'theme_change', [
    'theme_registry' => &$theme_registry,
  ]);
}

Functions

Namesort descending Description
beautytips_ui_admin_settings @file Adds includes to Beautytips settings page and provides some built in functionality. TODO: hook_theme invoke - maybe Invoked include hooks: admin_info, menu_change, theme_change, form_change, menu_items ex beautytips_textinput_info.
beautytips_ui_admin_submit Clear menu cache and theme registry in case beautytips help tips turned on or off
beautytips_ui_form_alter Implements hook_form_alter().
beautytips_ui_include_hook Determine whether an include implements a hook, cf. module_hook.
beautytips_ui_include_invoke Invoke hook in a particular include.
beautytips_ui_include_list Maintains a list of all loaded include files.
beautytips_ui_menu Implements hook_menu().
beautytips_ui_menu_alter Implements hook_menu_alter().
beautytips_ui_theme_registry_alter Implementation of hook_theme_registry_alter().