venveo / craft-characteristic

元素特性钻取

安装: 45

依赖: 0

建议者: 0

安全: 0

星星: 5

关注者: 4

分支: 0

开放问题: 35

类型:craft-plugin

1.0.0-beta.12 2023-08-21 17:53 UTC

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"。

特性链接块

一个包含特定特性、多个特性值、创建该字段及其所附加元素的链接的元素。

特性字段

一个自定义字段,允许您创建特性链接。它可以用于支持字段布局的任何元素(条目、产品、类别等)。在模板中使用时,它返回一个查询对象,用于配置源元素和字段的特性链接。