ebt_core-1.0.0-alpha3/ebt_core.module
ebt_core.module
<?php
/**
* @file
* EBT Core module file.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\block_content\BlockContentInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
/**
* Implements hook_entity_presave().
*/
function ebt_core_entity_presave(EntityInterface $entity) {
// Fix langcode is empty when you use block type entity form.
// More information: https://www.drupal.org/project/paragraphs/issues/2901390.
if ($entity->getEntityType()->id() == 'paragraph') {
$langcode_key = $entity->getEntityType()->getKey('langcode');
if (!empty($langcode_key)) {
/** @var \Drupal\paragraphs\Entity\Paragraph|null $entity */
$lang_value = $entity->get($langcode_key)->getLangcode();
if (!empty($lang_value)) {
$entity->set($langcode_key, $lang_value);
}
}
}
}
/**
* Implements hook_theme_registry_alter().
*/
function ebt_core_theme_registry_alter(&$theme_registry) {
$module_list = \Drupal::service('extension.list.module')->getList();
$ebt_modules = [];
foreach ($module_list as $module_name => $extention) {
if ($module_name == 'ebt_core') {
continue;
}
if (strpos($module_name, 'ebt_') === 0 && !empty($extention->status)) {
$ebt_modules[] = $module_name;
}
}
foreach ($ebt_modules as $ebt_module) {
$ebt_module_with_dashes = str_replace('_', '-', $ebt_module);
$module_handler = \Drupal::service('module_handler');
if ($module_handler->moduleExists('paragraphs')) {
$base_theme = 'paragraph';
$module_path = \Drupal::service('extension.list.module')->getPath($ebt_module);
$theme_registry['paragraph__' . $ebt_module . '__default'] = [
'path' => $module_path . '/templates',
'template' => 'paragraph--' . $ebt_module_with_dashes . '--default',
'render element' => $theme_registry[$base_theme]['render element'],
'base hook' => $base_theme,
'type' => 'module',
'theme path' => $module_path,
'preprocess functions' => $theme_registry[$base_theme]['preprocess functions'],
];
}
// Support legacy EBT Tabs block type name.
if ($ebt_module == 'ebt_tabs') {
$theme_registry['paragraph__ebt_tab__default'] = [
'path' => $module_path . '/templates',
'template' => 'paragraph--ebt-tab--default',
'render element' => $theme_registry[$base_theme]['render element'],
'base hook' => $base_theme,
'type' => 'module',
'theme path' => $module_path,
'preprocess functions' => $theme_registry[$base_theme]['preprocess functions'],
];
}
$base_theme = 'block';
$module_path = \Drupal::service('extension.list.module')->getPath($ebt_module);
$theme_registry['block__inline_block__' . $ebt_module] = [
'path' => $module_path . '/templates',
'template' => 'block--inline-block--' . $ebt_module_with_dashes,
'render element' => $theme_registry[$base_theme]['render element'],
'base hook' => $base_theme,
'type' => 'module',
'theme path' => $module_path,
'preprocess functions' => $theme_registry[$base_theme]['preprocess functions'],
];
$base_theme = 'block';
$module_path = \Drupal::service('extension.list.module')->getPath($ebt_module);
$theme_registry['block__block_content__' . $ebt_module] = [
'path' => $module_path . '/templates',
'template' => 'block--block-content--' . $ebt_module_with_dashes,
'render element' => $theme_registry[$base_theme]['render element'],
'base hook' => $base_theme,
'type' => 'module',
'theme path' => $module_path,
'preprocess functions' => $theme_registry[$base_theme]['preprocess functions'],
];
$base_theme = 'field';
$module_path = \Drupal::service('extension.list.module')->getPath($ebt_module);
$theme_registry['field__block_content__field_' . $ebt_module . '__' . $ebt_module] = [
'path' => $module_path . '/templates',
'template' => 'field--block-content--field-' . $ebt_module_with_dashes . '--' . $ebt_module_with_dashes,
'render element' => $theme_registry[$base_theme]['render element'],
'base hook' => $base_theme,
'type' => 'module',
'theme path' => $module_path,
'preprocess functions' => $theme_registry[$base_theme]['preprocess functions'],
];
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function ebt_core_theme_suggestions_block_alter(array &$suggestions, array $variables) {
// Adds template suggestions for Block types.
$content = $variables['elements']['content'];
if (isset($content['#block_content'])
and $content['#block_content'] instanceof BlockContentInterface) {
// Add 'block--block-content--BLOCK-TYPE.html.twig'.
$block_type_suggestions[] = 'block__block_content__' . $content['#block_content']->bundle();
// Add 'block--block-content--BLOCK-TYPE--VIEW-MODE.html.twig'.
$block_type_suggestions[] = 'block__block_content__' . $content['#block_content']->bundle() . '__' . $content['#view_mode'];
// Because block__block_content exists twice in $suggestions,
// the suggestion arrays are reversed for further processing.
$suggestions_rev = array_reverse($suggestions);
$block_type_suggestions = array_reverse($block_type_suggestions);
// Insert the block type and view mode suggestions between
// block__block_content and the block instance-specific suggestions.
$index = array_search('block__block_content', $suggestions_rev);
if (is_numeric($index)) {
array_splice($suggestions_rev, $index, 0, $block_type_suggestions);
$suggestions = array_reverse($suggestions_rev);
}
// If block__block_content isn't present as a suggestion.
else {
$suggestions_rev = array_merge($suggestions_rev, $block_type_suggestions);
$suggestions = array_reverse($suggestions_rev);
}
}
}
/**
* Implements hook_theme().
*/
function ebt_core_theme() {
return [
'ebt_settings_default' => [
'variables' => ['ebt_settings' => NULL],
'template' => 'ebt-settings-default',
],
];
}
/**
* Implements hook_preprocess_paragraph().
*/
function ebt_core_preprocess_block(&$variables) {
if (empty($variables['elements']['content']['#block_content']) ||
strpos($variables['elements']['content']['#block_content']->bundle(), 'ebt_') !== 0) {
return;
}
if (empty($variables['content']['#block_content']->field_ebt_settings)) {
return;
}
$service = \Drupal::service('ebt_core.generate_css');
$ebt_settings = $variables['content']['#block_content']->field_ebt_settings->getValue();
if (empty($ebt_settings[0]['ebt_settings']['design_options'])) {
return;
}
if (!empty($variables['base_plugin_id']) && $variables['base_plugin_id'] == 'inline_block') {
$block_class = 'block-revision-id-' . $variables['configuration']['block_revision_id'];
}
else {
$block_class = str_replace(':', '', $variables['plugin_id']);
$block_class = str_replace('_', '-', $block_class);
$block_class = 'ebt-block-' . $block_class;
}
$variables['styles'] = $service->generateFromSettings($ebt_settings[0]['ebt_settings']['design_options'], $block_class);
$service = \Drupal::service('ebt_core.generate_js');
$javascript_data = $service->generateFromSettings($ebt_settings[0]['ebt_settings']['design_options']);
if (!empty($javascript_data)) {
if (!empty($javascript_data['ebtCoreParallax'])) {
$variables['#attached']['library'][] = 'ebt_core/parallax';
}
if (!empty($javascript_data['ebtCoreBackgroundRemoteVideo'])) {
$variables['#attached']['library'][] = 'ebt_core/jquery_mb_ytplayer';
}
$variables['#attached']['drupalSettings']['ebtCore'][$block_class] = $javascript_data;
}
if (!empty($ebt_settings['0']['ebt_settings']['design_options']['other_settings']['edge_to_edge'])) {
$variables['attributes']['class'][] = 'ebt-edge-to-edge';
}
if (!empty($ebt_settings['0']['ebt_settings']['design_options']['other_settings']['container_width'])) {
$variables['attributes']['class'][] = 'ebt-width-' . $ebt_settings['0']['ebt_settings']['design_options']['other_settings']['container_width'];
}
}
/**
* Implements hook_ENTITY_TYPE_view().
*/
function ebt_core_block_content_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
if (strpos($entity->bundle(), 'ebt_') !== 0) {
return;
}
$bundle = _ebt_core_underscore_to_camel_case($entity->bundle());
// Use Block revision ID for Layout builder blocks.
/** @var \Drupal\block_content\Entity\BlockContent $entity */
$revision_id = $entity->get('revision_id')->getValue();
if (!empty($entity->field_ebt_settings)) {
$options = $entity->get('field_ebt_settings')->getValue();
$options = !empty($options[0]['ebt_settings']) ? $options[0]['ebt_settings'] : [];
}
else {
$options = [];
}
if (empty($options) || (isset($options['pass_options_to_javascript']) &&
$options['pass_options_to_javascript'] === FALSE)) {
return;
}
$block_options = [
'blockClass' => 'block-revision-id-' . $revision_id[0]['value'],
'options' => $options,
];
// Use plugin_id for Block content.
$build['#attached']['drupalSettings'][$bundle]['block-revision-id-' . $revision_id[0]['value']] = $block_options;
$uuid = $entity->get('uuid')->getValue();
$block_options = [
'blockClass' => 'plugin-id-block-content' . $uuid[0]['value'],
'options' => $options,
];
$build['#attached']['drupalSettings'][$bundle]['plugin-id-block-content' . $uuid[0]['value']] = $block_options;
}
/**
* Helper function to convert ebt_name block bundle to ebtName camelcase.
*/
function _ebt_core_underscore_to_camel_case($string) {
$str = str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
$str[0] = strtolower($str[0]);
return $str;
}
