Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ls206 created an issue. See original summary.

sawtell’s picture

sawtell’s picture

FileSize
3.47 KB

There are some bugs in my original patch. Also I've been told that the table caption element does exist!

The properties aren't described here: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21...

But are available in the template: https://api.drupal.org/api/drupal/core%21modules%21system%21templates%21...

I'm still trying to work out how to read the D8 documentation..

ToxaViking’s picture

Here is my vision

ToxaViking’s picture

Status: Active » Needs review
mertindervish’s picture

I have fixed some bugs of #4 patch.

Dozz’s picture

Added an update hook to the patch in #6.

Status: Needs review » Needs work

The last submitted patch, 7: tablefield-add-a-caption-field.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Dozz’s picture

Status: Needs work » Needs review
FileSize
8.87 KB

coding standards fixes.

Status: Needs review » Needs work

The last submitted patch, 9: tablefield-add-a-caption-field.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

lapek’s picture

I took a stab at this

I also:

  • removed the installation options that were referencing the D7 version
  • fixed an issue when using multiple tables, duplicate IDs would be created (a11y issue)
lapek’s picture

lapek’s picture

lapek’s picture

Status: Needs work » Needs review

  • lolandese committed 07aecd9 on 8.x-2.x authored by lapek
    Issue #2875233 by lapek, ls206, Dozz, ToxaViking, mertindervish: Add a...
lolandese’s picture

Status: Needs review » Fixed

Thanks to all for your contributions.

johnny5th’s picture

Is anyone getting this error with the patch?

I've got a node with a paragraph using tablefield. It works fine on existing entities, but new entities throws this error
Notice: Undefined index: caption in Drupal\tablefield\Plugin\Field\FieldType\TablefieldItem->setValue() (line 180 of modules/contrib/tablefield/src/Plugin/Field/FieldType/TablefieldItem.php).

lolandese’s picture

Status: Fixed » Needs work

Seems like line 180 has to be wrapped in a conditional to catch the use case of a tablefield used inside a paragraph. Take a look at the patch in #2902735: Caption dropped when editing with Paragraphs for D7 to get inspiration. A patch for D8 would be welcome.

cgomezg’s picture

Hi all,

Can we set in config this field optional? not checked by default?

We are having issues because this field is now displaying.

Thanks,

lolandese’s picture

Agreed. Should be part of the field's display options.

cgomezg’s picture

Table Caption isn't saving DEFAULT VALUE.

I should create a issue?

lolandese’s picture

Feel free to create new issues indicating this one as related or parent issue. Whatever feels more appropriate.

Try to describe the issue as accurate as possible, preferably with steps to reproduce.

cgomezg’s picture

Issue created and patch added for Caption default value
https://www.drupal.org/project/tablefield/issues/3021303

johnny5th’s picture

New patch based on dev that:

  1. Applies a simple if statement to clear the undefined index notice
  2. Gives a field display option to toggle the caption field's visibility
lolandese’s picture

Status: Needs work » Needs review

There is a patch to review. Meanwhile, thanks for the contribution.

lolandese’s picture

Status: Needs review » Needs work

Note the patch of #24 toggles the field form display which is okay as an empty value results in the caption not being shown. However, on a field with some captions already present it does not impact the normal display thus still seeing captions without the possibility to remove them through the edit form once they are disabled there with the checkbox.

Make it consistent by making sure the captions do not show once disabled on form display or, maybe even better, make the checkbox also available under "Manage display". We can imagine a use case where a caption is given as a default value but one does not want users to change it through the node edit. Or what for example if we want a caption in "Full content" but not on "Teaser" View mode?

lolandese’s picture

If you pull the latest dev version, you'll see field display option added as part of #3019613: Move display related settings from manage fields to manage display and add options. You can easily add the checkbox for the caption there as well.

lolandese’s picture

An additional problem found with the current implementation of the caption is that its value is stored twice in the database table for a created field. Inside e.g. node__field_mytablefield once in field_mytablefield_caption (varchar(255)) and once inside the serialized field_mytablefield_value (longblob). The longblob will look like:
a:3:{s:7:"caption";s:17:"Some caption text";i:0;a:2:{i:0;s:12:"row/column 0";i:1;s:8:"column 1";}i:1;a:2:{i:0;s:5:"row 1";i:1;s:3:"1-1";}}

The caption in the value should be avoided because it could result in the default value of the configuration like:

default_value:
  -
    rebuild:
      cols: '2'
      rows: '2'
    value:
      0:
        - 'row/column 0'
        - 'column 1'
      1:
        - 'row 1'
        - ''
      caption: 'Some caption text'
    format: null
    caption: 'Some caption text'

The first caption above seems to make having a valid schema impossible:

field.value.tablefield:
  type: mapping
  label: 'Tablefield value'
  mapping:
    rebuild:
      type: mapping
      label: 'Change number of rows/columns.'
      mapping:
        cols:
          type: string
          label: 'How many Columns'
        rows:
          type: string
          label: 'How many Rows'
    value:
      type: sequence
      sequence:
        type: sequence
        sequence:
          type: string
    format:
      type: string
      label: 'Input type'
    caption:
      type: string
    settings.export:
      type: integer

