isi-8.x-1.1-beta1/isi.module
isi.module
<?php
/**
* @file
* Contains isi.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\block_content\BlockContentInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
/**
* Implements hook_help().
*/
function isi_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the isi module.
case 'help.page.isi':
$text = file_get_contents(__DIR__ . '/README.md');
if (!\Drupal::moduleHandler()->moduleExists('markdown')) {
return '<pre>' . Html::escape($text) . '</pre>';
}
else {
// Use the Markdown filter to render the README.
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::configFactory()->get('markdown.settings')->getRawData();
$config = ['settings' => $settings];
$filter = $filter_manager->createInstance('markdown', $config);
return $filter->process($text, 'en');
}
default:
}
return NULL;
}
/**
* Implements hook_preprocess_HOOK().
*/
function isi_preprocess_block(&$variables) {
if ($variables['base_plugin_id'] == 'block_content') {
if (!empty($variables['elements']['content']['#block_content'])) {
$block_content = $variables['elements']['content']['#block_content'];
$test = $block_content->bundle();
if ($test == 'isi') {
$content = $variables['elements']['content'];
if (isset($content['#block_content']) && $content['#block_content'] instanceof BlockContentInterface) {
$variables['#attached']['library'][] = 'isi/inline';
if ($content['#block_content']->hasField('field_isi_drawer')) {
$footer = $content['#block_content']->get('field_isi_footer')->value;
$drawer = $content['#block_content']->get('field_isi_drawer')->value;
$initialVisit = $content['#block_content']->get('field_isi_initial_visit')->value;
if ($footer) {
$variables['footerEnabled'] = $footer;
$variables['#attached']['drupalSettings']['isi']['footer'] = 1;
if ($drawer) {
$variables['#attached']['library'][] = 'isi/drawer';
$variables['#attached']['drupalSettings']['isi']['drawer'] = 1;
$variables['drawerEnabled'] = $drawer;
$variables['drawerTitle'] = $content['#block_content']->get('field_isi_title')->value;
$variables['drawer_expand_text'] = $content['#block_content']->get('field_isi_toggle_open')->value;
$variables['drawer_collapse_text'] = $content['#block_content']->get('field_isi_toggle_close')->value;
}
else {
$variables['#attached']['library'][] = 'isi/stickyfooter';
$variables['#attached']['drupalSettings']['isi']['stickyfooter'] = 1;
}
}
if ($initialVisit) {
$variables['#attached']['library'][] = 'isi/initial_visit';
}
}
}
}
}
}
}
/**
* Implements hook_theme_suggestions_block_alter().
*/
function isi_theme_suggestions_block_alter(array &$suggestions, array $variables) {
if (isset($variables['elements']['content']['#block_content'])) {
$block_type = $variables['elements']['content']['#block_content']->get('type')
->getValue();
if ($block_type[0]['target_id'] == 'isi') {
$suggestions[] = 'block__isi';
}
}
}
/**
* Implements template_theme_suggestions_paragraph_alter()
*/
function isi_theme_suggestions_paragraph_alter(&$suggestions, $variables) {
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = &$variables['elements']['#paragraph'];
$bundle = $paragraph->bundle();
switch ($bundle) {
case 'isi_section':
$suggestions[] = 'paragraph__isi_section';
break;
}
}
/**
* Implements hook_theme_registry_alter().
*/
function isi_theme_registry_alter(&$theme_registry) {
$module_handler = \Drupal::service('module_handler');
$module_path = $module_handler->getModule('isi')->getPath();
if (isset($theme_registry['paragraph__isi_section'])) {
if ($theme_registry['paragraph__isi_section']['template'] != 'paragraph--isi-section') {
$theme_registry['paragraph__isi_section']['path'] = $module_path . '/templates';
$theme_registry['paragraph__isi_section']['template'] = 'paragraph--isi-section';
}
}
else {
$theme_registry['paragraph__isi_section'] = [
'render element' => 'elements',
'type' => 'module',
'theme path' => $module_path,
'template' => 'paragraph--isi-section',
'path' => $module_path . '/templates',
'preprocess functions' => [
'template_preprocess',
'template_preprocess_paragraph',
'contextual_preprocess',
'paragraphs_library_preprocess_paragraph',
'isi_preprocess_paragraph__isi_section',
],
'base hook' => 'paragraph',
];
}
if (isset($theme_registry['block__isi'])) {
if ($theme_registry['block__isi']['template'] != 'block--isi') {
$theme_registry['block__isi']['path'] = $module_path . '/templates';
$theme_registry['block__isi']['template'] = 'block--isi';
}
}
}
/**
* Implements hook_form_alter().
*/
function isi_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (in_array($form_id, [
'block_content_isi_form',
'block_content_isi_edit_form',
])) {
$form['global_settings'] = [
'#type' => 'details',
'#title' => t('Global settings'),
'#description' => t('Lorem ipsum.'),
'#open' => TRUE,
];
$form['global_settings']['field_isi_footer'] = $form['field_isi_footer'];
unset($form['field_isi_footer']);
$form['inline_block'] = [
'#type' => 'details',
'#title' => t('Inline block settings'),
'#description' => t('Content for inline ISI block.'),
'#open' => TRUE,
];
$form['inline_block']['field_isi_inline_content'] = $form['field_isi_inline_content'];
unset($form['field_isi_inline_content']);
$form['footer_block'] = [
'#type' => 'details',
'#title' => t('Footer settings'),
'#description' => t('Applicable only when footer checkbox is chosen.'),
'#open' => TRUE,
'#states' => [
'visible' => [
':input[name="field_isi_footer[value]"]' => ['checked' => TRUE],
],
],
];
$form['footer_block']['field_isi_initial_visit'] = $form['field_isi_initial_visit'];
$form['footer_block']['field_isi_drawer'] = $form['field_isi_drawer'];
$form['footer_block']['field_isi_title'] = $form['field_isi_title'];
$form['footer_block']['field_isi_initial_visit']['#weight'] = 10;
$form['footer_block']['field_isi_drawer']['#weight'] = 20;
$form['footer_block']['field_isi_title']['#weight'] = 30;
$form['footer_block']['drawer_block'] = [
'#type' => 'details',
'#title' => t('Drawer settings'),
'#description' => t('Applicable only when drawer checkbox is chosen.'),
'#open' => TRUE,
'#weight' => 40,
'#states' => [
'visible' => [
':input[name="field_isi_drawer[value]"]' => ['checked' => TRUE],
],
],
];
$form['footer_block']['drawer_block']['field_isi_toggle_open'] = $form['field_isi_toggle_open'];
$form['footer_block']['drawer_block']['field_isi_toggle_close'] = $form['field_isi_toggle_close'];
$form['footer_block']['drawer_block']['field_isi_drawer_content'] = $form['field_isi_drawer_content'];
unset($form['field_isi_initial_visit']);
unset($form['field_isi_drawer']);
unset($form['field_isi_title']);
unset($form['field_isi_toggle_open']);
unset($form['field_isi_toggle_close']);
unset($form['field_isi_drawer_content']);
}
}
/**
* Implements hook_preprocess_paragraph__HOOK().
*/
function isi_preprocess_paragraph__isi_section(&$variables) {
$paragraph = &$variables['paragraph'];
if ($paragraph->hasField('field_enabled')) {
foreach ($paragraph->get('field_enabled') as $row) {
if (!isset($row->getValue()['value']) || $row->getValue()['value'] == 0) {
unset($variables['content']);
}
}
}
}
