You are here

class UIkitComponents in UIkit Components 8

Same name and namespace in other branches
  1. 8.3 src/UIkitComponents.php \Drupal\uikit_components\UIkitComponents
  2. 8.2 src/UIkitComponents.php \Drupal\uikit_components\UIkitComponents
  3. 7.3 src/UIkitComponents.php \Drupal\uikit_components\UIkitComponents
  4. 7.2 src/UIkitComponents.php \Drupal\uikit_components\UIkitComponents

Class UIkitComponents

Provides helper functions for the UIkit Components module.

@package Drupal\uikit_components

Hierarchy

Expanded class hierarchy of UIkitComponents

2 files declare their use of UIkitComponents
UIkitComponentsAdminForm.php in src/Form/UIkitComponentsAdminForm.php
uikit_views.theme.inc in uikit_views/includes/uikit_views.theme.inc
Preprocessors and helper functions to make theming easier.

File

src/UIkitComponents.php, line 15

Namespace

Drupal\uikit_components
View source
class UIkitComponents {

  /**
   * Loads a theme include file.
   *
   * This function essentially does the same as Drupal core's
   * module_load_include() function, except targeting theme include files. It also
   * allows you to place the include files in a sub-directory of the theme for
   * better organization.
   *
   * Examples:
   * @code
   *   // Load includes/uikit_subtheme.admin.inc from the node module.
   *   uikit_theme_load_include('inc', 'uikit_subtheme', 'uikit_subtheme.admin', 'includes');
   *   // Load preprocess.inc from the uikit_subtheme theme.
   *   uikit_theme_load_include('inc', 'uikit_subtheme', 'preprocess');
   * @endcode
   *
   * Do not use this function in a global context since it requires Drupal to be
   * fully bootstrapped, use require_once DRUPAL_ROOT . '/path/file' instead.
   *
   * @param string $type
   *   The include file's type (file extension).
   * @param string $theme
   *   The theme to which the include file belongs.
   * @param string $name
   *   (optional) The base file name (without the $type extension). If omitted,
   *   $theme is used; i.e., resulting in "$theme.$type" by default.
   * @param string $sub_directory
   *   (optional) The sub-directory to which the include file resides.
   *
   * @return string
   *   The name of the included file, if successful; FALSE otherwise.
   */
  public static function uikit_theme_load_include($type, $theme, $name = NULL, $sub_directory = '') {
    static $files = [];
    if (isset($sub_directory)) {
      $sub_directory = '/' . $sub_directory;
    }
    if (!isset($name)) {
      $name = $theme;
    }
    $key = $type . ':' . $theme . ':' . $name . ':' . $sub_directory;
    if (isset($files[$key])) {
      return $files[$key];
    }
    if (function_exists('drupal_get_path')) {
      $file = DRUPAL_ROOT . '/' . drupal_get_path('theme', $theme) . "{$sub_directory}/{$name}.{$type}";
      if (is_file($file)) {
        require_once $file;
        $files[$key] = $file;
        return $file;
      }
      else {
        $files[$key] = FALSE;
      }
    }
    return FALSE;
  }

  /**
   * Get the library version from the UIkit base theme.
   *
   * @return string
   *   The major version of the UIkit library from the install UIkit base theme.
   */
  public static function getUIkitLibraryVersion() {
    $theme_list = \Drupal::service('theme_handler')
      ->listInfo();

    // Translatable strings.
    $t_args = [
      ':uikit_project' => Url::fromUri('https://www.drupal.org/project/uikit')
        ->toString(),
      ':themes_page' => Url::fromRoute('system.themes_page')
        ->toString(),
    ];
    if (isset($theme_list['uikit'])) {
      $uikit_libraries = Yaml::parse(drupal_get_path('theme', 'uikit') . '/uikit.libraries.yml');
      $uikit_version = explode('.', $uikit_libraries['uikit']['version']);
      return implode('.', $uikit_version);
    }
    else {
      drupal_set_message(t('The UIkit base theme is either not installed or could not be found. Please <a href=":uikit_project" target="_blank">download</a> and <a href=":themes_page">install</a> UIkit.', $t_args), 'error');
      return FALSE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UIkitComponents::getUIkitLibraryVersion public static function Get the library version from the UIkit base theme.
UIkitComponents::uikit_theme_load_include public static function Loads a theme include file.