mirror of
https://gitlab.unicamp.br/infimecc_drupal11_modules/site_users.git
synced 2026-05-07 00:05:30 -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:
18
src/Plugin/Menu/AddContentMenuLink.php
Normal file
18
src/Plugin/Menu/AddContentMenuLink.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\site_users\Plugin\Menu;
|
||||
|
||||
use Drupal\Core\Menu\MenuLinkDefault;
|
||||
|
||||
/**
|
||||
* Provides derived menu links for add-content actions in the account menu.
|
||||
*
|
||||
* @MenuLink(
|
||||
* id = "site_users.add_content_child",
|
||||
* deriver = "Drupal\site_users\Plugin\Menu\AddContentMenuLinkDeriver"
|
||||
* )
|
||||
*/
|
||||
class AddContentMenuLink extends MenuLinkDefault {
|
||||
}
|
||||
64
src/Plugin/Menu/AddContentMenuLinkDeriver.php
Normal file
64
src/Plugin/Menu/AddContentMenuLinkDeriver.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\site_users\Plugin\Menu;
|
||||
|
||||
use Drupal\Component\Plugin\Derivative\DeriverBase;
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Generates account menu "Adicionar" child links from site_users.settings.
|
||||
*/
|
||||
class AddContentMenuLinkDeriver extends DeriverBase implements ContainerDeriverInterface {
|
||||
|
||||
public function __construct(
|
||||
private readonly ConfigFactoryInterface $configFactory,
|
||||
) {}
|
||||
|
||||
public static function create(ContainerInterface $container, $base_plugin_id): static {
|
||||
return new static($container->get('config.factory'));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDerivativeDefinitions($base_plugin_definition): array {
|
||||
$this->derivatives = [];
|
||||
|
||||
$items = $this->configFactory
|
||||
->get('site_users.settings')
|
||||
->get('add_content_links') ?? [];
|
||||
|
||||
foreach ($items as $item) {
|
||||
if (empty($item['route_name']) || empty($item['label'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = $this->buildId($item);
|
||||
$this->derivatives[$id] = $base_plugin_definition;
|
||||
$this->derivatives[$id]['title'] = $item['label'];
|
||||
$this->derivatives[$id]['route_name'] = $item['route_name'];
|
||||
$this->derivatives[$id]['route_parameters'] = $item['route_parameters'] ?? [];
|
||||
$this->derivatives[$id]['weight'] = (int) ($item['weight'] ?? 0);
|
||||
$this->derivatives[$id]['menu_name'] = 'account';
|
||||
$this->derivatives[$id]['parent'] = 'site_users.add_content';
|
||||
}
|
||||
|
||||
return $this->derivatives;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a stable derivative ID from the item's route and parameters.
|
||||
*/
|
||||
private function buildId(array $item): string {
|
||||
$parts = [preg_replace('/[^a-z0-9]/', '_', strtolower($item['route_name']))];
|
||||
foreach ($item['route_parameters'] ?? [] as $value) {
|
||||
$parts[] = preg_replace('/[^a-z0-9]/', '_', strtolower((string) $value));
|
||||
}
|
||||
return implode('_', $parts);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user