mirror of
https://gitlab.unicamp.br/infimecc_drupal11_modules/site_users.git
synced 2026-05-04 09:50:41 -03:00
Permite usar título do nó homepage como título da página do microsite
Adiciona checkbox 'use_homepage_title' ao formulário de configuração do microsite. Quando ativado, o hook_preprocess_block substitui o título do bloco 'Título da Página' pelo título do nó configurado como homepage. Sem nó configurado ou no caso de fallback, mantém o nome do usuário. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -65,6 +65,50 @@ function _site_users_microsite_remove_homepage_from_tree(array &$items, int $hom
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements hook_preprocess_block().
|
||||||
|
*
|
||||||
|
* Substitui o título do bloco "Título da Página" pelo título do nó homepage
|
||||||
|
* quando o usuário tiver ativado essa opção nas configurações do microsite.
|
||||||
|
* Sem nó homepage configurado (ou no fallback), mantém o comportamento padrão.
|
||||||
|
*/
|
||||||
|
function site_users_microsite_preprocess_block(&$variables): void {
|
||||||
|
if ($variables['plugin_id'] !== 'page_title_block') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$route_match = \Drupal::routeMatch();
|
||||||
|
$route_name = $route_match->getRouteName();
|
||||||
|
|
||||||
|
$is_microsite = $route_name === 'entity.user.canonical'
|
||||||
|
|| str_starts_with($route_name, 'site_users_microsite.');
|
||||||
|
|
||||||
|
if (!$is_microsite) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = $route_match->getParameter('user');
|
||||||
|
if (!($user instanceof UserInterface)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$userData = \Drupal::service('user.data');
|
||||||
|
|
||||||
|
if (!$userData->get('site_users_microsite', $user->id(), 'use_homepage_title')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$homepage_nid = $userData->get('site_users_microsite', $user->id(), 'homepage_nid');
|
||||||
|
if (!$homepage_nid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$node = \Drupal::entityTypeManager()->getStorage('node')->load($homepage_nid);
|
||||||
|
if ($node && $node->isPublished() && $node->access('view')) {
|
||||||
|
$variables['content']['#title'] = $node->label();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements hook_preprocess_page().
|
* Implements hook_preprocess_page().
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ class MicrositeUserConfigForm extends FormBase {
|
|||||||
$form_state->set('user', $user);
|
$form_state->set('user', $user);
|
||||||
|
|
||||||
$homepage_nid = $this->userData->get('site_users_microsite', $user->id(), 'homepage_nid');
|
$homepage_nid = $this->userData->get('site_users_microsite', $user->id(), 'homepage_nid');
|
||||||
|
$use_homepage_title = $this->userData->get('site_users_microsite', $user->id(), 'use_homepage_title');
|
||||||
|
|
||||||
$nids = $this->entityTypeManager->getStorage('node')
|
$nids = $this->entityTypeManager->getStorage('node')
|
||||||
->getQuery()
|
->getQuery()
|
||||||
@@ -75,6 +76,13 @@ class MicrositeUserConfigForm extends FormBase {
|
|||||||
'#default_value' => $homepage_nid ?? '',
|
'#default_value' => $homepage_nid ?? '',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
$form['use_homepage_title'] = [
|
||||||
|
'#type' => 'checkbox',
|
||||||
|
'#title' => $this->t('Use homepage content title as page title'),
|
||||||
|
'#description' => $this->t('When checked, the title of the selected homepage content replaces the default page title (your display name).'),
|
||||||
|
'#default_value' => $use_homepage_title ?? FALSE,
|
||||||
|
];
|
||||||
|
|
||||||
$form['actions'] = ['#type' => 'actions'];
|
$form['actions'] = ['#type' => 'actions'];
|
||||||
$form['actions']['submit'] = [
|
$form['actions']['submit'] = [
|
||||||
'#type' => 'submit',
|
'#type' => 'submit',
|
||||||
@@ -98,6 +106,13 @@ class MicrositeUserConfigForm extends FormBase {
|
|||||||
$this->userData->set('site_users_microsite', $user->id(), 'homepage_nid', (int) $nid);
|
$this->userData->set('site_users_microsite', $user->id(), 'homepage_nid', (int) $nid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($form_state->getValue('use_homepage_title')) {
|
||||||
|
$this->userData->set('site_users_microsite', $user->id(), 'use_homepage_title', TRUE);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$this->userData->delete('site_users_microsite', $user->id(), 'use_homepage_title');
|
||||||
|
}
|
||||||
|
|
||||||
Cache::invalidateTags(['site_users_microsite_config:' . $user->id()]);
|
Cache::invalidateTags(['site_users_microsite_config:' . $user->id()]);
|
||||||
$this->messenger()->addStatus($this->t('Configuration saved.'));
|
$this->messenger()->addStatus($this->t('Configuration saved.'));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user