mirror of
https://gitlab.unicamp.br/infimecc_drupal11_modules/site_users.git
synced 2026-03-08 17:37:41 -03:00
Adiciona update_10003 e update_10004 para normalizar entradas de ldapUserSyncMappings que possuem prov_events, config_module, prov_module ou outros campos nulos, evitando TypeError no FieldProvider do ldap_user. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
362 lines
10 KiB
Plaintext
362 lines
10 KiB
Plaintext
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Install, update and uninstall functions for the Site Users module.
|
|
*/
|
|
|
|
use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
|
use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
|
use Drupal\field\Entity\FieldConfig;
|
|
use Drupal\field\Entity\FieldStorageConfig;
|
|
|
|
/**
|
|
* Implements hook_install().
|
|
*/
|
|
function site_users_install() {
|
|
// Configurar form display para os campos de usuário.
|
|
$form_display = EntityFormDisplay::load('user.user.default');
|
|
if ($form_display) {
|
|
// Campo Nome.
|
|
if (!$form_display->getComponent('field_user_name')) {
|
|
$form_display->setComponent('field_user_name', [
|
|
'type' => 'string_textfield',
|
|
'weight' => 10,
|
|
'settings' => [
|
|
'size' => 60,
|
|
'placeholder' => '',
|
|
],
|
|
'region' => 'content',
|
|
]);
|
|
}
|
|
|
|
// Campo Telefone.
|
|
if (!$form_display->getComponent('field_user_phone')) {
|
|
$form_display->setComponent('field_user_phone', [
|
|
'type' => 'telephone_default',
|
|
'weight' => 11,
|
|
'settings' => [
|
|
'placeholder' => '',
|
|
],
|
|
'region' => 'content',
|
|
]);
|
|
}
|
|
|
|
// Campo Fotos - Media Library widget.
|
|
if (!$form_display->getComponent('field_user_photos')) {
|
|
$form_display->setComponent('field_user_photos', [
|
|
'type' => 'media_library_widget',
|
|
'weight' => 14,
|
|
'settings' => [
|
|
'media_types' => [],
|
|
],
|
|
'region' => 'content',
|
|
]);
|
|
}
|
|
|
|
// Campo Biografia.
|
|
if (!$form_display->getComponent('field_user_bio')) {
|
|
$form_display->setComponent('field_user_bio', [
|
|
'type' => 'text_textarea',
|
|
'weight' => 15,
|
|
'settings' => [
|
|
'rows' => 5,
|
|
'placeholder' => '',
|
|
],
|
|
'region' => 'content',
|
|
]);
|
|
}
|
|
|
|
// Campo Redes Sociais.
|
|
if (!$form_display->getComponent('field_user_social_links')) {
|
|
$form_display->setComponent('field_user_social_links', [
|
|
'type' => 'social_link_widget',
|
|
'weight' => 16,
|
|
'settings' => [],
|
|
'region' => 'content',
|
|
]);
|
|
}
|
|
|
|
// Campo Foto Padrão - oculto no form (será gerenciado via hook_form_alter).
|
|
$form_display->removeComponent('field_user_default_photo');
|
|
|
|
$form_display->save();
|
|
}
|
|
|
|
// Configurar view display para os campos de usuário.
|
|
$view_display = EntityViewDisplay::load('user.user.default');
|
|
if ($view_display) {
|
|
// Campo Nome.
|
|
if (!$view_display->getComponent('field_user_name')) {
|
|
$view_display->setComponent('field_user_name', [
|
|
'type' => 'string',
|
|
'weight' => 10,
|
|
'label' => 'above',
|
|
'settings' => [
|
|
'link_to_entity' => FALSE,
|
|
],
|
|
'region' => 'content',
|
|
]);
|
|
}
|
|
|
|
// Campo Telefone.
|
|
if (!$view_display->getComponent('field_user_phone')) {
|
|
$view_display->setComponent('field_user_phone', [
|
|
'type' => 'telephone_link',
|
|
'weight' => 11,
|
|
'label' => 'above',
|
|
'settings' => [
|
|
'title' => '',
|
|
],
|
|
'region' => 'content',
|
|
]);
|
|
}
|
|
|
|
// Campo Fotos.
|
|
if (!$view_display->getComponent('field_user_photos')) {
|
|
$view_display->setComponent('field_user_photos', [
|
|
'type' => 'entity_reference_entity_view',
|
|
'weight' => 14,
|
|
'label' => 'above',
|
|
'settings' => [
|
|
'view_mode' => 'default',
|
|
'link' => FALSE,
|
|
],
|
|
'region' => 'content',
|
|
]);
|
|
}
|
|
|
|
// Campo Biografia.
|
|
if (!$view_display->getComponent('field_user_bio')) {
|
|
$view_display->setComponent('field_user_bio', [
|
|
'type' => 'text_default',
|
|
'weight' => 15,
|
|
'label' => 'above',
|
|
'settings' => [],
|
|
'region' => 'content',
|
|
]);
|
|
}
|
|
|
|
// Campo Redes Sociais.
|
|
if (!$view_display->getComponent('field_user_social_links')) {
|
|
$view_display->setComponent('field_user_social_links', [
|
|
'type' => 'social_link_formatter',
|
|
'weight' => 16,
|
|
'label' => 'above',
|
|
'settings' => [],
|
|
'region' => 'content',
|
|
]);
|
|
}
|
|
|
|
// Campo Foto Padrão.
|
|
if (!$view_display->getComponent('field_user_default_photo')) {
|
|
$view_display->setComponent('field_user_default_photo', [
|
|
'type' => 'entity_reference_entity_view',
|
|
'weight' => 5,
|
|
'label' => 'hidden',
|
|
'settings' => [
|
|
'view_mode' => 'default',
|
|
'link' => FALSE,
|
|
],
|
|
'region' => 'content',
|
|
]);
|
|
}
|
|
|
|
$view_display->save();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Adiciona o campo field_user_default_photo para seleção de foto padrão.
|
|
*/
|
|
function site_users_update_10001() {
|
|
// Criar field storage se não existir.
|
|
if (!FieldStorageConfig::loadByName('user', 'field_user_default_photo')) {
|
|
FieldStorageConfig::create([
|
|
'field_name' => 'field_user_default_photo',
|
|
'entity_type' => 'user',
|
|
'type' => 'entity_reference',
|
|
'settings' => [
|
|
'target_type' => 'media',
|
|
],
|
|
'cardinality' => 1,
|
|
'translatable' => FALSE,
|
|
])->save();
|
|
}
|
|
|
|
// Criar field instance se não existir.
|
|
if (!FieldConfig::loadByName('user', 'user', 'field_user_default_photo')) {
|
|
FieldConfig::create([
|
|
'field_name' => 'field_user_default_photo',
|
|
'entity_type' => 'user',
|
|
'bundle' => 'user',
|
|
'label' => 'Default Photo',
|
|
'description' => 'Select the main profile photo.',
|
|
'required' => FALSE,
|
|
'settings' => [
|
|
'handler' => 'default:media',
|
|
'handler_settings' => [
|
|
'target_bundles' => [
|
|
'image' => 'image',
|
|
],
|
|
'sort' => [
|
|
'field' => '_none',
|
|
'direction' => 'ASC',
|
|
],
|
|
'auto_create' => FALSE,
|
|
'auto_create_bundle' => '',
|
|
],
|
|
],
|
|
])->save();
|
|
}
|
|
|
|
// Configurar view display.
|
|
$view_display = EntityViewDisplay::load('user.user.default');
|
|
if ($view_display && !$view_display->getComponent('field_user_default_photo')) {
|
|
$view_display->setComponent('field_user_default_photo', [
|
|
'type' => 'entity_reference_entity_view',
|
|
'weight' => 5,
|
|
'label' => 'hidden',
|
|
'settings' => [
|
|
'view_mode' => 'default',
|
|
'link' => FALSE,
|
|
],
|
|
'region' => 'content',
|
|
])->save();
|
|
}
|
|
|
|
// Ocultar do form display (será gerenciado via hook_form_alter).
|
|
$form_display = EntityFormDisplay::load('user.user.default');
|
|
if ($form_display) {
|
|
$form_display->removeComponent('field_user_default_photo')->save();
|
|
}
|
|
|
|
return t('Default photo field created successfully.');
|
|
}
|
|
|
|
/**
|
|
* Adds the field_user_social_links field for social network profile links.
|
|
*/
|
|
function site_users_update_10002() {
|
|
// Create field storage if it does not exist.
|
|
if (!FieldStorageConfig::loadByName('user', 'field_user_social_links')) {
|
|
FieldStorageConfig::create([
|
|
'field_name' => 'field_user_social_links',
|
|
'entity_type' => 'user',
|
|
'type' => 'social_link',
|
|
'module' => 'site_users',
|
|
'cardinality' => -1,
|
|
'translatable' => FALSE,
|
|
])->save();
|
|
}
|
|
|
|
// Create field instance if it does not exist.
|
|
if (!FieldConfig::loadByName('user', 'user', 'field_user_social_links')) {
|
|
FieldConfig::create([
|
|
'field_name' => 'field_user_social_links',
|
|
'entity_type' => 'user',
|
|
'bundle' => 'user',
|
|
'label' => 'Social Links',
|
|
'description' => 'Social network profile links.',
|
|
'required' => FALSE,
|
|
])->save();
|
|
}
|
|
|
|
// Add to form display.
|
|
$form_display = EntityFormDisplay::load('user.user.default');
|
|
if ($form_display && !$form_display->getComponent('field_user_social_links')) {
|
|
$form_display->setComponent('field_user_social_links', [
|
|
'type' => 'social_link_widget',
|
|
'weight' => 16,
|
|
'settings' => [],
|
|
'region' => 'content',
|
|
])->save();
|
|
}
|
|
|
|
// Add to view display.
|
|
$view_display = EntityViewDisplay::load('user.user.default');
|
|
if ($view_display && !$view_display->getComponent('field_user_social_links')) {
|
|
$view_display->setComponent('field_user_social_links', [
|
|
'type' => 'social_link_formatter',
|
|
'weight' => 16,
|
|
'label' => 'above',
|
|
'settings' => [],
|
|
'region' => 'content',
|
|
])->save();
|
|
}
|
|
|
|
return t('Social links field created successfully.');
|
|
}
|
|
|
|
/**
|
|
* Corrige mapeamentos LDAP com prov_events nulo na config ativa.
|
|
*/
|
|
function site_users_update_10003() {
|
|
$config = \Drupal::configFactory()->getEditable('ldap_user.settings');
|
|
$mappings = $config->get('ldapUserSyncMappings');
|
|
|
|
if (empty($mappings)) {
|
|
return t('Nenhum mapeamento LDAP encontrado.');
|
|
}
|
|
|
|
$changed = FALSE;
|
|
foreach ($mappings as $direction => $direction_mappings) {
|
|
foreach ($direction_mappings as $id => $mapping) {
|
|
if (!is_array($mapping['prov_events'] ?? NULL)) {
|
|
$mappings[$direction][$id]['prov_events'] = [];
|
|
$changed = TRUE;
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($changed) {
|
|
$config->set('ldapUserSyncMappings', $mappings)->save();
|
|
return t('Mapeamentos LDAP corrigidos: prov_events nulo substituído por array vazio.');
|
|
}
|
|
|
|
return t('Nenhuma correção necessária nos mapeamentos LDAP.');
|
|
}
|
|
|
|
/**
|
|
* Corrige mapeamentos LDAP com campos de string nulos na config ativa.
|
|
*/
|
|
function site_users_update_10004() {
|
|
$config = \Drupal::configFactory()->getEditable('ldap_user.settings');
|
|
$mappings = $config->get('ldapUserSyncMappings');
|
|
|
|
if (empty($mappings)) {
|
|
return t('Nenhum mapeamento LDAP encontrado.');
|
|
}
|
|
|
|
$changed = FALSE;
|
|
$string_defaults = [
|
|
'ldap_attr' => '',
|
|
'user_attr' => '',
|
|
'user_tokens' => '',
|
|
'config_module' => 'ldap_user',
|
|
'prov_module' => 'ldap_user',
|
|
];
|
|
|
|
foreach ($mappings as $direction => $direction_mappings) {
|
|
foreach ($direction_mappings as $id => $mapping) {
|
|
foreach ($string_defaults as $field => $default) {
|
|
if (!isset($mapping[$field]) || !is_string($mapping[$field])) {
|
|
$mappings[$direction][$id][$field] = $default;
|
|
$changed = TRUE;
|
|
}
|
|
}
|
|
if (!isset($mapping['convert'])) {
|
|
$mappings[$direction][$id]['convert'] = FALSE;
|
|
$changed = TRUE;
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($changed) {
|
|
$config->set('ldapUserSyncMappings', $mappings)->save();
|
|
return t('Mapeamentos LDAP corrigidos: campos de string nulos normalizados.');
|
|
}
|
|
|
|
return t('Nenhuma correção necessária nos mapeamentos LDAP.');
|
|
}
|