[TWIG] Remove kludge event TwigPopulateVars

This commit is contained in:
Diogo Peralta Cordeiro 2021-09-18 07:27:17 +01:00
parent fda998e335
commit 67d62cf37b
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
15 changed files with 112 additions and 147 deletions

View File

@ -19,30 +19,8 @@
namespace Component\Left; namespace Component\Left;
use App\Core\Event;
use App\Core\Log;
use App\Core\Modules\Component; use App\Core\Modules\Component;
use App\Util\Common;
use Exception;
class Left extends Component class Left extends Component
{ {
public function onEndTwigPopulateVars(array &$vars)
{
try {
$user = Common::user();
if ($user != null) {
$actor = $user->getActor();
$vars['user_id'] = $user->getId();
$vars['user_nickname'] = $user->getNickname();
$vars['user_tags'] = $actor->getSelfTags();
$vars['user_followers'] = $actor->getFollowersCount();
$vars['user_followed'] = $actor->getFollowedCount();
$vars['user_avatar'] = $actor->getAvatarUrl();
}
} catch (Exception $e) {
Log::error('Got an exception while populating variables for the left panel: ' . $e);
}
return Event::next;
}
} }

View File

@ -53,7 +53,7 @@ class Posting extends Component
* @throws RedirectException * @throws RedirectException
* @throws ServerException * @throws ServerException
*/ */
public function onStartTwigPopulateVars(array &$vars): bool public function onAppendRightPostingBlock(array $vars, array &$res): bool
{ {
if (($user = Common::user()) === null) { if (($user = Common::user()) === null) {
return Event::next; return Event::next;
@ -106,7 +106,7 @@ class Posting extends Component
} }
} }
$vars['post_form'] = $form->createView(); $res = $form->createView();
return Event::next; return Event::next;
} }

View File

@ -19,14 +19,8 @@
namespace Component\Right; namespace Component\Right;
use App\Core\Event;
use App\Core\Modules\Component; use App\Core\Modules\Component;
use App\Util\Common;
class Right extends Component class Right extends Component
{ {
public function onStartTwigPopulateVars(array &$vars)
{
return Event::next;
}
} }

View File

@ -71,19 +71,19 @@ class Cover extends Plugin
* @param array $vars * @param array $vars
* *
* @return bool hook value; true means continue processing, false means stop. * @return bool hook value; true means continue processing, false means stop.
*/ *
public function onStartTwigPopulateVars(array &$vars): bool * public function onStartTwigPopulateVars(array &$vars): bool
{ * {
/*if (Common::user() != null) { * if (Common::user() != null) {
$cover = DB::find('cover', ['actor_id' => Common::user()->getId()]); * $cover = DB::find('cover', ['actor_id' => Common::user()->getId()]);
if ($cover != null) { * if ($cover != null) {
$vars['profile_extras'][] = ['name' => 'cover', 'vars' => ['img' => '/cover']]; * $vars['profile_extras'][] = ['name' => 'cover', 'vars' => ['img' => '/cover']];
} else { * } else {
$vars['profile_extras'][] = ['name' => 'cover', 'vars' => []]; * $vars['profile_extras'][] = ['name' => 'cover', 'vars' => []];
} * }
}*/ * }
return Event::next; * return Event::next;
} * }*/
/** /**
* Output our dedicated stylesheet * Output our dedicated stylesheet

View File

@ -24,6 +24,7 @@ namespace Plugin\Directory;
use App\Core\Event; use App\Core\Event;
use App\Core\Modules\Plugin; use App\Core\Modules\Plugin;
use App\Core\Router\RouteLoader; use App\Core\Router\RouteLoader;
use App\Core\Router\Router;
class Directory extends Plugin class Directory extends Plugin
{ {
@ -39,17 +40,16 @@ class Directory extends Plugin
} }
/** /**
* Populate twig vars * Add Links to menu
* *
* @param array $vars * @param array $res out menu items
* *
* @return bool hook value; true means continue processing, false means stop. * @return bool hook value; true means continue processing, false means stop.
*/ */
public function onStartTwigPopulateVars(array &$vars): bool public function onAddMainNavigationItem(array $vars, array &$res): bool
{ {
$vars['main_nav_tabs'][] = ['title' => 'Actors', 'route' => 'directory_actors']; $res[] = ['title' => 'Actors', 'path' => Router::url('directory_actors', [])];
$vars['main_nav_tabs'][] = ['title' => 'Groups', 'route' => 'directory_groups']; $res[] = ['title' => 'Groups', 'path' => Router::url('directory_groups', [])];
return Event::next; return Event::next;
} }
} }

