Implemented Entity::getEntityTitle/Entity::getEntityUrl/Entity::getEntityRoute helpers

Xon

Well-known member
As a developer, I often need to be able get a few very simple attributes (title/url to content/route prefix) for XF entities which need to be constantly re-implemented in a few spots.

This also crops up in the XF code-base too.

It would be great is there was standard helpers getEntityTitle/getEntityUrl/getEntityRoute which fetched this information, and some sort of interface to delimitate this feature-set is available.
 
Upvote 10
This suggestion has been implemented. Votes are no longer accepted.

DragonByte Tech

Well-known member
Look at this mess:
PHP:
/**
     * @return string
     */
    public function getContentLink()
    {
        if (empty($this->content_type) || empty($this->content_id))
        {
            return '';
        }

        $router = $this->app()->router('public');

        switch ($this->content_type)
        {
            case 'conversation_message':
                return '<a href="' . $router->buildLink('conversations/messages', ['message_id' => $this->content_id]) . '">' .
                    \XF::phrase('dbtech_credits_conversation_message_x', ['position' => $this->content_id]) .
                    '</a>';
                break;

// [...]

            case 'post':
                return '<a href="' . $router->buildLink('posts', ['post_id' => $this->content_id]) . '">' .
                    \XF::phrase('post_x', ['position' => $this->content_id]) .
                    '</a>';
                break;

            case 'profile_post':
                return '<a href="' . $router->buildLink('profile-posts', ['profile_post_id' => $this->content_id]) . '">' .
                    \XF::phrase('dbtech_credits_profile_post_x', ['position' => $this->content_id]) .
                    '</a>';
                break;

            case 'thread':
                return '<a href="' . $router->buildLink('threads', ['thread_id' => $this->content_id]) . '">' .
                    \XF::phrase('dbtech_credits_thread_x', ['position' => $this->content_id]) .
                    '</a>';
                break;

            case 'user':
                return '<a href="' . $router->buildLink('members', ['user_id' => $this->content_id]) . '">' .
                    \XF::phrase('dbtech_credits_user_x', ['position' => $this->content_id]) .
                    '</a>';
                break;

            default:
                return '';
        }
    }
Imagine if I could just preload the Content relation and get the link from $this->Content->getEntityUrl().

Don't let my dreams be memes. With a simple donation of a couple of development hours to write the interface and copy/paste the three methods into various entities, you too can improve the life of a developer TODAY. Call now at 1-800-DOIT-PLZ.
 

Chris D

XenForo developer
Staff member
This has been implemented via the new XF\Entity\LinkableInterface in XF 2.2:

 
Top