XF 2.1 Retrieving XFMG URLs outside of XF

dknife

Well-known member
My site outside of XenForo utilizes the session and content framework of XF, mainly for the media gallery portions, displaying alerts and stuff like recent threads etc. I've been converting my dev site to XF2.1 and very very slowly wrapping my head around the new class structure and trying to utilize as much of the entity and repo system as possible.

After messing around for a couple of hours tonight, I think I've managed to utilize the existing functions correctly to check for the image, check permissions and get the necessary canonical URLs.

This is to retrieve the media info from a media ID
PHP:
/** @var \XFMG\Entity\MediaItem $mediaItem */
$mediaItem = \XF::em()->find('XFMG:MediaItem', $g['media_id']);
if (!empty($mediaItem) && $mediaItem->canView() === true && $mediaItem->isVisible() === true) {
    $image_data = $mediaItem->getStructuredData();
    $thumb_url = $image_data['thumbnailUrl'];
    $full_url = $image_data['contentUrl'];
}

Is this sufficient to work with the abstract data of XF2 and to ensure permissions are followed for the image?
 
Assuming the correct visitor object is available (you can check \XF::visitor()), then it should be sufficient to check $mediaItem->canView(). Checking $mediaItem->isVisible() may exclude records that are normally visible to a member, such as moderated media items in the case of moderators. I would only check that if you want to ensure a media item is not moderated or deleted in addition to the permission checks.

I gather you've figured as much out already, but to generate full URLs you would do:
PHP:
$router = \XF::app()->router('public');
$link = $router->buildLink('canonical:media', $mediaItem);
 
Assuming the correct visitor object is available (you can check \XF::visitor()), then it should be sufficient to check $mediaItem->canView(). Checking $mediaItem->isVisible() may exclude records that are normally visible to a member, such as moderated media items in the case of moderators. I would only check that if you want to ensure a media item is not moderated or deleted in addition to the permission checks.
ok thanks, and yes the visitor object is available. It's critical to my site function outside of XF.

I gather you've figured as much out already, but to generate full URLs you would do:
I hadn't known the :media option for generating the canonical URL. There are a few places where I link to the page as well so that's also useful. Thanks!
 
I hadn't known the :media option for generating the canonical URL. There are a few places where I link to the page as well so that's also useful. Thanks!
The media portion is actually the route prefix, so you can usually drop-in any route there (threads, posts, etc) along with an entity to get the corresponding link :)
 
Top Bottom