From f9c530840c9e00cb03ead50ed90d21cbc2de3f51 Mon Sep 17 00:00:00 2001 From: Marc Berger Date: Mon, 16 Dec 2024 18:23:24 -0800 Subject: [PATCH 1/5] feat(SHS-5674): WIP - determining different link types with newer D10 function --- .../hs_field_helpers/hs_field_helpers.module | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module b/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module index 96d5ec4fa5..d706d0913c 100644 --- a/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module +++ b/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module @@ -16,9 +16,9 @@ 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; @@ -26,6 +26,7 @@ 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; @@ -652,3 +653,24 @@ function hs_field_helpers_form_alter(&$form, FormStateInterface $form_state, $fo unset($form['header']['views_bulk_operations_bulk_form']['select_all']); } } + +function hs_field_helpers_field_widget_complete_link_default_form_alter(&$field_widget_complete_form, FormStateInterface $form_state, $context) { + $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; + $supports_both = $supports_internal && $supports_external; + + dpm("internal:" . $supports_internal); + dpm("external:" . $supports_external); + dpm("both:" . $supports_both); + + if (isset($field_widget_complete_form['widget']) && is_array($field_widget_complete_form['widget'])) { + foreach ($field_widget_complete_form['widget'] as $key => &$item) { + if (is_array($item) && isset($item['uri'])) { + $item['uri']['#description'] = t('Custom hotdogs for sale'); + } + } + } +} From ecc0fa411ded4cf04caa33c09138d7a3d849f07d Mon Sep 17 00:00:00 2001 From: Marc Berger Date: Tue, 17 Dec 2024 10:57:53 -0800 Subject: [PATCH 2/5] feat(SHS-5674): Add custom help text to link_default widgets. --- .../hs_field_helpers/hs_field_helpers.module | 60 ++++++++++++++++--- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module b/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module index d706d0913c..a42d4d3b73 100644 --- a/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module +++ b/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module @@ -654,23 +654,65 @@ function hs_field_helpers_form_alter(&$form, FormStateInterface $form_state, $fo } } +/** + * 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; - $supports_both = $supports_internal && $supports_external; - dpm("internal:" . $supports_internal); - dpm("external:" . $supports_external); - dpm("both:" . $supports_both); + // 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 (isset($field_widget_complete_form['widget']) && is_array($field_widget_complete_form['widget'])) { - foreach ($field_widget_complete_form['widget'] as $key => &$item) { - if (is_array($item) && isset($item['uri'])) { - $item['uri']['#description'] = t('Custom hotdogs for sale'); - } + 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' => '', + '%no_link' => '', + ]); + } + + $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. + if (!$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. + if ($supports_internal && $supports_external) { + $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; } } + } From ad3850703df1480765458e99d5c156fabc651475 Mon Sep 17 00:00:00 2001 From: Marc Berger Date: Wed, 18 Dec 2024 16:23:52 -0800 Subject: [PATCH 3/5] feat(SHS-5674): Exit early if link doesn't support either type --- .../modules/humsci/hs_field_helpers/hs_field_helpers.module | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module b/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module index a42d4d3b73..10fbdbd989 100644 --- a/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module +++ b/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module @@ -673,6 +673,10 @@ function hs_field_helpers_field_widget_complete_link_default_form_alter(&$field_ $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 = [ From 5cd8d304601bc364f1bdd552d482a136e05708e6 Mon Sep 17 00:00:00 2001 From: Marc Berger Date: Wed, 18 Dec 2024 17:15:44 -0800 Subject: [PATCH 4/5] fix(SHS-5674): Fix styling issue with appended text --- .../humsci/hs_field_helpers/hs_field_helpers.module | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module b/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module index 10fbdbd989..05b2ed8921 100644 --- a/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module +++ b/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module @@ -694,22 +694,22 @@ function hs_field_helpers_field_widget_complete_link_default_form_alter(&$field_ $placeholders = [ '%internal_link' => '/about/contact-us', '%external_link' => 'https://www.stanford.edu', - '%appended' => $appended, + '@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); + $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. if (!$supports_internal && $supports_external) { - $description = t('This must be an external URL such as %external_link. %appended', $placeholders); + $description = t('This must be an external URL such as %external_link. @appended', $placeholders); } // Both internal and external link help text. if ($supports_internal && $supports_external) { - $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); + $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. From 23f1f6bb8296ce5b45ddea7d417e10aee7d68b35 Mon Sep 17 00:00:00 2001 From: Marc Berger Date: Fri, 20 Dec 2024 16:29:27 -0800 Subject: [PATCH 5/5] refactor(SHS-5674): Change if/else to be more performant --- .../modules/humsci/hs_field_helpers/hs_field_helpers.module | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module b/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module index 05b2ed8921..a8fa63cf51 100644 --- a/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module +++ b/docroot/modules/humsci/hs_field_helpers/hs_field_helpers.module @@ -703,12 +703,12 @@ function hs_field_helpers_field_widget_complete_link_default_form_alter(&$field_ } // External link help text. - if (!$supports_internal && $supports_external) { + 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. - if ($supports_internal && $supports_external) { + 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); }