mirror of
https://gitlab.unicamp.br/infimecc_drupal11_modules/site_tools.git
synced 2026-05-03 13:40:41 -03:00
Estende PageUserIsCurrentUser para rotas sem parâmetro 'user'
Cascata de resolução do UID do dono da página:
1. Parâmetro 'user' da rota (comportamento original)
2. Parâmetro 'arg_0' numérico (Views pages, ex.: /user/{uid}/blog)
3. Autor do nó (entity.node.canonical sem parâmetro 'user')
4. Fallback: UID extraído do path atual /user/{uid}/...
Injeta CurrentPathStack para suportar o fallback por path.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ namespace Drupal\site_tools\Plugin\Condition;
|
||||
|
||||
use Drupal\Core\Condition\ConditionPluginBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Path\CurrentPathStack;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
@@ -30,6 +31,7 @@ class PageUserIsCurrentUser extends ConditionPluginBase implements ContainerFact
|
||||
$plugin_definition,
|
||||
protected RouteMatchInterface $routeMatch,
|
||||
protected AccountInterface $currentUser,
|
||||
protected CurrentPathStack $currentPath,
|
||||
) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
}
|
||||
@@ -41,6 +43,7 @@ class PageUserIsCurrentUser extends ConditionPluginBase implements ContainerFact
|
||||
$plugin_definition,
|
||||
$container->get('current_route_match'),
|
||||
$container->get('current_user'),
|
||||
$container->get('path.current'),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -90,13 +93,22 @@ class PageUserIsCurrentUser extends ConditionPluginBase implements ContainerFact
|
||||
$page_uid = (int) $param;
|
||||
}
|
||||
else {
|
||||
// Rotas sem parâmetro 'user' (e.g., nós do microsite em /user/{id}/...):
|
||||
// usa o autor do nó como usuário da página.
|
||||
$node = $this->routeMatch->getParameter('node');
|
||||
if (!($node instanceof NodeInterface)) {
|
||||
// Views pages com arg_0 numérico (ex.: /user/{uid}/blog).
|
||||
$arg0 = $this->routeMatch->getParameter('arg_0');
|
||||
if (is_numeric($arg0)) {
|
||||
$page_uid = (int) $arg0;
|
||||
}
|
||||
// Nós do microsite: usa o autor como usuário da página.
|
||||
elseif (($node = $this->routeMatch->getParameter('node')) instanceof NodeInterface) {
|
||||
$page_uid = (int) $node->getOwnerId();
|
||||
}
|
||||
// Fallback: extrai o UID do path atual /user/{uid}/...
|
||||
elseif (preg_match('#^/user/(\d+)(/|$)#', $this->currentPath->getPath(), $m)) {
|
||||
$page_uid = (int) $m[1];
|
||||
}
|
||||
else {
|
||||
return FALSE;
|
||||
}
|
||||
$page_uid = (int) $node->getOwnerId();
|
||||
}
|
||||
|
||||
return $page_uid === (int) $this->currentUser->id();
|
||||
|
||||
Reference in New Issue
Block a user