View File

@ -27,12 +27,12 @@ use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Core\Modules\NoteHandlerPlugin; use App\Core\Modules\NoteHandlerPlugin;
use App\Core\Router\RouteLoader; use App\Core\Router\RouteLoader;
use App\Core\Router\Router;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\InvalidFormException; use App\Util\Exception\InvalidFormException;
use App\Util\Exception\NoSuchNoteException; use App\Util\Exception\NoSuchNoteException;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use App\Util\Formatting;
use App\Util\Nickname; use App\Util\Nickname;
use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
@ -114,12 +114,10 @@ class Favourite extends NoteHandlerPlugin
return Event::next; return Event::next;
} }
public function onInsertLeftPanelLink(string $user_nickname, &$res): bool public function onAddProfileNavigationItem(array $vars, array &$res): bool
{ {
$res[] = Formatting::twigRenderString(<<<END $res[] = ['title' => 'Favourites', 'path' => Router::url('actor_favourites_nickname', ['nickname' => $vars['nickname']])];
<a href="{{ path("actor_favourites_nickname", {'nickname' : user_nickname}) }}" class='hover-effect {{ active("favourites") }}'>Favourites</a> $res[] = ['title' => 'Reverse Favourites', 'path' => Router::url('actor_reverse_favourites_nickname', ['nickname' => $vars['nickname']])];
<a href="{{ path("actor_reverse_favourites_nickname", {'nickname' : user_nickname}) }}" class='hover-effect {{ active("reverse_favourites") }}'>Reverse Favs</a>
END, ['user_nickname' => $user_nickname]);
return Event::next; return Event::next;
} }

View File

