Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust .ck-content aria values (label, describedby) #166

Merged
merged 4 commits into from Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Field.php
Expand Up @@ -317,6 +317,8 @@ protected function inputHtml(mixed $value, ElementInterface $element = null): st
$baseConfig = [
'defaultTransform' => $defaultTransform?->handle,
'elementSiteId' => $element?->siteId,
'fieldName' => Craft::t('site', $this->name),
'describedBy' => $this->_describedBy($view),
'findAndReplace' => [
'uiType' => 'dropdown',
],
Expand Down Expand Up @@ -910,4 +912,25 @@ private function _adjustPurifierConfig(HTMLPurifier_Config $purifierConfig): HTM

return $purifierConfig;
}

/**
* Namespaces field's $describedBy value to be passed to the field.
*
* @param View $view
* @return string
*/
private function _describedBy(View $view): string
{
if (!empty($this->describedBy)) {
$describedByArray = explode(' ', $this->describedBy);
$namespace = trim(preg_replace('/\[|\]/', '-', $view->getNamespace()), '-');
foreach ($describedByArray as $key => $item) {
$describedByArray[$key] = "$namespace-$item";
}

return implode(' ', $describedByArray);
}

return '';
}
}
2 changes: 1 addition & 1 deletion src/web/assets/ckeditor/dist/ckeditor5-craftcms.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/ckeditor/dist/ckeditor5-craftcms.js.map

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions src/web/assets/ckeditor/src/ckeditor5-craftcms.js
Expand Up @@ -404,6 +404,30 @@ export const create = async function (element, config) {
Object.assign({plugins}, config),
);

// accessibility: https://github.com/craftcms/ckeditor/issues/74
editor.editing.view.change((writer) => {
const viewEditableRoot = editor.editing.view.document.getRoot();

// adjust aria-label
if (typeof config.fieldName != 'undefined' && config.fieldName.length) {
let ariaLabel = viewEditableRoot.getAttribute('aria-label');
writer.setAttribute(
'aria-label',
config.fieldName + ', ' + ariaLabel,
viewEditableRoot,
);
}

// adjust aria-describedby
if (typeof config.describedBy != 'undefined' && config.describedBy.length) {
writer.setAttribute(
'aria-describedby',
config.describedBy,
viewEditableRoot,
);
}
});

// Update the source element before the initial form value has been recorded,
// in case the value needs to be normalized
editor.updateSourceElement();
Expand Down