mirror of
https://gitlab.unicamp.br/infimecc_drupal11_modules/site_users.git
synced 2026-05-04 00:10:41 -03:00
Adiciona sub-módulo site_users_microsite e tema site_users_microsite_theme
Sub-módulo com ThemeNegotiator, controller de listagem de conteúdo por usuário, formulário de configuração de tipos de conteúdo por papel e serviços registrados. Tema com regiões header, highlighted, tabs, messages, content, sidebar, social e footer; template page.html.twig com header padrão gerado a partir dos dados do usuário (foto, nome, roles) quando a região header estiver vazia. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\site_users_microsite\Controller;
|
||||
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
use Drupal\user\UserInterface;
|
||||
|
||||
/**
|
||||
* Controller para listagem de conteúdo do micro-site do usuário.
|
||||
*/
|
||||
class MicrositeContentController extends ControllerBase {
|
||||
|
||||
/**
|
||||
* Página de listagem de conteúdo do micro-site.
|
||||
*/
|
||||
public function content(UserInterface $user): array {
|
||||
$allowed_types = $this->getAllowedContentTypes($user);
|
||||
|
||||
$cache = [
|
||||
'contexts' => ['user'],
|
||||
'tags' => [
|
||||
'node_list',
|
||||
'user:' . $user->id(),
|
||||
'config:site_users_microsite.settings',
|
||||
],
|
||||
];
|
||||
|
||||
if (empty($allowed_types)) {
|
||||
return [
|
||||
'#markup' => $this->t('No content types are configured for this profile type.'),
|
||||
'#cache' => $cache,
|
||||
];
|
||||
}
|
||||
|
||||
$nids = $this->entityTypeManager()->getStorage('node')
|
||||
->getQuery()
|
||||
->condition('uid', $user->id())
|
||||
->condition('type', $allowed_types, 'IN')
|
||||
->condition('status', 1)
|
||||
->accessCheck(TRUE)
|
||||
->sort('created', 'DESC')
|
||||
->execute();
|
||||
|
||||
if (empty($nids)) {
|
||||
return [
|
||||
'#markup' => $this->t('No content published yet.'),
|
||||
'#cache' => $cache,
|
||||
];
|
||||
}
|
||||
|
||||
$nodes = $this->entityTypeManager()->getStorage('node')->loadMultiple($nids);
|
||||
$build = $this->entityTypeManager()->getViewBuilder('node')->viewMultiple($nodes, 'teaser');
|
||||
$build['#cache'] = $cache;
|
||||
|
||||
return $build;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback de título para a página de conteúdo.
|
||||
*/
|
||||
public function title(UserInterface $user): TranslatableMarkup {
|
||||
return $this->t("@name's content", ['@name' => $user->getDisplayName()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retorna os tipos de conteúdo permitidos para os papéis do usuário.
|
||||
*/
|
||||
protected function getAllowedContentTypes(UserInterface $user): array {
|
||||
$role_content_types = $this->configFactory()
|
||||
->get('site_users_microsite.settings')
|
||||
->get('role_content_types') ?? [];
|
||||
|
||||
$allowed = [];
|
||||
foreach ($user->getRoles() as $role_id) {
|
||||
foreach ($role_content_types[$role_id] ?? [] as $type) {
|
||||
$allowed[$type] = $type;
|
||||
}
|
||||
}
|
||||
|
||||
return array_keys($allowed);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\site_users_microsite\Form;
|
||||
|
||||
use Drupal\Core\Form\ConfigFormBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\user\Entity\Role;
|
||||
|
||||
/**
|
||||
* Formulário de configuração do módulo User Microsite.
|
||||
*/
|
||||
class MicrositeSettingsForm extends ConfigFormBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId(): string {
|
||||
return 'site_users_microsite_settings_form';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getEditableConfigNames(): array {
|
||||
return ['site_users_microsite.settings'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state): array {
|
||||
$config = $this->config('site_users_microsite.settings');
|
||||
$content_types = $this->getContentTypes();
|
||||
|
||||
$form['role_content_types'] = [
|
||||
'#type' => 'fieldset',
|
||||
'#title' => $this->t('Content types per role'),
|
||||
'#description' => $this->t('Select which content types each role can publish in their micro-site.'),
|
||||
'#tree' => TRUE,
|
||||
];
|
||||
|
||||
if (empty($content_types)) {
|
||||
$form['role_content_types']['_empty'] = [
|
||||
'#markup' => '<p>' . $this->t('No content types found.') . '</p>',
|
||||
];
|
||||
}
|
||||
else {
|
||||
foreach (Role::loadMultiple() as $role_id => $role) {
|
||||
if (in_array($role_id, ['anonymous', 'authenticated'])) {
|
||||
continue;
|
||||
}
|
||||
$form['role_content_types'][$role_id] = [
|
||||
'#type' => 'checkboxes',
|
||||
'#title' => $role->label(),
|
||||
'#options' => $content_types,
|
||||
'#default_value' => $config->get('role_content_types.' . $role_id) ?? [],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return parent::buildForm($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state): void {
|
||||
$config = $this->config('site_users_microsite.settings');
|
||||
$raw = $form_state->getValue('role_content_types') ?? [];
|
||||
|
||||
foreach (Role::loadMultiple() as $role_id => $role) {
|
||||
if (in_array($role_id, ['anonymous', 'authenticated']) || !isset($raw[$role_id])) {
|
||||
continue;
|
||||
}
|
||||
$config->set('role_content_types.' . $role_id, array_values(array_filter($raw[$role_id])));
|
||||
}
|
||||
|
||||
$config->save();
|
||||
parent::submitForm($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retorna todos os tipos de conteúdo disponíveis.
|
||||
*/
|
||||
protected function getContentTypes(): array {
|
||||
$options = [];
|
||||
foreach (\Drupal::entityTypeManager()->getStorage('node_type')->loadMultiple() as $type) {
|
||||
$options[$type->id()] = $type->label();
|
||||
}
|
||||
ksort($options);
|
||||
return $options;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\site_users_microsite\Theme;
|
||||
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\Theme\ThemeNegotiatorInterface;
|
||||
|
||||
/**
|
||||
* Aplica o tema microsite nas rotas de perfil de usuário e do micro-site.
|
||||
*/
|
||||
class MicrositeThemeNegotiator implements ThemeNegotiatorInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function applies(RouteMatchInterface $route_match): bool {
|
||||
$route_name = $route_match->getRouteName();
|
||||
return $route_name === 'entity.user.canonical'
|
||||
|| str_starts_with($route_name, 'site_users_microsite.');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function determineActiveTheme(RouteMatchInterface $route_match): ?string {
|
||||
return 'site_users_microsite_theme';
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user