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;
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>';
}
}
function single_page_site_theme() {
return array(
'single_page_site' => array(
'template' => 'single-page-site',
'variables' => array(
'items' => array(),
'wrapper' => 'single-page-overall-wrapper',
),
),
);
}
function single_page_site_page_attachments(array &$page) {
$settings = \Drupal::config('single_page_site.config');
if (!empty($settings)) {
$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';
}
}
function single_page_site_form_menu_link_content_menu_link_content_form_alter(&$form, &$form_state) {
$form['#validate'][] = '_single_page_site_validate_menu_item';
}
function _single_page_site_validate_menu_item($form, FormStateInterface &$form_state) {
$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') {
$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')) {
$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."));
}
}
}