studiobosco / wn-helpers-plugin
WinterCMS 辅助插件
Requires
- php: >=8.0.2
- composer/installers: ~1.0
- ramsey/uuid: ^4.7
This package is not auto-updated.
Last update: 2024-09-19 13:11:01 UTC
README
WinterCMS 插件,提供各种辅助工具。
Twig
过滤器
inline_file
渲染文件的原始内容\ 使用方法:'path/to/file' | inline_file
style_for
从yourtheme/assets/styles.json
输出 (嵌套) 键的值\ 使用方法:'some.key' | style_for
url_without_host
从任何 URL 中移除主机和协议\ 使用方法:'http://foo.bar/path?query=1' | url_without_host
将得到'/path?query=1'
localized_file_path
将包含当前或给定区域设置的文件名后缀添加到文件名中\ 使用方法:'path/to/file.txt' | localized_file_path('en')
将得到'path/to/file_en.txt'
localized_url
将当前或给定区域设置本地化/前缀 URL\ 使用方法:'/some/url' | localized_url
将得到'/en/some/url'
locale_date
使用给定的格式和可选的区域设置或当前区域设置本地化日期\ 使用方法:'2022-01-01' | locale_date('MM/dd/yyyy', 'en')
base64_encode
输出 base64 编码的字符串\ 使用方法:'some string' | base64_encode
\base64_encode_file
输出文件的 base64 编码内容\ 使用方法:'path/to/file.png' | base64_encode_file
\ 如果路径是 URL,则必须将true
作为第一个参数传递:'/url/to/file.png' | base64_encode_file(true)
md5
输出给定输入的 md5 哈希\ 使用方法:'some-string' | md5
sha1
输出给定输入的 sha1 哈希\ 使用方法:'some-string' | sha1
url_decode
解码编码的 URLyoutube_video_id
从 YouTube 视频 URL 确定视频 ID\ 使用方法:'https://www.youtube.com/watch?v=xxx' | youtube_video_id
将得到'xxx'
vimeo_video_id
从 Vimeo 视频 URL 确定视频 ID\ 使用方法:'https://vimeo.com/123456' | vimeo_video_id
将得到'123456'
currency
使用给定的货币代码和区域设置格式化货币\ 使用方法:100 | currency('EUR', 'de')
将得到100,00 €
函数
inline_file
渲染文件的原始内容\ 使用方法:inline_file('path/to/file')
style_for
从yourtheme/assets/styles.json
输出 (嵌套) 键的值\ 使用方法:style_for('some.key')
child_pages_with_layout
返回具有特定布局的静态子页面列表\ 使用方法:child_pages_with_layout(page, 'default')
默认情况下,它只会返回可见页面。要获取所有页面的列表,请执行:child_pages_with_layout(page, 'default', false)
page_by_name
返回具有给定名称的静态页面\ 使用方法:page_by_name('somepage')
inject
将返回给定类的新的实例,带有可选参数\ 使用方法:inject('StudioBosco\Helpers\Classes\FormHelper', ...)
render_component
将渲染具有给定属性的组件。需要 cms 或静态页面对象才能工作。\ 使用方法:render_component('blogPosts', page, {postsPerPage: 10})
array_set
将设置数组中键的值\ 使用方法:array_set(some_array, 'some.key', 'some value')
array_get
将获取数组中键的值\ 使用方法:array_get(some_array, 'some.key', 'some default value')
set_deep
将设置数组或对象的嵌套值\ 使用方法:set_deep(some_array_or_object, 'some.key', 'some value')
get_deep
将获取数组或对象的嵌套值\ 使用方法:get_deep(some_array_or_object, 'some.key', 'some default value')
current_url
将返回当前 URLurl_without_host
从任何 URL 中移除主机和协议\ 使用方法:url_without_host('http://foo.bar/path?query=1')
将结果为'/path?query=1'
localized_url
将当前或给定区域设置的 URL 本地化/添加前缀\ 使用方法:localized_url('/some/url')
将结果为'/en/some/url'
query_param
将返回给定查询参数的值\ 使用方法:query_param('search')
youtube_video_id
从 YouTube 视频 URL 中确定视频 ID\ 使用方法:youtube_video_id('https://www.youtube.com/watch?v=xxx')
将结果为'xxx'
vimeo_video_id
从 Vimeo 视频 URL 中确定视频 ID\ 使用方法:vimeo_video_id('https://vimeo.com/123456')
将结果为'123456'
carbon_date
将任何内容转换为 carbon 日期\ 使用方法:carbon_date('now')
config
获取配置值,如果没有设置则返回可选默认值\ 使用方法:config('some.key', 'default value')
is_logged_in_backend
如果当前用户已登录到后端则返回 trueimage_size
返回一个包含图像文件大小的数组\ 使用方法:image_size('/absolute/path/to/100x100_image.jpg')
将结果为['width' => 100, 'height' => 100, 'aspect_ratio' => 1]
currency
使用给定的货币代码和区域设置格式化货币\ 使用方法:currency(100, 'EUR', 'de')
将结果为100,00 €
辅助函数
ArrayHelper
StudioBosco\Helpers\Classes\ArrayHelper::uniqueBy($array, $path)
返回在给定路径下找到的唯一值的数组。
CountryHelper
StudioBosco\Helpers\Classes\CountryHelper::getCountries($locale = null)
返回当前或给定区域设置的翻译后的国家列表。
CurrencyHelper
StudioBosco\Helpers\Classes\CurrencyHelper::getCurrencies()
返回货币列表。StudioBosco\Helpers\Classes\CurrencyHelper::format(float $amount, string $currencyCode, $locale = null)
根据给定区域设置格式化给定金额的货币。FormHelper
StudioBosco\Helpers\Classes\FormHelper::getFontAwesomeIconOptions()
返回用于下拉字段的 font awesome 图标列表。
LocaleHelper
StudioBosco\Helpers\Classes\LocaleHelper::getLocale()
获取当前前端区域设置StudioBosco\Helpers\Classes\LocaleHelper::hasTranslatePlugin()
如果 winter 翻译插件已启用则返回 trueStudioBosco\Helpers\Classes\LocaleHelper::hasMulitpleLocales()
如果已启用多个前端区域设置则返回 true
SocialMediaHelper
StudioBosco\Helpers\Classes\SocialMediaHelper::getYouTubeVideoId($url)
获取 YouTube 视频 URL 的视频 IDStudioBosco\Helpers\Classes\SocialMediaHelper::getVimeoVideoId($url)
获取 Vimeo 视频 URL 的视频 ID
StaticPagesHelper
StudioBosco\Helpers\Classes\StaticPagesHelper::getStaticPageOptions()
返回用于下拉字段的静态页面列表。StudioBosco\Helpers\Classes\StaticPagesHelper::getChildPagesWithLayout($page, $layout, $visibleOnly = true)
返回具有给定布局的静态子页面列表。StudioBosco\Helpers\Classes\StaticPagesHelper::getPageByName($name)
根据名称检索页面。
BlogHelper
StudioBosco\Helpers\Classes\BlogHelper::getAllBlogCategoryOptions()
返回用于下拉字段的博客分类列表。StudioBosco\Helpers\Classes\BlogHelper::getAllBlogPageOptions()
返回用于下拉字段的博客帖子列表。StudioBosco\Helpers\Classes\BlogHelper::getAllBlogTagOptions()
返回用于标签字段的标签(博客分类)列表。
UrlHelper
StudioBosco\Helpers\Classes\UrlHelper::currentUrl()
获取当前请求 URLStudioBosco\Helpers\Classes\UrlHelper::urlWithoutHost($url)
从任何 URL 中移除主机和协议StudioBosco\Helpers\Classes\UrlHelper::localizedUrl($url, $withoutHost = true)
将当前或给定区域设置添加到 URL 前缀
表单小部件
fontawesomeicon
显示可搜索的 font awesome 图标列表。
用法
some_icon_field:
type: fontawesomeicon
geolocation
显示一个可以搜索地址并在其上放置标记以获取纬度和经度值的地图。它将自动设置给定 lat 和 lng 字段的价值。
用法
my_geolocation_field:
type: geolocation
latField: lat # 'lat' is default
lngField: lng # 'lng' is default
mapHeight: 400px
revisions
显示模型表单的修订记录列表。允许通过撤销更改并将其提交到修订历史记录来撤销修订。
用法
首先,模型需要是可修订的
class MyModel extends Model
{
use \Winter\Storm\Database\Traits\Revisionable;
use \Winter\Storm\Database\Traits\Purgeable; // also add this
protected $revisionable = [
'title',
'description',
];
protected $purgeable = [
'revisions', // add revisions field to purgable or winter will try to store revisions on model save.
];
public $morphMany = [
'revision_history' => [
'System\Models\Revision',
'name' => 'revisionable',
'order' => 'created_at desc',
],
];
}
然后您需要将其作为字段添加。它将根据其类型(如果存在于模型表单中)显示字段值。但是,您可以覆盖字段。这对于具有关系的字段特别有用。
_revisions:
type: revisions
relation: revision_history # 'revision_history' is default
dateFormat: Y/m/d - H:i:s # 'Y/m/d - H:i:s' is default
fields: # override some fields
some_related_model:
nameFrom: title
valueFrom: my_id
some_special_field:
label: 'Some special field'
type: partial
path: $/path/to/partial.htm
partialwithvars
与部分表单小部件相同,但您可以向其中添加 vars
。在这些部分中,这些变量将作为 PHP 变量可用。
用法
my_partial_with_vars:
type: partialwithvars
path: _some_partial.htm
vars:
key: value
otherKey: otherValue
...
autocomplete
显示一个自动完成下拉列表。
用法
静态建议
my_autocomplete:
type: autocomplete
suggestions:
first: First
second: Second
last: Last
动态建议
my_autocomplete:
type: autocomplete
suggestions: getMyAutocompleteSuggestions # you can use a custom method
class MyModel extends Model
{
/**
* @param string $q - the search query
* @return {array} the autocomplete suggestions
*/
public function getMyAutocompleteSuggestions(string $q)
{
// return list with keys beeing the ID and value the label of the suggestion
}
}
Clipboard
剪贴板小部件允许添加一个复制和粘贴按钮,该按钮将从记录中的一个 targetField
或本地复制内容到另一个中。它只能将内容粘贴到与模型相同的类型和字段中。
some_nested_form:
label: Just some nested form
type: nestedform
clipboard_some_nested_form:
copyLabel: Copy nested form
pasteLabel: Paste nested form
type: clipboard
targetField: some_nested_form // use array-like syntax for nested fields. e.g. parentField[nestedField][evenDeeperNestedField]
Uuid
uuid 小部件允许生成 UUID。默认情况下,它是一个隐藏字段。它始终为只读。
my_uuid:
label: UUID
type: uuid
hidden: false # set to false to show the field
模型特性
RevisionableExtended
此特性为可修订模型添加了一些有用的功能,并支持修订 n-n 关系。
用法
class MyModel extends Model
{
use \StudioBosco\Helpers\Traits\RevisionableExtended; // must come befre regular revionsable trait
use \Winter\Storm\Database\Traits\Revisionable;
protected $revisionableRelations = [
'comments', // comments are now revisionable also they are 1-to-n
];
public $hasMany = [
'comments' => [ ... ],
];
}
NestedCasts
此特性会将存储在例如 JSON 字段中的嵌套属性转换为值。
用法
class MyModel extends Models
{
use \StudioBosco\Helpers\Traits\NestedCasts;
protected $casts = [
'some.nested.attribute' => 'boolean', // This attribute will be cast to the type or become null if it does not exist.
];
}
其他
日期范围过滤器作用域的默认值
现在您可以为日期范围列表过滤器使用默认值
scopes:
my_daterange:
type: daterange
default:
- 'now'
- 'now + 1 year'