You are here

class UIkitComponents in UIkit Components 8.2

Same name and namespace in other branches
  1. 8.3 src/UIkitComponents.php \Drupal\uikit_components\UIkitComponents
  2. 8 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.

Hierarchy

Expanded class hierarchy of UIkitComponents

5 files declare their use of UIkitComponents
AdminForm.php in src/Form/AdminForm.php
alter.inc in includes/alter.inc
Modify structured content arrays.
MenuEditForm.php in src/Form/MenuEditForm.php
preprocess.inc in includes/preprocess.inc
Set up variables to be placed within the template (.html.twig) files.
uikit_components.module in ./uikit_components.module
UIkit Components.

File

src/UIkitComponents.php, line 13

Namespace

Drupal\uikit_components
View source
class UIkitComponents {

  /**
   * Loads a project's 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 node.admin.inc from the node module.
   *   UIkitComponents::loadIncludeFile('inc', 'node', 'module', 'node.admin');
   *
   *   // Load includes/alter.inc from the uikit theme.
   *   UIkitComponents::loadIncludeFile('inc', 'uikit', 'theme', 'preprocess', 'includes');
   * @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 $project
   *   The project to which the include file belongs.
   * @param string $project_type
   *   The project type to which the include file belongs, either "theme" or
   *   "module". Defaults to "module".
   * @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 loadIncludeFile($type, $project, $project_type = 'module', $name = NULL, $sub_directory = '') {
    static $files = [];
    if (isset($sub_directory)) {
      $sub_directory = '/' . $sub_directory;
    }
    if (!isset($name)) {
      $name = $project;
    }
    $key = $type . ':' . $project . ':' . $name . ':' . $sub_directory;
    if (isset($files[$key])) {
      return $files[$key];
    }
    if (function_exists('drupal_get_path')) {
      $file = DRUPAL_ROOT . '/' . drupal_get_path($project_type, $project) . "{$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 = [
      ':clear_cache' => Url::fromRoute('system.performance_settings')
        ->toString(),
      ':themes_page' => Url::fromRoute('system.themes_page')
        ->toString(),
      ':uikit_project' => Url::fromUri('https://www.drupal.org/project/uikit')
        ->toString(),
    ];
    if (isset($theme_list['uikit'])) {
      $uikit_libraries = Yaml::parse(file_get_contents(drupal_get_path('theme', 'uikit') . '/uikit.libraries.yml'));
      return $uikit_libraries['uikit']['version'];
    }
    else {
      \Drupal::messenger()
        ->addError(t('The UIkit base theme is either not installed or enabled. Please <a href=":themes_page">enable</a>, or <a href=":uikit_project" target="_blank">download</a> and <a href=":themes_page">install</a> UIkit. If UIkit is installed and enabled, try <a href=":clear_cache">clearing all caches</a>.', $t_args));
      return FALSE;
    }
  }

  /**
   * Get the UIkit documentation URL for the given component.
   *
   * @param string $component
   *   The component to return a URL for.
   *
   * @return string
   *   Returns a URL for the given component if set, FALSE otherwise.
   */
  public static function getComponentURL($component) {
    if (!$component) {
      \Drupal::messenger()
        ->addWarning(t('URL cannot be returned, no component was given in <em class="placeholder">UIkitComponents::getComponentURL()</em>.'));
      return FALSE;
    }
    else {
      $uri = 'https://getuikit.com/v2/docs/' . $component . '.html';
      return Url::fromUri($uri)
        ->toString();
    }
  }

  /**
   * Returns the menu style.
   *
   * @param string $menu
   *   The name of the menu.
   *
   * @return bool
   *   Returns menu style, FALSE otherwise.
   */
  public static function getMenuStyle($menu) {
    return \Drupal::state()
      ->get($menu . '_menu_style') ?: 0;
  }

  /**
   * Sets the menu style.
   *
   * @param string $menu
   *   The name of the menu.
   *
   * @param string $value
   *   The style value to set for the menu.
   */
  public static function setMenuStyle($menu, $value) {
    \Drupal::state()
      ->set($menu . '_menu_style', $value);
  }

  /**
   * Returns the menu nav width classes.
   *
   * @param string $menu
   *   The name of the menu.
   *
   * @return bool
   *   Returns TRUE if the nav width classes are set, FALSE otherwise.
   */
  public static function getNavWidthClasses($menu) {
    return \Drupal::state()
      ->get($menu . '_menu_style_wrapper_widths') ?: 0;
  }

  /**
   * Sets the nav center modifier.
   *
   * @param string $menu
   *   The name of the menu.
   *
   * @param string $value
   *   The nav center modifier value to set for the menu.
   */
  public static function setNavWidthClasses($menu, $value) {
    \Drupal::state()
      ->set($menu . '_menu_style_wrapper_widths', $value);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UIkitComponents::getComponentURL public static function Get the UIkit documentation URL for the given component.
UIkitComponents::getMenuStyle public static function Returns the menu style.
UIkitComponents::getNavWidthClasses public static function Returns the menu nav width classes.
UIkitComponents::getUIkitLibraryVersion public static function Get the library version from the UIkit base theme.
UIkitComponents::loadIncludeFile public static function Loads a project's include file.
UIkitComponents::setMenuStyle public static function Sets the menu style.
UIkitComponents::setNavWidthClasses public static function Sets the nav center modifier.