diff --git a/src/Plugin/Field/FieldFormatter/TablefieldFormatter.php b/src/Plugin/Field/FieldFormatter/TablefieldFormatter.php index 07aa1ce..7657184 100644 --- a/src/Plugin/Field/FieldFormatter/TablefieldFormatter.php +++ b/src/Plugin/Field/FieldFormatter/TablefieldFormatter.php @@ -136,14 +136,12 @@ class TablefieldFormatter extends FormatterBase implements ContainerFactoryPlugi $entity_type = $entity->getEntityTypeId(); $entity_id = $entity->id(); - $row_header = $this->getSetting('row_header'); - $column_header = $this->getSetting('column_header'); - $elements = []; $header = []; foreach ($items as $delta => $table) { - + $row_header = $table->row_header; + $column_header = $table->column_header; if (!empty($table->value)) { // Tablefield::rationalizeTable($table->value);. $tabledata = $table->value; @@ -225,7 +223,7 @@ class TablefieldFormatter extends FormatterBase implements ContainerFactoryPlugi 'tablefield', ], ], - '#caption' => $caption, + '#caption' => $table->caption ?? $tabledata['caption'] ?? '', '#prefix' => '
', '#suffix' => '
', '#responsive' => FALSE, diff --git a/src/Plugin/Field/FieldType/TablefieldItem.php b/src/Plugin/Field/FieldType/TablefieldItem.php index b4ec661..37bebe5 100644 --- a/src/Plugin/Field/FieldType/TablefieldItem.php +++ b/src/Plugin/Field/FieldType/TablefieldItem.php @@ -44,6 +44,16 @@ class TablefieldItem extends FieldItemBase { 'length' => 255, 'default value' => '', ], + 'row_header' => [ + 'type' => 'int', + 'size' => 'tiny', + 'default value' => 0, + ], + 'column_header' => [ + 'type' => 'int', + 'size' => 'tiny', + 'default value' => 0, + ], ], ]; } @@ -146,6 +156,12 @@ class TablefieldItem extends FieldItemBase { $properties['caption'] = DataDefinition::create('string') ->setLabel(t('Table Caption')); + $properties['row_header'] = DataDefinition::create('boolean') + ->setLabel(t('Display first row as a table header')); + + $properties['column_header'] = DataDefinition::create('boolean') + ->setLabel(t('Display first column as a table header')); + return $properties; } @@ -182,7 +198,10 @@ class TablefieldItem extends FieldItemBase { if (isset($values['caption'])) { $values['value']['caption'] = $values['caption']; } - + if (!empty($values['header_orientation'])) { + $values['row_header'] = $values['header_orientation']['row_header'] ?? 0; + $values['column_header'] = $values['header_orientation']['column_header'] ?? 0; + } // If "Lock defaults" is enabled the table needs sorting. $lock = $this->getFieldDefinition()->getSetting('lock_values'); if ($lock) { diff --git a/src/Plugin/Field/FieldWidget/TablefieldWidget.php b/src/Plugin/Field/FieldWidget/TablefieldWidget.php index f07f8ea..4721522 100644 --- a/src/Plugin/Field/FieldWidget/TablefieldWidget.php +++ b/src/Plugin/Field/FieldWidget/TablefieldWidget.php @@ -146,7 +146,21 @@ class TablefieldWidget extends WidgetBase implements ContainerFactoryPluginInter '#size' => 60, '#description' => $this->t('This brief caption will be associated with the table and will help screen reader better describe the content within.'), ]; - + $element['header_orientation'] = [ + '#type' => 'fieldset', + '#title' => $this->t('Header orientation'), + '#description' => $this->t("Add table header(s) to help people understand the information in the table. People who use screen readers need headers to help them navigate through the table and understand the row and column they're in."), + ]; + $element['header_orientation']['row_header'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Row Header'), + '#default_value' => $items[$delta]->row_header ?? 1, + ]; + $element['header_orientation']['column_header'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Column Header'), + '#default_value' => $items[$delta]->column_header ?? 0, + ]; $element = [ '#type' => 'tablefield', '#input_type' => $this->getSetting('input_type'), diff --git a/tablefield.install b/tablefield.install index d5ea4fb..f28f0a1 100644 --- a/tablefield.install +++ b/tablefield.install @@ -17,6 +17,24 @@ function tablefield_update_8001() { ]); } +/** + * Adds columns for row and column headers. + */ +function tablefield_update_8002() { + tablefield_add_new_column('row_header', [ + 'type' => 'int', + 'size' => 'tiny', + 'default' => 0, + 'not null' => TRUE, + ]); + tablefield_add_new_column('column_header', [ + 'type' => 'int', + 'size' => 'tiny', + 'default' => 0, + 'not null' => TRUE, + ]); +} + /** * Helper function to add new columns to the field schema. * diff --git a/tests/src/Functional/TableValueFieldTest.php b/tests/src/Functional/TableValueFieldTest.php index e28bba9..bdf8af4 100644 --- a/tests/src/Functional/TableValueFieldTest.php +++ b/tests/src/Functional/TableValueFieldTest.php @@ -40,6 +40,13 @@ class TableValueFieldTest extends BrowserTestBase { */ public function testTableField() { $this->drupalGet('node/add/article'); + $assert_session = $this->assertSession(); + + // Verify header orientation elements. + $assert_session->pageTextContains('Header orientation'); + $assert_session->elementExists('css', '#edit-field-table-0-header-orientation-row-header'); + $assert_session->elementExists('css', '#edit-field-table-0-header-orientation-column-header'); + $assert_session->checkboxChecked('field_table[0][header_orientation][row_header]'); // Create a node. $edit = []; @@ -57,7 +64,6 @@ class TableValueFieldTest extends BrowserTestBase { $edit['field_table[0][tablefield][table][2][2]'] = 'Row 2-3'; $this->drupalPostForm(NULL, $edit, t('Save')); - $assert_session = $this->assertSession(); $assert_session->pageTextContains('Article Llamas are cool has been created.'); $assert_session->elementContains('css', 'table#tablefield-node-1-field_table-0 caption', 'Table caption');