ianreid/switcher
Composer 安装命令:
composer require ianreid/switcher
包简介
A flexible and easy-to-use sites switcher for Craft CMS.
README 文档
README
Switcher for Craft CMS
Requirements
This plugin requires Craft CMS ^4.x or ^5.x
Installation
To install the plugin, follow these instructions.
-
In your terminal, go to your Craft project:
cd /path/to/project -
Then tell Composer to load the plugin:
composer require ianreid/switcher -w && php craft plugin/install switcher
Overview
The Switcher plugin for Craft CMS provides a Twig function for generating a site switcher on your webpage. This switcher can be configured to display sites from the current group or all groups.
Twig function
The getSwitcherSites() Twig function comes with several parameters.
getSwitcherSites()
| Parameters | Type | Default | Description |
|---|---|---|---|
| source | Element or Array | null | Element or array from which the sites are extracted. |
| removeCurrent | Bool | true | Determines whether the current site should be excluded from the output. |
| onlyCurrentGroup | Bool | true | Specifies whether only sites from the current group should be shown. |
| redirectHomeIfMissing | Bool | false | Decides whether to redirect to the home page if a site is missing. |
Recommended usage
We suggest setting the source on a per-template basis. You can establish a fallback to entry, which allows you to set the variable for elements like categories, products, or an array of custom routes.
1. Define an availableSites variable in your main layout file
{% set availableSites = getSwitcherSites(switcherCustomSource|default(entry ?? null)) %}
In this variable, we use the switcherCustomSource variable as the source, with entry as the fallback, and then null.
You can then pass this variable to your navbar or any file that includes a site/languages switcher to prevent a redundant query.
By defining an availableSites variable at the top of your main layout file, you can also use it for the og:locale:alternate meta in the head, the <link rel=alternate>, etc.
2. Determine your source in your page template
For any craft\base\Element other than Entry (the default in the previous example), you can do the following in your page template :
{% set switcherCustomSource = product %}
or
{% set switcherCustomSource = category %}
or for an array (ex: with custom routes)
{% set switcherCustomSource = [
{'uri':'cart', 'siteId': 1},
{'uri':'panier', 'siteId': 2},
{'uri':'cesta', 'siteId': 3},
]
%}
Output
The function returns an array of items with two keys: "url" and "site". The "url" key is the site's URL, and the "site" key is the site model.
array [
0 => array [
"url" => "http://yoursite.com/uri"
"site" => craft\models\Site {}
]
1 => array [
"url" => "http://yoursite.com/en/uri"
"site" => craft\models\Site {}
]
]
Site Switcher Examples
Basic usage
{% if availableSites|length %}
{% for item in availableSites %}
<a
href="{{ url(item.url) }}"
hreflang="{{item.site.language}}"
lang="{{item.site.language}}"
>
{{ item.site.language }}
</a>
{% endfor %}
{% endif %}
Grouping Sites by Groups
{% if availableSites|length %}
{% set availableSitesByGroup = availableSites|group(lang => lang.site.group) %}
{% for group, langs in availableSitesByGroup %}
<h3 class="text-9 text-blue-500">{{ group }}</h3>
{% for item in langs %}
<a
href="{{ url(item.url) }}"
hreflang="{{item.site.language}}"
lang="{{item.site.language}}"
>
{{ item.site.language }}
</a>
{% endfor %}
{% endfor %}
{% endif %}
Displaying Only the First Two Letters of the Language
{% if availableSites|length %}
{% for item in availableSites %}
<a
href="{{ url(item.url) }}"
hreflang="{{item.site.language}}"
lang="{{item.site.language}}"
>
{{ item.site.language [0:2]|capitalize }}
</a>
{% endfor %}
{% endif %}
Using for og:locale:alternate in <head>
Use your previously created availableSites variable if you wish to apply the same parameters, or create a different variable if not.
💡 You must define your variable PRIOR to the following meta property.
<meta property="og:locale" content="{{ currentSite.language }}">
{% if availableSites|length %}
{% for item in availableSites %}
<meta property="og:locale:alternate" content="{{ item.site.language }}">
{% endfor %}
{% endif %}
💡 All these examples can be adjusted based on the function parameters.
This plugin is brought to you by Ian Reid Langevin
ianreid/switcher 适用场景与选型建议
ianreid/switcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 598 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cms」 「locale」 「site」 「language」 「localization」 「language switcher」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ianreid/switcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ianreid/switcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ianreid/switcher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
Package for convenient work with Laravel's localization features
404 'not found' and 403 'forbidden' error handlers. The 404 handler shows custom content for missing pages but not resources like CSS or JS. The 403 handler redirects to a login URL on unauthorized access.
GraphQL authentication for your headless Craft CMS applications.
Set Links with a specific language parameter
Supercharged text field validation.
统计信息
- 总下载量: 598
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-15