You are here

single_page_site.module in Single Page Site 8

File

single_page_site.module
View source
<?php

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_help().
 */
function single_page_site_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.single_page_site':
      $output = file_get_contents(drupal_get_path('module', 'single_page_site') . '/README.txt');
      return '<pre>' . $output . '</pre>';
  }
}

/**
 * Implements hook_theme().
 */
function single_page_site_theme() {
  return array(
    'single_page_site' => array(
      'template' => 'single-page-site',
      'variables' => array(
        'items' => array(),
        'wrapper' => 'single-page-overall-wrapper',
      ),
    ),
  );
}

/**
 * Implements hook_page_attachments().
 */
function single_page_site_page_attachments(array &$page) {
  $settings = \Drupal::config('single_page_site.config');
  if (!empty($settings)) {

    // Add JS to create anchors.
    $settings_js = array(
      'className' => !empty($settings
        ->get('class')) ? $settings
        ->get('class') : 'li',
      'menuClass' => $settings
        ->get('menuclass'),
      'isFrontpage' => $settings
        ->get('homepage'),
    );
    if (!$settings
      ->get('homepage')) {
      $settings_js['urlAlias'] = Url::fromRoute('single_page_site.page')
        ->toString();
    }
    $page['#attached']['drupalSettings']['singlePage'] = $settings_js;
    $page['#attached']['library'][] = 'single_page_site/single_page_site.menu';
  }
}

/**
 * Implements hook_form_FROM_ID_alter().
 */
function single_page_site_form_menu_link_content_menu_link_content_form_alter(&$form, &$form_state) {
  $form['#validate'][] = '_single_page_site_validate_menu_item';
}

/**
 * Validates the menu item.
 */
function _single_page_site_validate_menu_item($form, FormStateInterface &$form_state) {

  // Add validation callback to menu that is used as single page menu.
  $menu = $form_state
    ->getValue('menu_parent');
  $settings = \Drupal::config('single_page_site.config');
  if (strpos($menu, $settings
    ->get('menu')) !== FALSE) {
    $uri = $form_state
      ->getValue([
      'link',
      0,
      'uri',
    ]);
    if ($uri == 'internal:/single-page-site') {

      // Don't allow to add "single-page-site" as menu item.
      $form_state
        ->setErrorByName('url', t("You can't add this page to this menu, because it is used as a single page menu."));
    }
    elseif ($uri == 'internal:/' && $settings
      ->get('homepage')) {

      // Don't allow to add "<front>" as menu item if it is "single-page-site".
      // @codingStandardsIgnoreStart
      $form_state
        ->setErrorByName('url', t("You can't add the frontpage to this menu, because this menu is used for building your single page site " . "and you configured the single page as you homepage."));

      // @codingStandardsIgnoreEnd
    }
  }
}