mirror of
https://gitlab.unicamp.br/infimecc_drupal11_modules/site_users.git
synced 2026-03-08 01:17:41 -03:00
IDs Lattes têm 16 dígitos, excedendo o limite do INT (≈2.1 bilhões). Corrige erro SQLSTATE[22003] durante o cron ao salvar o campo. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
553 lines
17 KiB
Plaintext
553 lines
17 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();
|
|
}
|
|
|
|
// Criar view mode 'restricted'.
|
|
$view_mode_storage = \Drupal::entityTypeManager()->getStorage('entity_view_mode');
|
|
if (!$view_mode_storage->load('user.restricted')) {
|
|
\Drupal\Core\Entity\Entity\EntityViewMode::create([
|
|
'id' => 'user.restricted',
|
|
'label' => 'Restricted',
|
|
'targetEntityType' => 'user',
|
|
])->save();
|
|
}
|
|
|
|
// Criar view display 'restricted' vazio (template controla a exibição).
|
|
$display_storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
|
|
if (!$display_storage->load('user.user.restricted')) {
|
|
\Drupal\Core\Entity\Entity\EntityViewDisplay::create([
|
|
'targetEntityType' => 'user',
|
|
'bundle' => 'user',
|
|
'mode' => 'restricted',
|
|
'status' => TRUE,
|
|
])->save();
|
|
}
|
|
|
|
// Conceder permissão "access user profiles" ao papel anonymous.
|
|
$anonymous_role = \Drupal\user\Entity\Role::load('anonymous');
|
|
if ($anonymous_role && !$anonymous_role->hasPermission('access user profiles')) {
|
|
$anonymous_role->grantPermission('access user profiles');
|
|
$anonymous_role->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.');
|
|
}
|
|
|
|
/**
|
|
* Cria o view mode 'public' e seu display para perfis de usuário.
|
|
*/
|
|
function site_users_update_10005() {
|
|
// Criar o view mode 'public' se não existir.
|
|
$view_mode_storage = \Drupal::entityTypeManager()->getStorage('entity_view_mode');
|
|
if (!$view_mode_storage->load('user.public')) {
|
|
\Drupal\Core\Entity\Entity\EntityViewMode::create([
|
|
'id' => 'user.public',
|
|
'label' => 'Public',
|
|
'targetEntityType' => 'user',
|
|
])->save();
|
|
}
|
|
|
|
// Criar o view display 'public' se não existir.
|
|
$display_storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
|
|
if (!$display_storage->load('user.user.public')) {
|
|
$display = \Drupal\Core\Entity\Entity\EntityViewDisplay::create([
|
|
'targetEntityType' => 'user',
|
|
'bundle' => 'user',
|
|
'mode' => 'public',
|
|
'status' => TRUE,
|
|
]);
|
|
|
|
$display->setComponent('field_user_default_photo', [
|
|
'type' => 'entity_reference_entity_view',
|
|
'weight' => 5,
|
|
'label' => 'hidden',
|
|
'settings' => [
|
|
'view_mode' => 'default',
|
|
'link' => FALSE,
|
|
],
|
|
'region' => 'content',
|
|
]);
|
|
|
|
$display->setComponent('field_user_bio', [
|
|
'type' => 'text_default',
|
|
'weight' => 10,
|
|
'label' => 'hidden',
|
|
'settings' => [],
|
|
'region' => 'content',
|
|
]);
|
|
|
|
$display->setComponent('field_user_social_links', [
|
|
'type' => 'social_link_formatter',
|
|
'weight' => 15,
|
|
'label' => 'hidden',
|
|
'settings' => [],
|
|
'region' => 'content',
|
|
]);
|
|
|
|
$display->save();
|
|
}
|
|
|
|
// Conceder permissão "access user profiles" ao papel anonymous.
|
|
$anonymous_role = \Drupal\user\Entity\Role::load('anonymous');
|
|
if ($anonymous_role && !$anonymous_role->hasPermission('access user profiles')) {
|
|
$anonymous_role->grantPermission('access user profiles');
|
|
$anonymous_role->save();
|
|
}
|
|
|
|
return t("View mode 'public' para usuário criado com foto padrão, bio e redes sociais.");
|
|
}
|
|
|
|
/**
|
|
* 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.');
|
|
}
|
|
|
|
/**
|
|
* Cria view mode 'restricted', campo field_user_selected_view_mode e config role_view_modes.
|
|
*/
|
|
function site_users_update_10006() {
|
|
$view_mode_storage = \Drupal::entityTypeManager()->getStorage('entity_view_mode');
|
|
$display_storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
|
|
|
|
// 1. Criar view mode 'restricted'.
|
|
if (!$view_mode_storage->load('user.restricted')) {
|
|
\Drupal\Core\Entity\Entity\EntityViewMode::create([
|
|
'id' => 'user.restricted',
|
|
'label' => 'Restricted',
|
|
'targetEntityType' => 'user',
|
|
])->save();
|
|
}
|
|
|
|
// 2. Criar view display 'restricted' vazio (template controla a exibição).
|
|
if (!$display_storage->load('user.user.restricted')) {
|
|
\Drupal\Core\Entity\Entity\EntityViewDisplay::create([
|
|
'targetEntityType' => 'user',
|
|
'bundle' => 'user',
|
|
'mode' => 'restricted',
|
|
'status' => TRUE,
|
|
])->save();
|
|
}
|
|
|
|
// 3. Criar field storage se não existir.
|
|
if (!FieldStorageConfig::loadByName('user', 'field_user_selected_view_mode')) {
|
|
FieldStorageConfig::create([
|
|
'field_name' => 'field_user_selected_view_mode',
|
|
'entity_type' => 'user',
|
|
'type' => 'string',
|
|
'settings' => [
|
|
'max_length' => 64,
|
|
'is_ascii' => TRUE,
|
|
'case_sensitive' => FALSE,
|
|
],
|
|
'cardinality' => 1,
|
|
'translatable' => FALSE,
|
|
])->save();
|
|
}
|
|
|
|
// 4. Criar field instance se não existir.
|
|
if (!FieldConfig::loadByName('user', 'user', 'field_user_selected_view_mode')) {
|
|
FieldConfig::create([
|
|
'field_name' => 'field_user_selected_view_mode',
|
|
'entity_type' => 'user',
|
|
'bundle' => 'user',
|
|
'label' => 'Profile visibility',
|
|
'description' => 'Controls how this profile appears to other visitors.',
|
|
'required' => FALSE,
|
|
'default_value' => [['value' => 'restricted']],
|
|
])->save();
|
|
}
|
|
|
|
// 5. Remover do form display.
|
|
$form_display = EntityFormDisplay::load('user.user.default');
|
|
if ($form_display) {
|
|
$form_display->removeComponent('field_user_selected_view_mode')->save();
|
|
}
|
|
|
|
// 6. Remover de todos os view displays existentes.
|
|
$displays = $display_storage->loadMultiple();
|
|
foreach ($displays as $display) {
|
|
if ($display->getTargetEntityTypeId() === 'user' && $display->getComponent('field_user_selected_view_mode')) {
|
|
$display->removeComponent('field_user_selected_view_mode')->save();
|
|
}
|
|
}
|
|
|
|
// 7. Adicionar role_view_modes ao config se ausente.
|
|
$config = \Drupal::configFactory()->getEditable('site_users.settings');
|
|
if ($config->get('role_view_modes') === NULL) {
|
|
$config->set('role_view_modes', [])->save();
|
|
}
|
|
|
|
return t("View mode 'restricted' e campo field_user_selected_view_mode criados.");
|
|
}
|
|
|
|
/**
|
|
* Altera field_user_id_lattes de INT para BIGINT (IDs Lattes têm 16 dígitos).
|
|
*/
|
|
function site_users_update_10007() {
|
|
// Atualiza a config ativa.
|
|
\Drupal::configFactory()
|
|
->getEditable('field.storage.user.field_user_id_lattes')
|
|
->set('settings.size', 'big')
|
|
->save(TRUE);
|
|
|
|
// Atualiza a definição instalada e o esquema do banco de dados.
|
|
$manager = \Drupal::entityDefinitionUpdateManager();
|
|
$storage_definition = $manager->getFieldStorageDefinition('field_user_id_lattes', 'user');
|
|
if ($storage_definition) {
|
|
$storage_definition->setSetting('size', 'big');
|
|
$manager->updateFieldStorageDefinition($storage_definition);
|
|
}
|
|
|
|
return t('field_user_id_lattes alterado para BIGINT para suportar IDs Lattes de 16 dígitos.');
|
|
}
|
|
|
|
/**
|
|
* 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.');
|
|
}
|