Skip to content

Commit

Permalink
Merge pull request #14610 from jeabakker/60-fixes
Browse files Browse the repository at this point in the history
fix(widgets): set correct widget URLs
  • Loading branch information
jdalsem committed Apr 25, 2024
2 parents 3e20311 + e5ceaf6 commit a365731
Show file tree
Hide file tree
Showing 46 changed files with 479 additions and 125 deletions.
5 changes: 5 additions & 0 deletions docs/guides/events-list.rst
Expand Up @@ -1209,6 +1209,11 @@ Other

* ``entity`` - entity that owns the icons

**entity:url, <entity_type>:<entity_subtype>** |results|
Return the URL for the entity ``$params['entity']``. Note: Generally it is better to override the
``getUrl()`` method of ElggEntity. This event should be used when it's not possible to subclass
(like if you want to extend a bundled plugin without overriding many views).

**entity:url, <entity_type>** |results|
Return the URL for the entity ``$params['entity']``. Note: Generally it is better to override the
``getUrl()`` method of ElggEntity. This event should be used when it's not possible to subclass
Expand Down
26 changes: 17 additions & 9 deletions engine/classes/Elgg/Widgets/EntityUrlHandler.php
Expand Up @@ -12,29 +12,37 @@ class EntityUrlHandler {
/**
* Returns widget URLS used in widget titles
*
* @param \Elgg\Event $event 'entity:url', 'object'
* @param \Elgg\Event $event 'entity:url', 'object:widget'
*
* @return void|string
* @return null|string
*/
public function __invoke(\Elgg\Event $event) {
public function __invoke(\Elgg\Event $event): ?string {
$widget = $event->getEntityParam();
if (!$widget instanceof \ElggWidget) {
return;
return null;
}

$admin_segments = null;
switch ($widget->handler) {
case 'content_stats':
return 'admin/statistics';
$admin_segments = 'statistics';
break;
case 'cron_status':
return 'admin/cron';
$admin_segments = 'cron';
break;
case 'banned_users':
return 'admin/users/banned';
$admin_segments = 'users/banned';
break;
case 'new_users':
return 'admin/users';
$admin_segments = 'users';
break;
case 'online_users':
return 'admin/users/online';
$admin_segments = 'users/online';
break;
case 'elgg_blog':
return 'https://elgg.org/blog/all';
}

return $admin_segments ? elgg_generate_url('admin', ['segments' => $admin_segments]) : null;
}
}
2 changes: 1 addition & 1 deletion engine/classes/ElggEntity.php
Expand Up @@ -1109,8 +1109,8 @@ public function getTimeUpdated(): int {
public function getURL(): string {
$url = elgg_generate_entity_url($this, 'view');

$url = _elgg_services()->events->triggerResults('entity:url', "{$this->getType()}:{$this->getSubtype()}", ['entity' => $this], $url);
$url = _elgg_services()->events->triggerResults('entity:url', $this->getType(), ['entity' => $this], $url);

if (empty($url)) {
return '';
}
Expand Down
2 changes: 1 addition & 1 deletion engine/events.php
Expand Up @@ -155,7 +155,7 @@
],
],
'entity:url' => [
'object' => [
'object:widget' => [
\Elgg\Widgets\EntityUrlHandler::class => [],
],
],
Expand Down
65 changes: 57 additions & 8 deletions mod/activity/classes/Elgg/Activity/Widgets.php
Expand Up @@ -10,22 +10,71 @@ class Widgets {
/**
* Set the title URL for the activity widget
*
* @param \Elgg\Event $event 'entity:url', 'object'
* @param \Elgg\Event $event 'entity:url', 'object:widget'
*
* @return void|string
* @return null|string
*/
public static function widgetURL(\Elgg\Event $event) {
$return_value = $event->getValue();
if (!empty($return_value)) {
public static function riverWidgetURL(\Elgg\Event $event): ?string {
if (!empty($event->getValue())) {
// someone already set an url
return;
return null;
}

$widget = $event->getEntityParam();
if (!$widget instanceof \ElggWidget || $widget->handler !== 'river_widget') {
return;
return null;
}

return elgg_generate_url('default:river');
$owner = $widget->getOwnerEntity();
if ($owner instanceof \ElggGroup) {
return elgg_generate_url('collection:river:group', [
'guid' => $owner->guid,
]);
} elseif ($owner instanceof \ElggUser) {
if ($widget->context === 'dashboard') {
if ($widget->content_type === 'all') {
return elgg_generate_url('collection:river:all');
}

return elgg_generate_url('collection:river:friends', [
'username' => $owner->username,
]);
}

return elgg_generate_url('collection:river:owner', [
'username' => $owner->username,
]);
}

return elgg_generate_url('collection:river:all');
}

/**
* Set the title URL for the group activity widget
*
* @param \Elgg\Event $event 'entity:url', 'object:widget'
*
* @return null|string
*/
public static function groupWidgetURL(\Elgg\Event $event): ?string {
if (!empty($event->getValue())) {
// someone already set an url
return null;
}

$widget = $event->getEntityParam();
if (!$widget instanceof \ElggWidget || $widget->handler !== 'group_activity') {
return null;
}

$group_guid = (int) $widget->group_guid;
$group = get_entity($group_guid);
if (!$group instanceof \ElggGroup) {
return null;
}

return elgg_generate_url('collection:river:group', [
'guid' => $group->guid,
]);
}
}
5 changes: 3 additions & 2 deletions mod/activity/elgg-plugin.php
Expand Up @@ -62,8 +62,9 @@
],
'events' => [
'entity:url' => [
'object' => [
'Elgg\Activity\Widgets::widgetURL' => [],
'object:widget' => [
'Elgg\Activity\Widgets::groupWidgetURL' => [],
'Elgg\Activity\Widgets::riverWidgetURL' => [],
],
],
'register' => [
Expand Down
1 change: 1 addition & 0 deletions mod/activity/languages/en.php
Expand Up @@ -23,4 +23,5 @@
'groups:tool:activity' => 'Enable group activity',
'groups:tool:activity:description' => 'Show an activity feed about group related content.',

'activity:more' => "More activity",
);
9 changes: 7 additions & 2 deletions mod/activity/views/default/widgets/group_activity/content.php
Expand Up @@ -3,16 +3,20 @@
* Group activity widget
*/

/* @var $widget \ElggWidget */
$widget = elgg_extract('entity', $vars);
if (!$widget instanceof \ElggWidget) {
return;
}

$num = (int) $widget->num_display ?: 8;
$guid = (int) $widget->group_guid;

$group = get_entity($guid);
if (!$group instanceof ElggGroup) {
// no group selected yet
echo '<p>' . elgg_echo('widgets:group_activity:content:noselect') . '</p>';
echo elgg_view('page/components/no_results', [
'no_results' => elgg_echo('widgets:group_activity:content:noselect'),
]);
return;
}

Expand All @@ -22,5 +26,6 @@
'limit' => $num,
'pagination' => false,
'no_results' => elgg_echo('widgets:group_activity:content:noactivity'),
'widget_more' => elgg_view_url($widget->getURL(), elgg_echo('activity:more')),
],
]);
16 changes: 15 additions & 1 deletion mod/activity/views/default/widgets/group_activity/edit.php
Expand Up @@ -12,14 +12,27 @@
$owner = elgg_get_logged_in_user_entity();
}

$groups = $owner->getGroups(['limit' => false]);
/* @var $groups \ElggBatch */
$groups = $owner->getGroups([
'limit' => false,
'batch' => true,
'sort_by' => [
'property' => 'name',
'direction' => 'ASC',
],
]);

$mygroups = [];
if (!$widget->group_guid) {
$mygroups[0] = '';
}

/* @var $group \ElggGroup */
foreach ($groups as $group) {
if (!$group->isToolEnabled('activity')) {
continue;
}

$mygroups[$group->guid] = $group->getDisplayName();
}

Expand All @@ -29,6 +42,7 @@
'name' => 'params[group_guid]',
'value' => $widget->group_guid,
'options_values' => $mygroups,
'required' => true,
]);

