mirror of
https://gitlab.unicamp.br/infimecc_drupal11_modules/site_users.git
synced 2026-03-09 09:57:41 -03:00
- Adiciona hook_entity_view_mode_alter() para redirecionar visitantes
não-proprietários e não-admins para o view mode 'public' ao acessar
/user/{uid}, exibindo apenas foto padrão, bio e redes sociais
- Simplifica hook_entity_field_access(): operação 'view' retorna neutral
para não-proprietários (visibilidade controlada pelo view mode)
- Cria update_10005() que provisiona o view mode 'public', seu display
e concede a permissão 'access user profiles' ao papel anonymous
- Replica a concessão de permissão em hook_install() para novas instalações
- Corrige TypeError em hook_entity_view_mode_alter(): $context aceita null
- Amplia hook_media_access() para permitir visualização de mídia publicada
também para usuários anônimos
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
433 lines
12 KiB
Plaintext
433 lines
12 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();
|
|
}
|
|
|
|
// 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.');
|
|
}
|
|
|
|
/**
|
|
* 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.');
|
|
}
|