farm-2.x-dev/modules/core/l10n/farm_l10n.module
modules/core/l10n/farm_l10n.module
<?php
/**
* @file
* Hooks and customizations for the farm_l10n module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
/**
* Implements hook_help().
*/
function farm_l10n_help($route_name, RouteMatchInterface $route_match) {
$output = '';
// Help text for the farm/settings/language form.
if ($route_name == 'farm_l10n.settings') {
$output .= '<p>' . t('Select the default language for the user interface. Individual users can override this by editing their profile.') . '</p>';
}
return $output;
}
/**
* Implements hook_entity_bundle_info_alter().
*/
function farm_l10n_entity_bundle_info_alter(&$bundles) {
// If the content translation module is not enabled, alter all entity type
// bundles to mark them as not translatable. This fixes an issue with
// JSON:API PATCH requests when authenticated as a user with a non-default
// language.
// @see https://www.drupal.org/project/farm/issues/3335267
if (\Drupal::moduleHandler()->moduleExists('content_translation')) {
return;
}
foreach ($bundles as $entity_type => $entity_type_bundles) {
foreach ($entity_type_bundles as $bundle => $bundle_info) {
if (!empty($bundle_info['translatable']) && $bundle_info['translatable'] === TRUE) {
$bundles[$entity_type][$bundle]['translatable'] = FALSE;
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function farm_l10n_form_language_admin_overview_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Disable the ability to change the site's default language and direct users
// to /farm/settings/language instead.
// @see https://www.drupal.org/project/farm/issues/3257430
$message = t('To change the default language of farmOS, please go to <a href=":url">farmOS language settings</a>.', [':url' => Url::fromRoute('farm_l10n.settings')->toString()]);
\Drupal::messenger()->addWarning($message);
foreach (Element::children($form['languages']) as $langcode) {
$form['languages'][$langcode]['default']['#access'] = FALSE;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function farm_l10n_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Use the "Selected language" as the default for new users (unless it is
// still set to "site_default").
$selected_language = \Drupal::config('language.negotiation')->get('selected_langcode');
if ($selected_language == 'site_default') {
return;
}
if (!empty($form['language']['preferred_langcode'])) {
$form['language']['preferred_langcode']['#default_value'] = $selected_language;
}
if (!empty($form['language']['preferred_admin_langcode'])) {
$form['language']['preferred_admin_langcode']['#default_value'] = $selected_language;
}
}
