mirror of
https://gitlab.unicamp.br/infimecc_drupal11_modules/site_users.git
synced 2026-05-09 22:53:49 -03:00
Adiciona menu 'Adicionar' configurável no menu da conta do usuário
Item pai 'Adicionar' no menu account com subitens derivados dinamicamente a partir de site_users.settings:add_content_links. O pai fica oculto quando o usuário não tem acesso a nenhum dos routes configurados. - Rota site_users.add_content com _custom_access via AddContentAccessCheck - hook_menu_links_discovered_alter() gera os subitens com IDs estáveis - Formulário de settings com tabela editável (label, rota, parâmetro, peso) - CSS do microsite atualizado com dropdown ao hover/focus-within Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -177,6 +177,66 @@ class SiteUsersSettingsForm extends ConfigFormBase {
|
||||
}
|
||||
}
|
||||
|
||||
// Fieldset para itens do menu "Adicionar".
|
||||
$form['add_content_links'] = [
|
||||
'#type' => 'fieldset',
|
||||
'#title' => $this->t('Add content menu items'),
|
||||
'#description' => $this->t('Configure items shown under the "Adicionar" entry in the account menu. Each item points to an entity add route. Leave "Label" empty to remove the row.'),
|
||||
'#tree' => TRUE,
|
||||
];
|
||||
|
||||
$saved_items = $config->get('add_content_links') ?? [];
|
||||
// Append one blank row for adding a new item.
|
||||
$saved_items[] = ['label' => '', 'route_name' => '', 'route_parameters' => [], 'weight' => 0];
|
||||
|
||||
$form['add_content_links']['table'] = [
|
||||
'#type' => 'table',
|
||||
'#header' => [
|
||||
$this->t('Label'),
|
||||
$this->t('Route name'),
|
||||
$this->t('Parameter name'),
|
||||
$this->t('Parameter value'),
|
||||
$this->t('Weight'),
|
||||
],
|
||||
'#empty' => $this->t('No items yet. Fill in the row below to add one.'),
|
||||
];
|
||||
|
||||
foreach ($saved_items as $delta => $item) {
|
||||
$params = $item['route_parameters'] ?? [];
|
||||
$param_name = array_key_first($params) ?? '';
|
||||
$param_value = $params[$param_name] ?? '';
|
||||
|
||||
$form['add_content_links']['table'][$delta]['label'] = [
|
||||
'#type' => 'textfield',
|
||||
'#default_value' => $item['label'] ?? '',
|
||||
'#size' => 20,
|
||||
'#placeholder' => $this->t('e.g. Artigo'),
|
||||
];
|
||||
$form['add_content_links']['table'][$delta]['route_name'] = [
|
||||
'#type' => 'textfield',
|
||||
'#default_value' => $item['route_name'] ?? '',
|
||||
'#size' => 30,
|
||||
'#placeholder' => 'e.g. node.add',
|
||||
];
|
||||
$form['add_content_links']['table'][$delta]['param_name'] = [
|
||||
'#type' => 'textfield',
|
||||
'#default_value' => $param_name,
|
||||
'#size' => 20,
|
||||
'#placeholder' => 'e.g. node_type',
|
||||
];
|
||||
$form['add_content_links']['table'][$delta]['param_value'] = [
|
||||
'#type' => 'textfield',
|
||||
'#default_value' => $param_value,
|
||||
'#size' => 20,
|
||||
'#placeholder' => 'e.g. article',
|
||||
];
|
||||
$form['add_content_links']['table'][$delta]['weight'] = [
|
||||
'#type' => 'number',
|
||||
'#default_value' => (int) ($item['weight'] ?? 0),
|
||||
'#size' => 4,
|
||||
];
|
||||
}
|
||||
|
||||
return parent::buildForm($form, $form_state);
|
||||
}
|
||||
|
||||
@@ -230,6 +290,32 @@ class SiteUsersSettingsForm extends ConfigFormBase {
|
||||
}
|
||||
}
|
||||
|
||||
// Salvar add_content_links: ignorar linhas sem label ou route_name.
|
||||
$links_raw = $form_state->getValue(['add_content_links', 'table']) ?? [];
|
||||
$add_content_links = [];
|
||||
foreach ($links_raw as $row) {
|
||||
$label = trim($row['label'] ?? '');
|
||||
$route_name = trim($row['route_name'] ?? '');
|
||||
if ($label === '' || $route_name === '') {
|
||||
continue;
|
||||
}
|
||||
$params = [];
|
||||
$param_name = trim($row['param_name'] ?? '');
|
||||
$param_value = trim($row['param_value'] ?? '');
|
||||
if ($param_name !== '') {
|
||||
$params[$param_name] = $param_value;
|
||||
}
|
||||
$add_content_links[] = [
|
||||
'label' => $label,
|
||||
'route_name' => $route_name,
|
||||
'route_parameters' => $params,
|
||||
'weight' => (int) ($row['weight'] ?? 0),
|
||||
];
|
||||
}
|
||||
// Reordena por weight.
|
||||
usort($add_content_links, fn($a, $b) => $a['weight'] <=> $b['weight']);
|
||||
$config->set('add_content_links', $add_content_links);
|
||||
|
||||
// Salvar role_view_modes: apenas os valores marcados (filtrar 0).
|
||||
$role_view_modes_raw = $form_state->getValue('role_view_modes') ?? [];
|
||||
$roles = \Drupal\user\Entity\Role::loadMultiple();
|
||||
|
||||
Reference in New Issue
Block a user