venveo / craft-characteristic
元素特性钻取
Requires
- craftcms/cms: ^3.4.0
- dev-master
- 1.0.0-beta.12
- 1.0.0-beta.11
- 1.0.0-beta.10
- 1.0.0-beta.9
- 1.0.0-beta.8
- 1.0.0-beta.7
- 1.0.0-beta.6
- 1.0.0-beta.5
- 1.0.0-beta.4
- 1.0.0-beta.3
- 1.0.0-beta.2
- 1.0.0-beta.1
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/json5-and-vue/cli-service-2.2.3
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/express-4.18.2
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/qs-6.5.3
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/decode-uri-component-0.2.2
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/loader-utils-and-vue/cli-service-1.4.2
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/yargs-parser-5.0.1
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/terser-4.8.1
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/thenify-3.3.1
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/shell-quote-1.7.3
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/eventsource-1.1.1
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/async-2.6.4
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/url-parse-1.5.10
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/follow-redirects-1.14.8
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/node-sass-7.0.0
- dev-refactor/vue-3
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/path-parse-1.0.7
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/ws-6.2.2
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/dns-packet-1.3.4
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/hosted-git-info-2.8.9
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/lodash-4.17.21
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/y18n-3.2.2
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/elliptic-6.5.4
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/highlight.js-9.18.5
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/http-proxy-1.18.1
- dev-dependabot/npm_and_yarn/src/assetbundles/characteristicsfield/hapi/hoek-8.5.1
- dev-change/refactor
- dev-feature/audit-table
This package is auto-updated.
Last update: 2024-09-21 20:07:52 UTC
README
特性提供了一种新的存储和查询元素复杂描述关系和属性的方式。
您可以将它视为一个“属性值”系统,其中属性可能应用于元素(例如“标签”),而特定的值可以应用于该属性(例如,具有标签或分类和文本字段的超级表字段)
例如,考虑您有一份当地餐厅的列表,并且您想构建一个帮助游客根据他们的需求选择餐厅的系统。您可以 创建 一系列标签或分类,例如
- "有户外座位"
- "提供披萨"
- "仅限现金支付"
然而,这些数据完全不考虑上下文,实际上只是一个想法的云。进一步地,如果我们想引入价格点,如“$”,“$$”,“$$$”,“$$$$”,“$$$$$”
我们现在是否创建一个新的类别组,还是只是将其与其他组一起放入?如果我们创建一个新的组,我们必须现在编写更多的代码。
特性通过创建两个新的元素类型并按组排序来解决这个问题。可以像标签一样应用的“特性”元素,一旦应用,就可以应用“特性值”。这个值是自由形式的,并且当创建新的不同值时将创建新的元素 按特性。
例如,作为开发者,我会创建一个名为“Restaurant Finder Attributes”的属性组,在其中,内容编辑可以像其他元素一样定义它们的特性
- "接受现金"
- "价格水平"
- "户外座位"
- "哪道菜看起来最美味?"
- "我什么时候会被禁止喝酒?"
通过使用自定义字段,我现在可以按需应用这些特性及其值。注意上面的最后一个选项:它要求我选择一张我喜欢的照片。这是因为特性和特性值可以为每个组提供自己的字段布局!
特性提供了一个名为“Drilldown”的辅助变量
Drilldown 工具允许您提供元素查询,例如:craft.entries.section('restaurants')
并访问最相关的特性和其选项。该助手还根据所选选项管理一个“状态”,允许您快速创建一个“测验”来找到最合适的元素。
要求
此插件需要 Craft CMS 3.4.0 或更高版本。
使用特性
Drilldown 辅助工具
{# Any arbitrary base element query #} {% set query = craft.entries.section('restaurants') %} {# Create an instance of the drilldown helper for the characteristic group with the handle `restaurantCharacteristics` #} {% set drilldown = craft.characteristic.drilldown('restaurantCharacteristics', query) %} {% set state = drilldown.state %} {% set current = drilldown.currentCharacteristic %} {# Get a text field called characteristicDescription off of the characteristic #} <h2>{{ current.characteristicDescription }}</h2> {# Get all of the options available for the current characteristic #} {% set options = drilldown.currentOptions.all() %} <ul> {% for option in options %} {# Use the applyToDrilldownState method to create a URL for this value based on the current state #} <li><a href="{{ option.applyToDrilldownState(state).url }}">{{ option.value }}</a></li> {# Grab a featuredImage Asset field off of the option #} {% if option.featuredImage.exists() %} <img src="{{ option.featuredImage.one().url }}" /> {% endif %} {% endfor %} <hr> {# Optional URL to skip the question with picking an answer #} <a href="{{ drilldown.skipUrl() }}">Skip Question</a> </ul> <hr> <div><strong>Current Result Set</strong></div> {% if drilldown.results.count() == 1 %} <h1>You did it!</h1> {% endif %} {% for item in drilldown.results.all() %} <div>{{ item.title }}</div> {% endfor %}
特性字段
该字段返回一个为元素预先配置的 CharacteristicLinkBlockQuery。
获取条目上的所有特性并以表格形式显示
<table class="table-auto"> <thead> <tr> <th class="px-4 py-2">Characteristic</th> <th class="px-4 py-2">Value</th> </tr> </thead> <tbody> {% set blocks = entry.restaurantAttributes.all() %} {% for block in blocks %} <tr> <td class="border px-4 py-2">{{ block.characteristic.title }}</td> {# We're going to create a string out of the characteristic value's text value #} <td class="border px-4 py-2">{{ block.values.all()|column('value')|join(', ') }}</td> </tr> {% endfor %} </tbody> </table>
查询元素
有多种方法可以查询具有特定特性的元素。
您可以使用本机 Craft 关系,例如
{% set characteristic = craft.characteristic.characteristics.handle('price').one() %} {% set value = characteristic.values.value('$').one() %} {# Get the first restaurant with a price "$" #} {% set restaurants = craft.entries.section('restaurants').relatedTo(['and', {targetElement: characteristic.id}, {targetElement: value.id}]) %} {{ restaurants.one().title }}
术语和概念
特性组
包含一组特性、其值和链接。允许您维护不相关的特性的分离。例如:“产品特性”,“餐厅特性”
特性
表示分配给其他元素的描述性属性的元素。例如:“材料”,“流量率”,“周日开门”。特性可能具有自定义字段。
特性值
表示特性潜在值的元素。每个特性值相对于特定的特性。特性值具有一个value
属性,这是一个文本字符串,对每个特性是唯一的。例如:"是"、"否"、"1.25"。
特性链接块
一个包含特定特性、多个特性值、创建该字段及其所附加元素的链接的元素。
特性字段
一个自定义字段,允许您创建特性链接。它可以用于支持字段布局的任何元素(条目、产品、类别等)。在模板中使用时,它返回一个查询对象,用于配置源元素和字段的特性链接。