echo elgg_view('object/widget/edit/num_display', [
Expand Down
5 changes: 4 additions & 1 deletion mod/activity/views/default/widgets/river_widget/content.php
Expand Up @@ -3,14 +3,17 @@
* Activity widget content view
*/

/* @var $widget \ElggWidget */
$widget = elgg_extract('entity', $vars);
if (!$widget instanceof \ElggWidget) {
return;
}

$num_display = (int) $widget->num_display ?: 8;

$options = [
'limit' => $num_display,
'pagination' => false,
'widget_more' => elgg_view_url($widget->getURL(), elgg_echo('activity:more')),
];

if ($widget->context === 'dashboard') {
Expand Down
41 changes: 41 additions & 0 deletions mod/blog/classes/Elgg/Blog/Widgets.php
@@ -0,0 +1,41 @@
<?php

namespace Elgg\Blog;

/**
* Widget related functions
*/
class Widgets {

/**
* Set the URL for the blog widget
*
* @param \Elgg\Event $event 'entity:url', 'object:widget'
*
* @return null|string
*/
public static function blogWidgetUrl(\Elgg\Event $event): ?string {
if (!empty($event->getValue())) {
// url already provided
return null;
}

$widget = $event->getEntityParam();
if (!$widget instanceof \ElggWidget || $widget->handler !== 'blog') {
return null;
}

$owner = $widget->getOwnerEntity();
if ($owner instanceof \ElggGroup) {
return elgg_generate_url('collection:object:blog:group', [
'guid' => $owner->guid,
]);
} elseif ($owner instanceof \ElggUser) {
return elgg_generate_url('collection:object:blog:owner', [
'username' => $owner->username,
]);
}

return elgg_generate_url('collection:object:blog:all');
}
}
5 changes: 5 additions & 0 deletions mod/blog/elgg-plugin.php
Expand Up @@ -110,6 +110,11 @@
GroupToolContainerLogicCheck::class => [],
],
],
'entity:url' => [
'object:widget' => [
'Elgg\Blog\Widgets::blogWidgetUrl' => [],
],
],
'form:prepare:fields' => [
'blog/save' => [
PrepareFields::class => [],
Expand Down
13 changes: 5 additions & 8 deletions mod/blog/views/default/widgets/blog/content.php
Expand Up @@ -3,8 +3,10 @@
* User blog widget display view
*/

/* @var $widget \ElggWidget */
$widget = elgg_extract('entity', $vars);
if (!$widget instanceof \ElggWidget) {
return;
}

$num_display = (int) $widget->num_display ?: 4;

Expand All @@ -15,19 +17,14 @@
'pagination' => false,
'distinct' => false,
'no_results' => elgg_echo('blog:none'),
'widget_more' => elgg_view_url($widget->getURL(), elgg_echo('blog:moreblogs')),
];

$owner = $widget->getOwnerEntity();
if ($owner instanceof \ElggUser) {
$options['owner_guid'] = $owner->guid;
$url = elgg_generate_url('collection:object:blog:owner', ['username' => $owner->username]);
} elseif ($owner instanceof \ElggGroup) {
$options['container_guid'] = $widget->owner_guid;
$url = elgg_generate_url('collection:object:blog:group', ['guid' => $owner->guid]);
} else {
$url = elgg_generate_url('collection:object:blog:all');
$options['container_guid'] = $owner->guid;
}

$options['widget_more'] = elgg_view_url($url, elgg_echo('blog:moreblogs'));

echo elgg_list_entities($options);
12 changes: 7 additions & 5 deletions mod/bookmarks/classes/Elgg/Bookmarks/Widgets.php
Expand Up @@ -10,20 +10,20 @@ class Widgets {
/**
* Set the title URL for the bookmarks widgets
*
* @param \Elgg\Event $event 'entity:url', 'object'
* @param \Elgg\Event $event 'entity:url', 'object:widget'
*
* @return void|string
* @return null|string
*/
public static function widgetURL(\Elgg\Event $event) {
public static function widgetURL(\Elgg\Event $event): ?string {
$return_value = $event->getValue();
if (!empty($return_value)) {
// someone already set an url
return;
return null;
}

$widget = $event->getEntityParam();
if (!$widget instanceof \ElggWidget || $widget->handler !== 'bookmarks') {
return;
return null;
}

$owner = $widget->getOwnerEntity();
Expand All @@ -36,5 +36,7 @@ public static function widgetURL(\Elgg\Event $event) {
'username' => $owner->username,
]);
}

return elgg_generate_url('collection:object:bookmarks:all');
}
}
2 changes: 1 addition & 1 deletion mod/bookmarks/elgg-plugin.php
Expand Up @@ -98,7 +98,7 @@
],
],
'entity:url' => [
'object' => [
'object:widget' => [
'Elgg\Bookmarks\Widgets::widgetURL' => [],
],
],
Expand Down

0 comments on commit a365731

Please sign in to comment.