XF 2.2 Understanding handlers

Gobb

Member
Hi,

Just hoping to get some clarification as to using handlers. The following is seen in the alert abstract handler:
PHP:
public function getTemplateName($action)
    {
        return 'public:alert_' . $this->contentType . '_' . $action;
    }

    public function getTemplateData($action, UserAlert $alert, Entity $content = null)
    {
        if (!$content)
        {
            $content = $alert->Content;
        }

        return [
            'alert' => $alert,
            'user' => $alert->User,
            'extra' => $alert->extra_data,
            'content' => $content
        ];
    }

    public function render(UserAlert $alert, Entity $content = null)
    {
        if (!$content)
        {
            $content = $alert->Content;
            if (!$content)
            {
                return '';
            }
        }

        $action = $alert->action;
        $template = $this->getTemplateName($action);
        $data = $this->getTemplateData($action, $alert, $content);

        return \XF::app()->templater()->renderTemplate($template, $data);
    }

What I want to understand is, the $alert variable and $content variable and how this is passed in. Am I correct to assume the UserAlert $alert is the UserAlert entity and the data that is currently being passed in, and the Entity $content is the entity of content that is being fetched? So if an alert is alerting a thread, the Entity $content would be the XF:Thread entity and relevant contents?

Only asking because my own handler only works and delivers the template when removing the equivalent "UserAlert", but if it's just Entity it works fine.

Any assistance would be very much appreciated,
Thanks
 

Kirby

Well-known member
$alert is a UserAlert entity.

What exactly does
my own handler only works and delivers the template when removing the equivalent "UserAlert"
mean - do you get any erors?
Or does it not display anything at all?
Can you post the full code of your handler class?
 

Gobb

Member
$alert is a UserAlert entity.

What exactly does

mean - do you get any erors?
Or does it not display anything at all?
Can you post the full code of your handler class?
All good, It seems to be working now after your clarification and my re-typing of it. I think I was just using the incorrect name for the entity. I have an Entity called KenoLog which my intent is to fetch the logs and render the appropriate template depending on the content_type and action entered into the database for the logs, similar to alerts/reactions gobb_keno_post_insert as an example. This is instead of a bunch of if statements within one template.

Thanks for the clarification, pretty sure it helped me solve the issue. :)
 
Top