@ -70,14 +70,14 @@ class Poll extends NoteHandlerPlugin
* @param array $vars * @param array $vars
* *
* @return bool hook value; true means continue processing, false means stop. * @return bool hook value; true means continue processing, false means stop.
*/ *
public function onStartTwigPopulateVars(array &$vars): bool * public function onStartTwigPopulateVars(array &$vars): bool
{ * {
$vars['tabs'][] = ['title' => 'Poll', * $vars['tabs'][] = ['title' => 'Poll',
'href' => 'newpoll', * 'href' => 'newpoll',
]; * ];
return Event::next; * return Event::next;
} * }*/
/** /**
* Output our dedicated stylesheet * Output our dedicated stylesheet

View File

@ -71,22 +71,22 @@ class ProfileColor extends Plugin
* @param array $vars * @param array $vars
* *
* @return bool hook value; true means continue processing, false means stop. * @return bool hook value; true means continue processing, false means stop.
*/ *
public function onStartTwigPopulateVars(array &$vars): bool * public function onStartTwigPopulateVars(array &$vars): bool
{ * {
/*$vars['profile_tabs'][] = [ * /*$vars['profile_tabs'][] = [
'title' => 'Color', * 'title' => 'Color',
'desc' => 'Change your profile color.', * 'desc' => 'Change your profile color.',
'path' => 'profilecolor/profilecolor.html.twig', * 'path' => 'profilecolor/profilecolor.html.twig',
];*/ * ];
if (Common::user() != null) { * if (Common::user() != null) {
$color = DB::find('profile_color', ['actor_id' => Common::user()->getId()]); * $color = DB::find('profile_color', ['actor_id' => Common::user()->getId()]);
if ($color != null) { * if ($color != null) {
$vars['profile_extras'][] = ['name' => 'profilecolor', 'vars' => ['color' => $color->getColor()]]; * $vars['profile_extras'][] = ['name' => 'profilecolor', 'vars' => ['color' => $color->getColor()]];
} * }
} * }
return Event::next; * return Event::next;
} * }*/
/** /**
* Output our dedicated stylesheet * Output our dedicated stylesheet

View File

@ -89,8 +89,12 @@ class Controller extends AbstractController implements EventSubscriberInterface
$request = $event->getRequest(); $request = $event->getRequest();
$this->request = $request; $this->request = $request;
$this->vars = ['controller' => $controller, 'request' => $request, 'have_user' => Common::user() !== null];
Event::handle('StartTwigPopulateVars', [&$this->vars]); $this->vars = ['controller' => $controller, 'request' => $request];
$user = Common::user();
if ($user !== null) {
$this->vars['current_actor'] = $user->getActor();
}
$event->stopPropagation(); $event->stopPropagation();
return $event; return $event;
@ -111,10 +115,9 @@ class Controller extends AbstractController implements EventSubscriberInterface
} }
$this->vars = array_merge_recursive($this->vars, $response); $this->vars = array_merge_recursive($this->vars, $response);
Event::handle('EndTwigPopulateVars', [&$this->vars]);
$template = $this->vars['_template']; $template = $this->vars['_template'];
unset($this->vars['_template'], $this->vars['request'], $response['_template']); unset($this->vars['_template'], $response['_template']);
// Respond in the most preferred acceptable content type // Respond in the most preferred acceptable content type
$accept = $request->getAcceptableContentTypes() ?: ['text/html']; $accept = $request->getAcceptableContentTypes() ?: ['text/html'];

View File

@ -265,7 +265,7 @@ class Actor extends Entity
Cache::delete('selftags-' . $this->id); Cache::delete('selftags-' . $this->id);
} }
public function getFollowersCount() public function getSubscribersCount()
{ {
return Cache::get('followers-' . $this->id, return Cache::get('followers-' . $this->id,
function () { function () {
@ -274,7 +274,7 @@ class Actor extends Entity
}); });
} }
public function getFollowedCount() public function getSubscriptionsCount()
{ {
return Cache::get('followed-' . $this->id, return Cache::get('followed-' . $this->id,
function () { function () {

View File

@ -113,7 +113,7 @@ abstract class Common
public static function actor(): ?Actor public static function actor(): ?Actor
{ {
return self::user()->getActor(); return self::user()?->getActor();
} }
public static function userNickname(): ?string public static function userNickname(): ?string

View File

@ -8,7 +8,7 @@
<div class="h-feed hfeed notes"> <div class="h-feed hfeed notes">
{% if notes is defined and notes is not empty %} {% if notes is defined and notes is not empty %}
{% for conversation in notes %} {% for conversation in notes %}
{% include '/note/view.html.twig' with {'note': conversation['note'], 'have_user': have_user, 'replies': conversation['replies']} only %} {% include '/note/view.html.twig' with {'note': conversation['note'], 'replies': conversation['replies']} only %}
<hr tabindex="0" title="{{ 'End of note and replies.' | trans }}"> <hr tabindex="0" title="{{ 'End of note and replies.' | trans }}">
{% endfor %} {% endfor %}
{% else %} {% else %}

View File

@ -16,7 +16,6 @@
{% if app.user %} {% if app.user %}
<div class="note-actions"> <div class="note-actions">
{% if have_user %}
{% for current_action in get_note_actions(note) %} {% for current_action in get_note_actions(note) %}
{{ form_start(current_action) }} {{ form_start(current_action) }}
{% if current_action.submit_favourite is defined %} {% if current_action.submit_favourite is defined %}
@ -30,7 +29,6 @@
{% endif %} {% endif %}
{{ form_end(current_action) }} {{ form_end(current_action) }}
{% endfor %} {% endfor %}
{% endif %}
</div> </div>
{% endif %} {% endif %}

View File

@ -6,12 +6,13 @@
<aside class="panel-content accessibility-target"> <aside class="panel-content accessibility-target">
{% if app.user %} {% if app.user %}
<section class='section-widget section-widget-padded' title="{{ 'Your profile information.' | trans }}"> <section class='section-widget section-widget-padded' title="{{ 'Your profile information.' | trans }}">
<a id="user" href="{{ path('actor_view_nickname', {'nickname' : user_nickname}) }}"> <a id="user" href="{{ path('actor_view_nickname', {'nickname' : current_actor.getNickname()}) }}">
<img src='{{ user_avatar }}' class="icon icon-avatar" alt="{{ 'Your account\'s avatar.' | trans }}"> <img src='{{ current_actor.getAvatarUrl() }}' class="icon icon-avatar" alt="{{ 'Your account\'s avatar.' | trans }}">
<div class="user-info"> <div class="user-info">
<strong id="user-nickname" title="{{ 'Your account\' nickname.' | trans }}">{{ user_nickname }}</strong> <strong id="user-nickname" title="{{ 'Your account\' nickname.' | trans }}">{{ current_actor.getNickname() }}</strong>
<nav class="user-tags"> <nav class="user-tags">
{% set user_tags = current_actor.getSelfTags() %}
{% if user_tags %} {% if user_tags %}
{% for tag in user_tags %} {% for tag in user_tags %}
<a href='#'> #{{ tag }}</a> <a href='#'> #{{ tag }}</a>
@ -22,28 +23,26 @@
</nav> </nav>
<section id="user-stats"> <section id="user-stats">
{% if user_followers %}<h2>{{ 'Followers' | trans }} {{ user_followers }}</h2> {% endif %} <h2>{{ 'Subscriptions' | trans }} {{ current_actor.getSubscriptionsCount() }}</h2>
{% if user_followed %}<h2>{{ 'Followed' | trans }} {{ user_followed }}</h2> {% endif %} <h2>{{ 'Subscribers' | trans }} {{ current_actor.getSubscribersCount() }}</h2>
</section> </section>
</div> </div>
</a> </a>
{# {% for extra in profile_extras %}
{% include '/'~ extra.name ~ '/view.html.twig' with {'vars': extra.vars} only %}
{% endfor %} #}
<nav tabindex="0" class="profile-navigation" title="{{ 'Navigate through account related pages.' | trans }}"> <nav tabindex="0" class="profile-navigation" title="{{ 'Navigate through account related pages.' | trans }}">
<a title='{{ 'Your messages.' | trans }}' href='{{ path('settings') }}' class='{{ active("replies") }}'> <a title='{{ 'Your messages.' | trans }}' href='{{ path('settings') }}' class='{{ active("replies") }}'>
Messages Messages
</a> </a>
<a title='{{ 'Replies to your notes.' | trans }}' href="{{ path('replies', {'nickname' : user_nickname}) }}" class='{{ active("replies") }}'> <a title='{{ 'Replies to your notes.' | trans }}' href="{{ path('replies', {'nickname' : current_actor.getNickname()}) }}" class='{{ active("replies") }}'>
Replies Replies
</a> </a>
{% for link in handle_event('InsertLeftPanelLink', user_nickname) %}
{{ link | raw }} {% for tab in handle_event('AddProfileNavigationItem', {'nickname': current_actor.getNickname()}) %}
<a href="{{ tab['path'] }}">
{{ tab['title'] }}
</a>
{% endfor %} {% endfor %}
<a title='{{ 'Access your account settings.' | trans }}' href="{{ path('settings') }}" class='{{ active('settings') }}'> <a title='{{ 'Access your account settings.' | trans }}' href="{{ path('settings') }}" class='{{ active('settings') }}'>
@ -85,8 +84,8 @@
</a> </a>
</li> </li>
<li> <li>
{% if user_nickname is defined %} {% if app.user %}
<a href="{{ path("home_all", {'nickname' : user_nickname}) }}" class='{{ active("home_all") }}'> <a href="{{ path("home_all", {'nickname' : current_actor.getNickname()}) }}" class='{{ active("home_all") }}'>
Home Home
</a> </a>
{% endif %} {% endif %}
@ -97,11 +96,11 @@
</a> </a>
</li> </li>
{% if main_nav_tabs is defined %} {% if app.user %}
{% for tab in main_nav_tabs %} {% for link in handle_event('AddMainNavigationItem', {'nickname': current_actor.getNickname()}) %}
<li> <li>
<a href="{{ path(tab['route']) }}" class='{{ active(tab['route']) }}'> <a href="{{ link['path'] }}">
{{ tab['title'] }} {{ link['title'] }}
</a> </a>
</li> </li>
{% endfor %} {% endfor %}

View File

@ -5,16 +5,12 @@
<a id="anchor-right-panel" class="anchor-hidden" title="{{ 'Press tab to access selected region!' | trans }}"></a> <a id="anchor-right-panel" class="anchor-hidden" title="{{ 'Press tab to access selected region!' | trans }}"></a>
<aside class="panel-content accessibility-target"> <aside class="panel-content accessibility-target">
{% if post_form is defined and not post_form.isRendered() %} {% set post_form = handle_event('AppendRightPostingBlock', {'request': request}) %}
<section class="section-widget" title={{ 'Create a new note.' | trans }}> <section class="section-widget" title={{ 'Create a new note.' | trans }}>
<details class="section-title-details" title={{ 'Expand if you want to access more options.' | trans }}> <details class="section-title-details" title={{ 'Expand if you want to access more options.' | trans }}>
<summary class="section-title-summary"> <summary class="section-title-summary">
{{ "Create a note" | trans }}{{ icon('arrow-down', 'icon icon-details-open') | raw }} {{ "Create a note" | trans }}{{ icon('arrow-down', 'icon icon-details-open') | raw }}
</summary> </summary>
{% for tab in tabs %}
<a class="section-title-menu-element" href={{ path(tab['href']) }}>{{ tab['title'] }}</a>
{% endfor %}
</details> </details>
<div class="section-form"> <div class="section-form">
@ -37,7 +33,6 @@
</fieldset> </fieldset>
</div> </div>
</section> </section>
{% endif %}
{% set current_path = app.request.get('_route') %} {% set current_path = app.request.get('_route') %}
{% if right_panel_vars is defined %} {% if right_panel_vars is defined %}