Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion services/drupal/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
"drupal/layout_section_classes": "^1.6",
"drupal/limited_field_widgets": "^2.1@beta",
"drupal/linked_field": "^1.7",
"drupal/linkit": "^6.0@RC",
"drupal/linkit": "^7.0",
"drupal/linkit_telephone": "^1.0@dev",
"drupal/m4032404": "^2.0",
"drupal/masquerade": "^2.0",
Expand Down
23 changes: 9 additions & 14 deletions services/drupal/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions services/drupal/composer.patches.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@
"Fix label accessibility when new select elements created": "patches/shs-select-label-accessibility-d10.patch"
},
"drupal/linkit": {
"Editing links does not remove possible stale data- attributes": "https://www.drupal.org/files/issues/2021-08-09/linkit_rename-stale-data-attributes_3111578-3.patch",
"Allow scrolling autocomplete results": "https://www.drupal.org/files/issues/2021-11-02/linkit-missing_scroll_bar-3067904-3.patch"
"Editing links does not remove possible stale data- attributes": "https://www.drupal.org/files/issues/2021-08-09/linkit_rename-stale-data-attributes_3111578-3.patch"
},
"drupal/paragraphs_entity_embed": {
"Prevent the reuse of embedded paragraphs": "https://www.drupal.org/files/issues/2020-04-30/paragraphs_entity_embed-single_use-3132549-2.patch",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ epa-edit-media-ckeditor5:
dependencies:
- ckeditor5/ckeditor5

epa_wysiwyg_linkit:
js:
js/epa_wysiwyg_linkit.js: {}
dependencies:
- core/drupal
- core/once

web-area-linkit-ckeditor5:
js:
js/build/webAreaLinkit.js: { preprocess: false, minified: true }
Expand Down
155 changes: 58 additions & 97 deletions services/drupal/web/modules/custom/epa_wysiwyg/epa_wysiwyg.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

use Drupal\Component\Utility\DeprecationHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\StringTranslation\ByteSizeMarkup;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\file\IconMimeTypes;

Expand All @@ -20,8 +20,7 @@ function epa_wysiwyg_library_info_alter(&$libraries, $extension) {
// Add CKEditor overrides JS file to CKEditor library.
if ($extension == 'ckeditor' && isset($libraries['drupal.ckeditor'])) {
$module_path = \Drupal::service('extension.list.module')->getPath('epa_wysiwyg');
$js_path = "/$module_path/js/ckeditor-overrides.js";
$libraries['drupal.ckeditor']['js'][$js_path] = [];
$libraries['drupal.ckeditor']['js']["/$module_path/js/ckeditor-overrides.js"] = [];
}
}

Expand Down Expand Up @@ -49,7 +48,12 @@ function epa_wysiwyg_pathologic_alter(&$url_params, $parts, $settings) {
function epa_wysiwyg_theme() {
return [
'epa_file_link' => [
'variables' => ['file' => NULL, 'link_text' => NULL, 'attributes' => [], 'media_accessibility' => NULL],
'variables' => [
'file' => NULL,
'link_text' => NULL,
'attributes' => [],
'media_accessibility' => NULL,
],
],
];
}
Expand Down Expand Up @@ -105,113 +109,70 @@ function template_preprocess_epa_file_link(&$variables) {

/**
* Implements hook_form_FORM_ID_alter().
*
* Editor link dialog (Linkit).
*/
function epa_wysiwyg_form_editor_link_dialog_alter(&$form, FormStateInterface $form_state, $form_id) {
// Attach our custom javascript library.
$form['#attached']['library'][] = 'epa_wysiwyg/epa-wysiwyg-linkit';

// Provide a fieldset for switching the profile.
$form['attributes']['select-profile'] = [
'#title' => t('Filter search results'),
function epa_wysiwyg_form_editor_link_dialog_alter(
array &$form,
FormStateInterface $form_state,
$form_id
) {
// Attach the JS that controls the UI switching.
$form['#attached']['library'][] = 'epa_wysiwyg/epa_wysiwyg_linkit';

// Determine active profile (default fallback).
$active_profile = $form_state->get('epa_linkit_profile') ?? 'default';

// Profile selector radios.
$form['attributes']['epa_linkit_profile'] = [
'#type' => 'radios',
'#title' => t('Filter scope'),
'#options' => [
'web_area_content' => t('Your internal links'),
'default' => t('No filter: all WebCMS content'),
'web_area_content' => t('Your group links'),
],
'web_area_content' => [
'#description' => t('Search within your web areas only.'),
],
'default' => [
'#description' => t('Search within your web areas and www.epa.gov'),
],
'#default_value' => $active_profile,
'#weight' => -20,
'#default_value' => 'default',
'#ajax' => [
'callback' => 'epa_wysiwyg_linkit_profile_ajax',
'wrapper' => 'epa-linkit-wrapper',
],
];

// Add states to disable and hide the default linkit input unless the default
// profile is selected.
$form['attributes']['href_default'] = array_merge($form['attributes']['href'], [
'#type' => 'linkit',
'#description' => t('Type the page title, paste in a complete external URL, or add an email address or phone number.'),
'#autocomplete_route_name' => 'linkit.autocomplete',
'#autocomplete_route_parameters' => [
'linkit_profile_id' => 'default',
],
"#weight" => -10,
'#default_value' => $input['href'] ?? '',
'#states' => [
'visible' => [
':input[name="attributes[select-profile]"]' => ['value' => 'default'],
],
'disabled' => [
':input[name="attributes[select-profile]"]' => ['value' => 'web_area_content'],
],
],
]);

// Provide a second linkit input that uses the web_area_content linkit profile
// and is disabled and hidden unless the web_area_content profile is selected.
$form['attributes']['href_web_area_content'] = array_merge($form['attributes']['href'], [
'#type' => 'linkit',
'#description' => t('Type the page title, paste in a complete external URL, or add an email address or phone number.'),
'#autocomplete_route_name' => 'linkit.autocomplete',
'#autocomplete_route_parameters' => [
'linkit_profile_id' => 'web_area_content',
],
"#weight" => -10,
'#default_value' => $input['href'] ?? '',
'#states' => [
'visible' => [
':input[name="attributes[select-profile]"]' => ['value' => 'web_area_content'],
],
'disabled' => [
':input[name="attributes[select-profile]"]' => ['value' => 'default'],
],
],
]);

$form['attributes']['href'] = array_merge($form['attributes']['href'], [
'#states' => [
'invisible' => [
':input[name="attributes[select-profile]"]' => [
['value' => 'web_area_content'],
'or',
['value' => 'default'],
],
],
],
]);
// Wrap the Linkit field so it can be rebuilt.
$form['attributes']['href']['#prefix'] = '<div id="epa-linkit-wrapper">';
$form['attributes']['href']['#suffix'] = '</div>';

array_unshift($form['#validate'], '_epa_wysiwyg_populate_href');
// Force Linkit field + selected profile.
$form['attributes']['href']['#type'] = 'linkit';
$form['attributes']['href']['#autocomplete_route_name'] = 'linkit.autocomplete';
$form['attributes']['href']['#autocomplete_route_parameters'] = [
'linkit_profile_id' => $active_profile,
];

// Persist selection on submit.
$form['#submit'][] = 'epa_wysiwyg_linkit_profile_submit';
}

/**
* Custom logic to accommodate the additional href_default and
* href_web_area_content attributes.
* AJAX callback to rebuild the Linkit field when profile changes.
*/
function _epa_wysiwyg_populate_href(array &$form, FormStateInterface $form_state) {
$href_profile_fields = [
'href_default',
'href_web_area_content',
];
function epa_wysiwyg_linkit_profile_ajax(array &$form, FormStateInterface $form_state) {
$profile = $form_state->getValue(['attributes', 'epa_linkit_profile']);
$form_state->set('epa_linkit_profile', $profile);
$form_state->setRebuild(TRUE);

$href_value = $form_state->getValue(['attributes', 'href']);

foreach ($href_profile_fields as $field_name) {
// The href attribute is reset to empty when the form is initialized. If the
// attribute has a value when the form is submitted, then another module
// (i.e. anchor) updated it. Let's set our linkit profile inputs to match.
if (!empty($href_value)) {
$form_state->setValue(['attributes', $field_name], $href_value);
}
// The href attribute is empty so we want to populate it with the value from
// the linkit profile attribute that has a value.
else {
$value = $form_state->getValue(['attributes', $field_name]);
if (!empty($value)) {
$form_state->setValue(['attributes', 'href'], $value);
}
}
return $form['attributes']['href'];
}

/**
* Submit handler to store selected profile.
*/
function epa_wysiwyg_linkit_profile_submit(array &$form, FormStateInterface $form_state) {
if ($form_state->hasValue(['attributes', 'epa_linkit_profile'])) {
$form_state->set(
'epa_linkit_profile',
$form_state->getValue(['attributes', 'epa_linkit_profile'])
);
}
}
Loading