You are here

uikit_components.module in UIkit Components 8

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.
 */
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function uikit_components_help($route_name, RouteMatchInterface $route_match) {
  $output = '';
  switch ($route_name) {
    case 'help.page.uikit_components':
      $output = '<p>' . t('The UIkit components module provides additional components and functionality to the UIkit base theme and various Drupal core modules. 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>';
      break;
    case 'uikit_components.admin':
      $output = '<p>' . t('Configuration for the UIkit Components module.') . '</p>';
      break;
    case 'uikit_components.core':
      $output = '<p>' . t('UIkit offers over 30 modular and extendible components, which can be combined with each other. Components are divided into different compartments according to their purpose and functionality. This form allows you to control how some of these components are used.') . '</p>';
      break;
    case 'uikit_components.advanced':
      $output = '<p>' . t("UIkit offers some advanced components that are not included in the UIkit core framework. Usually you wouldn't use these components in your everyday website. They include stuff like nestables and datepickers, which will come in handy, if you create advanced user interfaces like administration areas. This form allows you to control how some of these components are used.") . '</p>';
      break;
  }
  return $output;
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function uikit_components_form_menu_link_content_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $menu_link = $form_state
    ->getFormObject()
    ->getEntity();
  $menu_link_options = $menu_link->link
    ->first()->options ?: [];

  // Translatable string arguments.
  $t_args = [
    ':navbar' => Url::fromUri('https://getuikit.com/v2/docs/navbar.html')
      ->toString(),
  ];

  // UIkit navbar component options.
  $form['menu_item_type'] = [
    '#type' => 'select',
    '#title' => t('Menu item type'),
    '#default_value' => !empty($menu_link_options['menu_item_type']) ? $menu_link_options['menu_item_type'] : 0,
    '#options' => [
      0 => t('Normal menu item'),
      'nav_header' => t('Navigation header'),
      'nav_divider' => t('Navigation divider'),
    ],
    '#description' => t('Select the type of menu item this is. <em class="placeholder">Navigation header</em> and <em class="placeholder">Navigation divider</em> are special menu items from the <a href=":navbar" target="_blank">UIkit navbar component</a>.', $t_args),
  ];
  $form['actions']['submit']['#submit'][] = 'uikit_components_menu_link_content_form_submit';
}

/**
 * Process handler for the menu link content form.
 *
 * @param array $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 */
function uikit_components_menu_link_content_form_submit($form, FormStateInterface $form_state) {
  $menu_item_type = $form_state
    ->getValue('menu_item_type');
  $type = empty($menu_item_type) ? 'normal_menu_item' : $menu_item_type;
  $options = [
    'menu_item_type' => $type,
  ];
  $menu_link = $form_state
    ->getFormObject()
    ->getEntity();
  $menu_link_options = $menu_link->link
    ->first()->options;
  $merged = array_merge($menu_link_options, $options);
  $menu_link->link
    ->first()->options = $merged;
  $menu_link
    ->save();
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function uikit_components_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $block = $form_state
    ->getFormObject()
    ->getEntity();
  $region = $block
    ->getRegion();
  if ($region == 'navbar') {

    // Add navbar alignment setting to navbar blocks.
    $form['uikit_navbar_alignment'] = [
      '#type' => 'select',
      '#title' => t('UIkit navbar alignment'),
      '#default_value' => $block
        ->getThirdPartySetting('uikit_components', 'uikit_navbar_alignment'),
      '#options' => [
        0 => t('Left'),
        1 => t('Right'),
      ],
      '#description' => t('Select the alignment of this menu in the UIkit navbar region.'),
      '#weight' => 0,
    ];

    // Add a form entity builder.
    $form['#entity_builders'][] = 'uikit_components_form_block_form_builder';
  }
}

/**
 * Entity builder for the block configuration entity.
 */
function uikit_components_form_block_form_builder($entity_type, Drupal\block\Entity\Block $block, &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  if ($form_state
    ->getValue('uikit_navbar_alignment')) {
    $block
      ->setThirdPartySetting('uikit_components', 'uikit_navbar_alignment', $form_state
      ->getValue('uikit_navbar_alignment'));
  }
}