verbb / vizy

A flexible visual editor for Craft CMS
Other
43 stars 8 forks source link

Fixing SuperTableBlockQuery when needing |length from a super table field within a Vizy Block Type #227

Closed jan-dh closed 1 year ago

jan-dh commented 1 year ago

Question

I have a Vizy block type called table which has multiple richtext fields: col_1, col_2, col_3, ...

I would like to know how many of the first row actually have content in them. Using a normal Craft table approach, something like this works:

{%-set test = (table[0]|filter(i => i|length > 0)|length) -%}

I was trying to get something similar to work with super table, since I need a bit more styling options in my table, but I'm struggling to work around the SuperTableBlockQuery "issue". When I try to access the table from my Block type template, I try:

{% set rows = table.all() %}

Using the .all(). or .one() method both give me back SuperTableBlockQuery instead of an actual array or collection I can get the length for with |length. I keep getting the SuperTableBlockQuery.

Additional context

related to https://github.com/verbb/super-table/issues/467

engram-design commented 1 year ago

I'll need a bit more information about your Vizy block setup. The block type is called table, but do you have a Super Table field inside that? Is that also named table? Is the Super Table field the thing that contains your multiple rich text fields?

I will assume this is in the template partial for the Vizy block, and the Super Table field handle is table. You're saying that both these provide identical output (they are both SuperTableBlockQuery)?

{% dd table %}

{# ... and #}
{% dd table.all() %}
jan-dh commented 1 year ago

The Super table field has a handle: table. The block within the Vizy field is called table with the handle table.

The first one outputs the SuperTableBlockQuery, the second one outputs an array of 2 (which is indeed the amount of rows within the table in the CMS).

When I try to get the fields of this first row however, doing: table.all()[0]|length, I get back 27, while it is a table with 5 fields.

jan-dh commented 1 year ago

I would think I would be able to do sth like:

{%-set test = (table.all()[0]|filter(i => i|length > 0)|length) -%}

engram-design commented 1 year ago

Calling table.all()[0]|length would be correct. Try {% dd table.all()[0] %} and you'll see it's a SuperTableBlockElement which probably has 27 public properties, hence the "size" of it. If you just want to get the first row of the Super Table field, you can use table.all()[0] or table.all() | first.

But, more importantly, you can't just call length on a SuperTableBlockElement which is referenced in your issue on the Super Table GitHub issues. Rows on a Table field are plain arrays, not objects like Super Table, so you'll get different results.

There's also not really an "isEmpty()" value for a Super Table block either. You would need to loop through each field in the block to check its isEmpty() method on the field.

I think this is probably more of a general Super Table issue than a Vizy one. You'll also have the same issue with Matrix I believe.

jan-dh commented 1 year ago

Yep, agree, will close it here & check in Super table repo if needed, thx for the input!