whitelabel-8.x-2.x-dev/whitelabel.module
whitelabel.module
<?php
/**
* @file
* Provides hooks and helper functions for the white label functionality.
*/
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Form\FormStateInterface;
use Drupal\whitelabel\WhiteLabelBlockAlter;
use Drupal\whitelabel\WhiteLabelSystemBrandingBlockAlter;
/**
* Implements hook_altcolor_alter_colors().
*/
function whitelabel_altcolor_alter_colors($theme, &$colors, $cacheableMetadata) {
if ($whitelabel = \Drupal::service('whitelabel.whitelabel_provider')->getWhiteLabel()) {
// Merge the white label colors in the color array.
$colors = array_merge($colors, $whitelabel->getPalette());
// Add the white label as a cache dependency.
$cacheableMetadata->addCacheableDependency($whitelabel);
}
}
/**
* Implements hook_block_view_alter().
*
* @see whitelabel_block_view_pre_render()
*/
function whitelabel_block_view_alter(array &$build, BlockPluginInterface $block) {
$build['#pre_render'][] = [WhiteLabelBlockAlter::class, 'preRender'];
}
/**
* Implements hook_block_view_BASE_BLOCK_ID_alter().
*/
function whitelabel_block_view_system_branding_block_alter(array &$build, BlockPluginInterface $block) {
$build['#pre_render'][] = [WhiteLabelSystemBrandingBlockAlter::class, 'preRender'];
}
/**
* Implements hook_form_FORM_ID_alter() for the entity_form_display_edit_form.
*
* Ensure that white label is the only widget for entity reference revision
* fields referencing a white label entity.
*/
function whitelabel_form_entity_form_display_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($form['#entity_type'], $form['#bundle']);
// Loop over ERR field's display options with whitelabel target type.
foreach (array_keys($field_definitions) as $field_name) {
if ($field_definitions[$field_name]->getType() == 'entity_reference_revisions') {
if ($field_definitions[$field_name]->getSettings()['target_type'] == 'whitelabel') {
// Ensure that only the white label widget is used for WL fields.
$form['fields'][$field_name]['plugin']['type']['#options'] = [
'entity_reference_whitelabel' => $form['fields'][$field_name]['plugin']['type']['#options']['entity_reference_whitelabel'],
];
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter() for the field_storage_config_edit_form.
*
* Ensures that white label entities can no longer be selected in a 'normal'
* entity reference field.
*/
function whitelabel_form_field_storage_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_state->getFormObject()->getEntity()->getType() == 'entity_reference') {
unset($form['settings']['target_type']['#options'][(string) t('Content')]['whitelabel']);
}
}
/**
* Helper function for fetching enabled themes.
*
* @return array
* Array of themes, keyed by system name.
*/
function whitelabel_load_available_themes() {
$theme_options = [];
$themes = \Drupal::service('theme_handler')->listInfo();
uasort($themes, [ModuleExtensionList::class, 'sortByName']);
foreach ($themes as &$theme) {
if (!empty($theme->info['hidden'])) {
continue;
}
$theme_options[$theme->getName()] = $theme->info['name'];
}
return $theme_options;
}
/**
* Implements hook_entity_extra_field_info().
*/
function whitelabel_entity_extra_field_info() {
$extra = [];
if (\Drupal::moduleHandler()->moduleExists('altcolor')) {
$extra['whitelabel']['whitelabel']['form']['altcolor'] = [
'label' => t('Color schemes'),
'description' => 'Provides a color scheme configuration form for themes which support it.',
'weight' => 50,
'visible' => TRUE,
];
}
return $extra;
}