The above schema will always result in:

Configuration validation => Array
(
    [field.field.node.article.field_table1:default_value.0.value.caption] => variable type is string but applied schema class is Drupal\Core\Config\Schema\Sequence
)

A configuration schema, that is currently missing on the module, is proposed in the patch at https://www.drupal.org/project/tablefield/issues/2757309#comment-12911693.

lolandese’s picture

lolandese’s picture

It might be worth to see how this was done in the Drupal 7 version at #1251738: Allow ability to set table caption. That was implemented after the D8 port was created (like many other "new" D7 TableField features).

Also #2301003: tfoot & caption tag support is related and interesting to take in consideration here.

lolandese’s picture

Here's how it's stored on Drupal TableField 7.x-3.x as delta 0 (first element of a multivalue field) in table field_data_field_mytablefield:

Array
(
    [caption] => 
    [rebuild] => Array
        (
            [count_cols] => 3
            [count_rows] => 5
            [rebuild] => Rebuild Table
        )

    [import] => Array
        (
            [file] => 
            [import] => Upload CSV
        )

    [paste] => Array
        (
            [paste_delimiter] => 
            [data] => 
            [paste_import]  field_data_field_table field_data_field_table=> Import & Rebuild
        )

    [tabledata] => Array
        (
            [row_0] => Array
                (
                    [col_0] => 0
                    [col_1] => A
                    [col_2] => B
                    [weight] => 1
                )

            [row_1] => Array
                (
                    [col_0] => 1
                    [col_1] => A1
                    [col_2] => B1
                    [weight] => 2
                )

            [row_2] => Array
                (
                    [col_0] => 2
                    [col_1] => A2
                    [col_2] => B2
                    [weight] => 3
                )

            [row_3] => Array
                (
                    [col_0] => 3
                    [col_1] => A3
                    [col_2] => B3
                    [weight] => 4
                )

            [row_4] => Array
                (
                    [col_0] => 4
                    [col_1] => A4
                    [col_2] => B4
                    [weight] => 5
                )

        )

)

versus Drupal TableField 8.x-2.x as delta 0 (first element of a multivalue field) in table node__field_table:

Array
(
    [0] => Array
        (
            [0] => 0
            [1] => A
            [2] => B
        )

    [1] => Array
        (
            [0] => 1
            [1] => A1
            [2] => B1
        )

    [2] => Array
        (
            [0] => 2
            [1] => A2
            [2] => B2
        )

    [3] => Array
        (
            [0] => 3
            [1] => A3
            [2] => B3
        )

    [4] => Array
        (
            [0] => 4
            [1] => A4
            [2] => B4
        )

    [caption] => 
)
lolandese’s picture

It looks like some refactoring of the database structure is needed, just like was done on 7.x-2.x to go to 7.x-3.x. Most of that work is in #2697105: RFC: Proposal to Enable Table Data Storage as JSON. We can borrow from there or even make a bigger leap forward by pushing forward #2883127: Optional Enable Table Data Storage as JSON that makes use of the MySQL version 5.7+ JSON Data Type.

lolandese’s picture

larowlan’s picture

This update hook failed to update the schema key-value.

ThomWilhelm’s picture

I'm finding after upgrading from 8.x-2.0-alpha1 to 8.x-2.0-alpha3 when I try to programmatically create a node that contains a tablefield I get an error related to the newly introduced caption field:

$node = \Drupal\node\Entity\Node::create([
  'type' => 'application',
  'title' => "Test application",
  'uid' => $user->id(),
]);
$node->save();

Drupal\Core\Entity\EntityStorageException</em>: SQLSTATE[23000]: Integrity constraint violation: 1048 Column &#039;field_last_event_visitation_caption&#039; cannot be null: INSERT INTO {node__field_last_event_visitation} (entity_id, revision_id, bundle, delta, langcode, field_last_event_visitation_value, field_last_event_visitation_format, field_last_event_visitation_caption) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7);

Only way to avoid this is to save all the table fields again through the manage fields UI. Once you do this you get caption values in the field configuration (see the uploaded screenshot). I think a better update hook should have been provided for this that added the required caption value to existing fields.

lolandese’s picture

To summarize some of the comments above ("Needs work" items):

  • #18: Use a conditional to catch the use case of a tablefield used inside a paragraph.
  • #19: Make the caption field optional and #26 Make this checkbox also available under "Manage display".
  • #28: The caption value is stored twice in the database table for a created field.
  • #32: Some refactoring of the database structure is needed.
  • #34: Update hook failed to update the schema key-value.
  • #35: Update hook should add the required caption value to existing fields.
cgomezg’s picture

New version of #24 patch based on dev that:

1. Gives a field display option to toggle the caption field's visibility

lolandese’s picture

Status: Needs work » Needs review

The last patch needs review (#37).

Renrhaf’s picture

Status: Needs review » Reviewed & tested by the community

We use the patch from #37 since a lot of time and it seems to work properly so far.

osab’s picture

+1 to port this patch. #37 works fine and it is really helpful feature.

mudassar774’s picture

Not sure if its relevant, Can we make caption field position adjustable, Normally Table captions should be at bottom of Table