You are here

uikit_components.module in UIkit Components 7.3

UIkit Components.

Companion module to the UIkit base theme to provide additional components and functionality.

File

uikit_components.module
View source
<?php

/**
 * @file
 * UIkit Components.
 *
 * Companion module to the UIkit base theme to provide additional components and
 * functionality.
 */

/**
 * Implements hook_help().
 */
function uikit_components_help($path, $arg) {
  switch ($path) {
    case 'admin/config/user-interface/uikit-components':
      return '<p>' . t('TODO: Add help text and a configuration form for configurable settings.') . '</p>';
    case 'admin/help#uikit_components':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The UIkit components module provides additional components and functionality to the UIkit base theme. Some aspects of the frontend cannot be themed without going through the backend, such as empty navbar links. With this module we can add more functionality to the UIkit frontend through the Drupal backend without the need for contributed modules which may add more functionality than needed.') . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Navbar special menu items') . '</dt>';
      $output .= '<dd><p>' . t('When working with menus, UIkit navbars can contain special menu items such as headers, dividers and parent items with no path. Users with the <em>Administer menus and menu items</em> permission can create these special menu items by entering the corresponding text in the <em>Path</em> field when adding/editing links from the <a href="@menu">Menus</a> administration page.', array(
        '@menu' => '/admin/structure/menu',
      )) . '</p>';
      $output .= '<p>Enter one of the following (including &lt; and &gt;) in the <em>Path</em> field to use the special menu item:</p>';
      $output .= t('<ul><li><em>&lt;uk-nav-header&gt;</em>: Creates a menu item as a header</li><li><em>&lt;uk-nav-divider&gt;</em>: Creates a menu item as a separator</li><li><em>&lt;uk-parent&gt;</em>: Creates a parent menu item with no path to follow when clicked</li></ul>') . '</dd>';
      $output .= '</dl>';
      return $output;
  }
}

/**
 * Implements hook_menu().
 */
function uikit_components_menu() {

  // The <uk-nav-header>, <uk-parent> and <uk-nav-divider> menu items allow us
  // to enter these values in the path field when creating a new menu item so we
  // can theme navbar menus correctly.
  $items['<uk-nav-header>'] = array(
    'page callback' => 'drupal_not_found',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['<uk-parent>'] = array(
    'page callback' => 'drupal_not_found',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['<uk-nav-divider>'] = array(
    'page callback' => 'drupal_not_found',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/user-interface/uikit-components'] = array(
    'title' => 'UIkit components',
    'description' => 'Configure UIkit components.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uikit_components_admin_settings_form',
    ),
    'access arguments' => array(
      'administer menu',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function uikit_components_theme($existing, $type, $theme, $path) {
  return array(
    'uikit_navbar' => array(
      'variables' => array(
        'menus' => NULL,
        'menus_flipped' => NULL,
        'attached' => FALSE,
      ),
    ),
  );
}
function theme_uikit_navbar($variables) {
  $menus = $variables['menus'];
  $menus_flipped = $variables['menus_flipped'];
  $attributes['class'][] = 'uk-navbar';
  if ($variables['attached']) {
    $attributes['class'][] = 'uk-navbar-attached';
  }
  $output = '';
  if (!empty($menus) || !empty($menus_flipped)) {

    // Navbar wrapper.
    $output = '<nav' . drupal_attributes($attributes) . '>';
    if (!empty($menus)) {
      foreach ($menus as $menu) {

        // Render menu trees for each menu.
        $menu_tree = menu_tree($menu);
        $menu_tree['#theme_wrappers'] = array(
          'menu_tree__navbar',
        );
        $output .= render($menu_tree);
      }
    }
    if (!empty($menus_flipped)) {
      foreach ($menus_flipped as $menu_flip) {

        // Render menu trees for each flipped menu.
        $menu_tree_flip = menu_tree($menu_flip);
        $menu_tree_flip['#theme_wrappers'] = array(
          'menu_tree__navbar_flip',
        );
        $output .= render($menu_tree_flip);
      }
    }
    $output .= '</nav>';
  }
  return $output;
}

/**
 * Implements template_preprocess_html().
 */
function uikit_components_preprocess_html(&$variables) {
  $t_args = array(
    '@elements' => 'https://www.drupal.org/project/elements',
    '@xautoload' => 'https://www.drupal.org/project/xautoload',
    '@update' => '/update.php',
  );
  if (!module_exists('elements') && !module_enable(array(
    'elements',
  ))) {
    $message = t('<em class="placeholder">UIkit Components</em> requires the <a href="@elements">Elements</a> module. Please download and install <em class="placeholder">Elements</em> and run <a href="@update">update.php</a>.', $t_args);
    drupal_set_message($message, 'error');
  }
  if (!module_exists('xautoload') && !module_enable(array(
    'xautoload',
  ))) {
    $message = t('<em class="placeholder">UIkit Components</em> requires the <a href="@xautoload">X Autoload</a> module. Please download and install <em class="placeholder">X Autoload</em> and run <a href="@update">update.php</a>.', $t_args);
    drupal_set_message($message, 'error');
  }
}

/**
 * System settings form builder for the uikit_components module.
 *
 * @return array
 *   Returns the system settings form.
 */
function uikit_components_admin_settings_form() {
  $form = array();
  return system_settings_form($form);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function uikit_components_form_menu_edit_item_alter(&$form, &$form_state, $form_id) {
  $link_path_description = t('The path for this menu link. This can be an internal path such as <em class="placeholder">node/add</em> or an external URL such as <em class="placeholder">http://example.com</em>. Enter <em class="placeholder">&lt;front&gt;</em> to link to the front page, <em class="placeholder">&lt;uk-nav-header&gt;</em> to create a header, <em class="placeholder">&lt;uk-nav-divider&gt;</em> to create a separator, or <em class="placeholder">&lt;uk-parent&gt;</em> to create a parent menu item with no link.');
  $form['link_path']['#description'] = $link_path_description;
}