定制 elementworks/craft-preparse-field 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

elementworks/craft-preparse-field

Composer 安装命令:

composer require elementworks/craft-preparse-field

包简介

A fieldtype that parses Twig when an element is saved and saves the result as plain text.

README 文档

README

A fieldtype that parses Twig when an element is saved and saves the result as plain text.

Requirements

This plugin requires Craft CMS 4.0.0 or later and PHP 8.0.2 or later.

Installation

To install the plugin, follow these instructions.

  1. Open your terminal and go to your Craft project:

     cd /path/to/project
    
  2. Then tell Composer to load the plugin:

     composer require besteadfast/craft-preparse-field
    
  3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Preparse Field.

Usage

When creating a new Preparse field, you add the Twig that you want run to the field's settings. When an element with that Preparse field is saved, the code will be parsed and the resulting value saved as plain text.

It's worth noting that the Preparse field is only updated when the element the field is on is saved. If you grab data from a related element (like in the category title example below), and then update the related element, the preparsed value will not automatically be updated.

In the Twig, the element that the Preparse field is added to is available as a variable named element. It's best to use this variable (as opposed to something like entry or asset) because it's possible you add the same Preparse field to multiple element types. This also means that when a Preparse field is added to a Matrix, SuperTable, or Neo block, that block will be what is available as element, so if you want to access the element that the Matrix/SuperTable/Neo field belongs to, you will want to use element.owner.

Examples

If you have a category field on your element named relatedCategory, you can save the category title to the Preparse field by adding the following Twig to the field settings:

{{ element.relatedCategory.one().title ?? '' }}

This is useful for saving preparsed values to a field for use with sorting, searching, or similar things.

You can also do more advanced stuff, for instance performance optimizing. Let's say you have three different asset fields that may or may not be populated. Having to check these in the template may require a bunch of queries since you can't check if a field has a relation in Craft without actually querying for it. You could do something like this to get the id of the asset to use:

{% if element.smallListImage | length %}
    {{ element.smallListImage.one().id }}
{% elseif element.largeListImage | length %}
    {{ element.largeListImage.one().id }}
{% elseif element.mainImage | length %}
    {{ element.mainImage.one().id }}
{% endif %}

You'd probably want to wrap that in {% apply spaceless %} ... {% endapply %} to make it more useful...

Or you could just use it to do some bulk work when saving, like pre-generating a bunch of image transforms with Imager X:

{% if element.mainImage | length %}
    {% set transformedImages = craft.imager.transformImage(element.mainImage.one(), [
    { width: 1000 },
        { width: 900 },
        { width: 800 },
        { width: 700 },
        { width: 600 },
        { width: 500 },
        { width: 400 },
        { width: 300 },
        { width: 200 },
        { width: 100 }
    ]) %}
{% endif %}

Preparse also has access to your site's template root, so you can even include local templates if you want to do more advanced stuff and/or want to keep your field's Twig in version control:

{% include '_partials/customPreparseFieldStuff' %}

Make sure that you always write solid Twig, taking into account that fields may not be populated yet. If an error occurs in your Twig, the element will not be saved. Code defensively!

Price, License, and Support

The plugin is released under the MIT license, meaning you can do whatever you want with it as long as you don't blame us. It's free, which means there is absolutely no support included, but you might get it anyway. Just post an issue here on GitHub if you have one, and we'll see what we can do. :)

Changelog

See the changelog file.

elementworks/craft-preparse-field 适用场景与选型建议

elementworks/craft-preparse-field 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 194 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 11 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「twig」 「cms」 「fieldtype」 「Craft」 「craftcms」 「craft-plugin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 elementworks/craft-preparse-field 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 elementworks/craft-preparse-field 我们能提供哪些服务?
定制开发 / 二次开发

基于 elementworks/craft-preparse-field 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 194
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 23
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-19