You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jun 24, 2020. It is now read-only.
There is a nice tutorial piece inside what @oligriffiths wrote that should go into the guides.
Storing here for now:
Building filters
A filter must implement at least 1 of 2 interfaces. These interfaces are: KTemplateFilterRead and KTemplateFilterWrite,
and correspond to enabling the filter for read and/or write mode respectively.
So, building on the example above, suppose we wanted to create a filter that would automatically create a hyperlink whenever
anybody referenced another user in their comment (twitter style).
Let's define how the filter will work:
Parse all mentions using the @username syntax
Usernames must be at least 3 characters long
Usernames can only contain letters in lowercase and uppercase, and numbers
When finding a username match from the users table, replace with a link to the users' profile
The class must extend the abstract filter class and implement the "write" interface as we're operating on the data in the evaluated template.
Implement the write(&$text) method, this is a requirement of the write interface. $text contains the evaluated template as a string. Notice that $text$ is referenced, we'll get to that in a moment.
Run a regular expression match on the evaluated template to find all @mentions
Loop through each match and attempt to find a corresponding user. Note: the getUser() function is dummy and for explanatory purposes only, in the real world, this would do some kind of DB lookup.
If a user is found, replace the @mention text with a hyperlink to the users profile. Note: $user->profile_url is for explanatory purposes only.
As $text is referenced, meaning any changes we make to it will affect the original variable passed to this function, and any other filters for that matter.
And that's it, pop the above code in /components/com_comments/templates/filters/mention.php and you'd be good to go.
Filters advanced
Filters are stacked in a chain in the order they're added to the template. Filters also have a priority that allows you to set where in the chain the filter will be placed. By default there are 5 priorities:
KCommand::PRIORITY_LOWEST
KCommand::PRIORITY_LOW
KCommand::PRIORITY_NORMAL
KCommand::PRIORITY_HIGH
KCommand::PRIORITY_HIGHEST
The number corresponds to priority value. Priorities run from lowest to highest, highest being run first, lowest being run last. The priority can be set in the _initialize() method:
If you need to get a reference to the template object that the filter belongs to, you can call $this->getTemplate(). This may be useful if you need to gain access to other functions in the template, the original data passed to the template, or even the view, for example to generate a url $this->getTemplate()->getView()->createRoute($url) as would be the correct way from the example above, instead of $user->profile_url. See KTemplateFilterAbstract and KTemplateAbstract for a list of all public methods.
The text was updated successfully, but these errors were encountered:
There is a nice tutorial piece inside what @oligriffiths wrote that should go into the guides.
Storing here for now:
Building filters
A filter must implement at least 1 of 2 interfaces. These interfaces are:
KTemplateFilterRead
andKTemplateFilterWrite
,and correspond to enabling the filter for read and/or write mode respectively.
So, building on the example above, suppose we wanted to create a filter that would automatically create a hyperlink whenever
anybody referenced another user in their comment (twitter style).
Let's define how the filter will work:
This is a fairly straight forward process:
Simple as that!
Ok, to explain the above a bit:
write(&$text)
method, this is a requirement of the write interface.$text
contains the evaluated template as a string. Notice that$text$
is referenced, we'll get to that in a moment.@mentions
getUser()
function is dummy and for explanatory purposes only, in the real world, this would do some kind of DB lookup.@mention
text with a hyperlink to the users profile. Note:$user->profile_url
is for explanatory purposes only.$text
is referenced, meaning any changes we make to it will affect the original variable passed to this function, and any other filters for that matter.And that's it, pop the above code in
/components/com_comments/templates/filters/mention.php
and you'd be good to go.Filters advanced
Filters are stacked in a chain in the order they're added to the template. Filters also have a priority that allows you to set where in the chain the filter will be placed. By default there are 5 priorities:
The number corresponds to priority value. Priorities run from lowest to highest, highest being run first, lowest being run last. The priority can be set in the
_initialize()
method:If you need to get a reference to the template object that the filter belongs to, you can call
$this->getTemplate()
. This may be useful if you need to gain access to other functions in the template, the original data passed to the template, or even the view, for example to generate a url$this->getTemplate()->getView()->createRoute($url)
as would be the correct way from the example above, instead of$user->profile_url
. SeeKTemplateFilterAbstract
andKTemplateAbstract
for a list of all public methods.The text was updated successfully, but these errors were encountered: