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

SHS-5674: Help text for link fields is confusing #1702

Merged
merged 7 commits into from
Jan 13, 2025
70 changes: 69 additions & 1 deletion docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Layout\LayoutDefinition;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\field_permissions\Plugin\FieldPermissionTypeInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\field_permissions\Plugin\FieldPermissionTypeInterface;
use Drupal\hs_field_helpers\Plugin\Field\FieldFormatter\AddToCalFormatter;
use Drupal\hs_field_helpers\Plugin\Field\FieldFormatter\EntityTitleHeading;
use Drupal\hs_field_helpers\Plugin\Field\FieldFormatter\HsViewfieldFormatterDefault;
use Drupal\hs_field_helpers\Plugin\Field\FieldType\DisplayModeField;
use Drupal\hs_field_helpers\Plugin\Field\FieldWidget\DateTimeYearOnly;
use Drupal\hs_field_helpers\Plugin\Field\FieldWidget\HsViewfieldWidgetSelect;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\link\Plugin\Field\FieldType\LinkItem;
use Drupal\ui_patterns\Element\PatternContext;
use Drupal\ui_patterns\UiPatterns;

Expand Down Expand Up @@ -652,3 +653,70 @@ function hs_field_helpers_form_alter(&$form, FormStateInterface $form_state, $fo
unset($form['header']['views_bulk_operations_bulk_form']['select_all']);
}
}

/**
* Implements hook_field_widget_complete_WIDGET_TYPE_form_alter().
*/
function hs_field_helpers_field_widget_complete_link_default_form_alter(&$field_widget_complete_form, FormStateInterface $form_state, $context) {
// Add custom help text to link_default widgets.
if (!isset($field_widget_complete_form['widget']) || !is_array($field_widget_complete_form['widget'])) {
return;
}

$description = NULL;
$appended = NULL;

// Figure out if link supports internal, external, or both.
$field_definition = $context['items']->getFieldDefinition();
$settings = $field_definition->getSettings();
$link_type = $settings['link_type'];
$supports_internal = ($link_type & LinkItem::LINK_INTERNAL) === LinkItem::LINK_INTERNAL;
$supports_external = ($link_type & LinkItem::LINK_EXTERNAL) === LinkItem::LINK_EXTERNAL;

if (!$supports_internal && !$supports_external) {
return;
}

// Append extra text to the following form_ids.
$base_form = $form_state->getBuildInfo()['base_form_id'];
$append_extra_text_to = [
'shortcut_form',
'menu_link_content_form',
];

if (in_array($base_form, $append_extra_text_to)) {
$appended = t('Enter %front to link to the front page. Enter %no_link to display a menu item that is only text without a link.', [
'%front' => '<front>',
'%no_link' => '<nolink>',
]);
}

$placeholders = [
'%internal_link' => '/about/contact-us',
'%external_link' => 'https://www.stanford.edu',
'@appended' => $appended,
];

// Internal link help text.
if ($supports_internal && !$supports_external) {
$description = t('This must be an internal path such as %internal_link You can also start typing the title of a piece of content to select it. @appended', $placeholders);
}

// External link help text.
elseif (!$supports_internal && $supports_external) {
$description = t('This must be an external URL such as %external_link. @appended', $placeholders);
}

// Both internal and external link help text.
else {
$description = t('Start typing the title of a piece of content to select it. You can also enter an internal path such as %internal_link or an external URL such as %external_link. @appended', $placeholders);
}

// Add custom text to help text.
foreach ($field_widget_complete_form['widget'] as &$item) {
if (is_array($item) && isset($item['uri'])) {
$item['uri']['#description'] = $description;
}
}

}
Loading