Của bạn đây:
This guide will show you how to use the PHP callbacks options with the
Custom Fields by Waindigo add-on.
Using the PHP callback field type
Using the PHP callback field type you can create almost any type of field you want.
There are a number of custom fields available as resources which use the PHP callback field type:
Star Rating For Custom Fields 1.1.x by Waindigo
Date Field For Custom Fields by Waindigo
Step 1
Firstly, download and install the latest version of the
Custom Fields by Waindigo add-on.
If you are creating a post field, you will also need to install the
Custom Post Fields by Waindigo add-on.
If you are creating a social forum field, you will also need to install the
Social Groups by Waindigo add-on.
If you are creating a resource field, you will also need to buy and install the XenForo Resource Manager add-on.
Instructions for installing the add-ons can be found on the resource pages.
Step 2
Enable debug mode by adding the following line of code to the bottom of your library/config.php file.
Step 3 (Thread fields)
To create a custom thread field, from the Admin Control Panel, go to the Applications tab and click Custom Thread Fields.
View attachment 6550
Step 3 (Social forum fields)
To create a custom social forum field, from the Admin Control Panel, go to the Applications tab and click Custom Social Forum Fields.
View attachment 6551
Step 3 (Resource fields)
To create a custom resource field, from the Admin Control Panel, go to the Applications tab and click Resource Fields/Custom Resource Fields (depending on version of Resource Manager installed).
View attachment 6552
Step 3 (Post fields)
To create a custom post field, from the Admin Control Panel, go to the Applications tab and click Custom Post Fields.
View attachment 6553
Step 4
Click the '+ Create New Field' button.
View attachment 6554
Step 5
Browse to the /library folder of your XenForo installation. If this is the first time you have created any PHP callbacks or an add-on, create a new folder to store all your callbacks and add-ons. For this example, we will call the folder Waindigo. The folder name should contain only upper and lowercase characters, and it is recommended that the first letter is a capital letter.
Now create another folder inside the one you just created to identify this callback. For this example, we will call the folder CustomUsernameField. Again, the folder name should contain only upper and lowercase characters, and it is recommended that the first letter is a capital letter.
Step 6
Create an empty .php file inside this folder. For this example, we will call the file CustomField.php, but you can call it anything you like, provided it only contains upper and lowercase characters as before, and it is recommended that the first letter is a capital letter again.
So, we have a new file with a name that is something like:
/library/Waindigo/CustomUsernameField/CustomField.php
If we strip off the /library/ and .php bits and replace the forward slashes with underscores, we get our
class name.
For this example, our class name is, therefore:
Waindigo_CustomUsernameField_CustomField
So that XenForo recognises our file, we write our class name in the file as follows:
Mã:
<?php
class Waindigo_CustomUsernameField_CustomField
{
}
Step 7
We now need to add our method. The PHP callback field type has the following callback signature:
Mã:
XenForo_Template_Abstract $template, array $field
We place our method inside of the two braces in our PHP file. Our PHP file will therefore look as follows:
Mã:
<?php
class Waindigo_CustomUsernameField_CustomField
{
public static function render(XenForo_Template_Abstract $template, array $field)
{
}
}
Step 8
Our render function will contain a link to a XenForo template. We will also be able to make available to the template any additional parameters that we want to set.
For our example, we will just provide the template with a parameter called {$field}, which comes from our callback signature and contains all the information about the custom field (such as the ID, title, description, etc.).
The code to create an array of our parameters in our example is as follows:
Mã:
$params = array(
'field' => $field
);
In the next step, we will create a template called waindigo_username_field_customusernamefield. You will note that we have prepended the template name with the name of our general add-ons folder and appended the template name with the name of our callback folder. Also, template names should only contain lowercase characters and underscores.
The code to link back to the XenForo template is as follows:
Mã:
return $template->create('waindigo_username_field_customusernamefield', $params);
So our PHP file should look like this:
Mã:
<?php
class Waindigo_CustomUsernameField_CustomField
{
public static function render(XenForo_Template_Abstract $template, array $field)
{
$params = array(
'field' => $field
);
return $template->create('waindigo_username_field_customusernamefield', $params);
}
}
Step 9
Enter the class and method name of the PHP callback into the custom field edit screen and click 'Save Field'.
View attachment 6555
Step 10
To create a new Template, go to the Appearance tab, click Templates and click '+ Create New Template'.
View attachment 6556
Step 11
Name the template waindigo_username_field_customusernamefield and enter the following HTML:
Mã:
<dl class="ctrlUnit">
<dt>
<label for="ctrl_{$field.validator_name}">{$field.title}:</label>
<xen:if is="{$field.required}"><dfn>{xen:phrase required}</dfn></xen:if>
</dt>
<dd>
<input type="search" name="{$field.name}" placeholder="{xen:phrase user_name}..." results="0" class="textCtrl AutoComplete AcSingle" id="ctrl_{$field.validator_name}" />
<xen:if is="{$field.description}"><p class="explain">{$field.description}</p></xen:if>
</dd>
</dl>
Step 12
To create a new Admin Template, go to the Development tab, click Admin Templates and click '+ Create Admin Template'.
View attachment 6557
Step 13
Name the template waindigo_username_field_customusernamefield and enter the following HTML:
Mã:
<xen:textbox name="criteria[username]" />
<xen:textbox name="criteria[username]" />
<xen:textboxunit
inputclass="quickSearchText AutoComplete AcSingle" placeholder="{xen:phrase user_name}..." type="search" results="0" label="{$field.title}:" explain="{xen:raw $field.description}"
name="{$field.validator_name}" id="ctrl_{$field.validator_name}"
value="{$field.field_value}" data-validatorname="{$field.validator_name}" />
Done!