Skip to content
Snippets Groups Projects
Commit 4e93392f authored by Stephen Mustgrave's avatar Stephen Mustgrave
Browse files

Issue #3481732 by edwardsay: Stacked table without header causes warnings

parent dd5d85e3
Branches 1.0.x
Tags 1.0.1
1 merge request!87Issue #3481732 by edwardsay: Stacked table without header causes warnings
Pipeline #314802 passed with warnings
......@@ -3,6 +3,7 @@
namespace Drupal\uswds_ckeditor_integration\Plugin\Filter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Logger\LoggerChannelTrait;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
......@@ -16,7 +17,9 @@
* type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
* )
*/
class FilterTableAttributes extends FilterBase {
class FilterUswdsTableStacked extends FilterBase {
use LoggerChannelTrait;
/**
* {@inheritdoc}
......@@ -31,45 +34,48 @@ public function process($text, $langcode): FilterProcessResult {
// Add USWDS Class to table.
foreach ($tables as $table) {
$rows = $xpath->query('.//tr', $table);
$headers = $xpath->query('//thead//th', $rows->item(0));
$table_headers = [];
// Add attributes to column headers.
foreach ($headers as $header) {
$header->setAttribute('scope', 'col');
$label = $header->nodeValue;
$table_headers[] = $label;
}
$headers = $xpath->query('.//thead//th', $rows->item(0));
// Add scope to row.
$skip_first = TRUE;
foreach ($rows as $row) {
if ($skip_first) {
$skip_first = FALSE;
continue;
if ($headers->length > 0) {
$table_headers = [];
// Add attributes to column headers.
foreach ($headers as $header) {
$header->setAttribute('scope', 'col');
$label = $header->nodeValue;
$table_headers[] = $label;
}
$counter = 0;
foreach ($row->childNodes as $key => $tag) {
switch ($tag->nodeName) {
case 'td':
$data_label = $table_headers[$counter];
$tag->setAttribute('data-label', $data_label);
if ($key === 0) {
$tag->setAttribute('scope', 'row');
}
$counter++;
break;
default:
break;
// Add scope to row.
$skip_first = TRUE;
foreach ($rows as $row) {
if ($skip_first) {
$skip_first = FALSE;
continue;
}
$counter = 0;
foreach ($row->childNodes as $key => $tag) {
switch ($tag->nodeName) {
case 'td':
$data_label = $table_headers[$counter];
$tag->setAttribute('data-label', $data_label);
if ($key === 0) {
$tag->setAttribute('scope', 'row');
}
$counter++;
break;
default:
break;
}
}
}
$result->setProcessedText(Html::serialize($dom));
}
else {
$this->getLogger('uswds_ckeditor_integration')->warning('Table without header trying to use USWDS stacked, not setting header is an accessibility issue.');
}
}
$result->setProcessedText(Html::serialize($dom));
}
return $result;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment