我正在为重力表单创建一个自定义字段插件。我正在寻找一种方法来更改自定义字段的表单编辑器中显示的图标。我浏览了文档,剖析了其他插件,并疯狂地用谷歌搜索,目前我还没有找到改变它的方法。可能没有简单或内置的方法来覆盖图标,但我想我应该问开发人员 hivemind。
理想情况下,我只会在存在覆盖功能或设置时更改它。我不想对 JS 选择器和 css 感到疯狂(除非很简单)。
有什么想法吗?谢谢!
希望您能够在您的项目中解决这个问题,但对于那些来寻找答案的人来说。来自 Gravity Form 自己的
GF_Field
类:
/**
* Returns the field's form editor icon.
*
* This could be an icon url or a gform-icon class.
*
* @since 2.5
*
* @return string
*/
public function get_form_editor_field_icon() {
return 'gform-icon--cog';
}
要扩展此功能以进行您自己的定制,您有 3 个返回值选项:
get_form_editor_field_icon
返回font Awesome/dashicons类,超级简单,几乎没有任何不便。 (Gravity Forms 版本 2.6.1 引入了 icon_namespace
,能够通过添加 GFAddOn::get_icon_namespace() 来指定和使用自定义字体图标。get_form_editor_field_icon
返回 SVG(内容),它将在适当的位置渲染 SVG。您可能需要做一些额外的样式来正确设置 SVG 大小,否则它会呈现“添加字段”按钮的全宽。get_form_editor_field_icon
返回您自己的自定义图标的 图像路径。使用示例:
class Super_Awesome_Field_Type extends GF_Field {
public $type = 'your_custom_field_type';
/**
* Returns the field's form editor icon.
*
* This could be an icon url or a gform-icon class.
*
* @since 2.5
*
* @return string
*/
public function get_form_editor_field_icon() {
// set your own custom value here
return 'gform-icon--flag';
}
/**
* Returns the field button properties for the form editor. The array contains two elements:
* 'group' => 'standard_fields' // or 'advanced_fields', 'post_fields', 'pricing_fields'
* 'text' => 'Button text'
*
* Built-in fields don't need to implement this because the buttons are added in sequence in GFFormDetail
*
* @return array
*/
public function get_form_editor_button() {
return array(
'group' => 'advanced_fields',
'text' => $this->get_form_editor_field_title(),
// you could also manually just override in this method as well
'icon' => $this->get_form_editor_field_icon(),
'description' => $this->get_form_editor_field_description()
);
}
}
对于那些想要深入研究代码的人,可以在 common.php 的
get_icon_markup
中使用它来渲染。