Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
We have the need to display a table caption.
I have attached a patch to allow this but the main issue I have faced is down the default table render element not allowing the caption element.
Comment | File | Size | Author |
---|---|---|---|
#37 | tablefield-add-a-caption-field-2875233-37.patch | 1.73 KB | cgomezg |
| |||
#35 | tablefield field config diff.png | 16.59 KB | ThomWilhelm |
#24 | tablefield-add-a-caption-field-24.patch | 2.26 KB | johnny5th |
#13 | tablefield-add-a-caption-field-13.patch | 6.56 KB | lapek |
Comments
Comment #2
sawtell CreditAttribution: sawtell commentedComment #3
sawtell CreditAttribution: sawtell commentedThere 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..
Comment #4
ToxaViking CreditAttribution: ToxaViking as a volunteer and at Lemberg Solutions, Drupal Ukraine Community commentedHere is my vision
Comment #5
ToxaViking CreditAttribution: ToxaViking as a volunteer and at Lemberg Solutions, Drupal Ukraine Community commentedComment #6
mertindervish CreditAttribution: mertindervish at Bulcode commentedI have fixed some bugs of #4 patch.
Comment #7
Dozz CreditAttribution: Dozz commentedAdded an update hook to the patch in #6.
Comment #9
Dozz CreditAttribution: Dozz commentedcoding standards fixes.
Comment #11
lapek CreditAttribution: lapek commentedI took a stab at this
I also:
Comment #12
lapek CreditAttribution: lapek commentedComment #13
lapek CreditAttribution: lapek commentedComment #14
lapek CreditAttribution: lapek commentedComment #16
lolandese CreditAttribution: lolandese commentedThanks to all for your contributions.
Comment #17
johnny5th CreditAttribution: johnny5th at Encore Multimedia commentedIs 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).
Comment #18
lolandese CreditAttribution: lolandese commentedSeems 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.
Comment #19
cgomezgHi all,
Can we set in config this field optional? not checked by default?
We are having issues because this field is now displaying.
Thanks,
Comment #20
lolandese CreditAttribution: lolandese commentedAgreed. Should be part of the field's display options.
Comment #21
cgomezgTable Caption isn't saving DEFAULT VALUE.
I should create a issue?
Comment #22
lolandese CreditAttribution: lolandese commentedFeel 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.
Comment #23
cgomezgIssue created and patch added for Caption default value
https://www.drupal.org/project/tablefield/issues/3021303
Comment #24
johnny5th CreditAttribution: johnny5th at Encore Multimedia commentedNew patch based on dev that:
Comment #25
lolandese CreditAttribution: lolandese commentedThere is a patch to review. Meanwhile, thanks for the contribution.
Comment #26
lolandese CreditAttribution: lolandese commentedNote 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?
Comment #27
lolandese CreditAttribution: lolandese commentedIf 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.
Comment #28
lolandese CreditAttribution: lolandese commentedAn 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:
The first caption above seems to make having a valid schema impossible:
The above schema will always result in:
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.
Comment #29
lolandese CreditAttribution: lolandese commentedComment #30
lolandese CreditAttribution: lolandese commentedIt 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.
Comment #31
lolandese CreditAttribution: lolandese commentedHere'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:
versus Drupal TableField 8.x-2.x as delta 0 (first element of a multivalue field) in table node__field_table:
Comment #32
lolandese CreditAttribution: lolandese commentedIt 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.
Comment #33
lolandese CreditAttribution: lolandese commentedComment #34
larowlanThis update hook failed to update the schema key-value.
Comment #35
ThomWilhelm CreditAttribution: ThomWilhelm commentedI'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:
Drupal\Core\Entity\EntityStorageException</em>: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'field_last_event_visitation_caption' 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.
Comment #36
lolandese CreditAttribution: lolandese commentedTo summarize some of the comments above ("Needs work" items):
Comment #37
cgomezgNew version of #24 patch based on dev that:
1. Gives a field display option to toggle the caption field's visibility
Comment #38
lolandese CreditAttribution: lolandese at Cognizant Technology Solutions commentedThe last patch needs review (#37).
Comment #39
RenrhafWe use the patch from #37 since a lot of time and it seems to work properly so far.
Comment #40
osab CreditAttribution: osab as a volunteer and at AnyforSoft, Drupal Ukraine Community for AnyforSoft commented+1 to port this patch. #37 works fine and it is really helpful feature.
Comment #41
mudassar774 CreditAttribution: mudassar774 commentedNot sure if its relevant, Can we make caption field position adjustable, Normally Table captions should be at bottom of Table