Skip to content
Snippets Groups Projects
Commit 294a5671 authored by Lily Yan's avatar Lily Yan Committed by Eric Bremner
Browse files

ISTWCMS-6195 Remove support for nolink and button in all link fields

parent 6fa3f1ea
No related branches found
No related tags found
1 merge request!314ISTWCMS-6195 Refactor generic nolink/button link field function in uw_cfg_common.module
......@@ -1783,3 +1783,51 @@ function uw_cfg_common_views_query_alter(ViewExecutable $view, QueryPluginBase $
$query->addOrderBy('redirect', 'rid');
}
}
/**
* Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
*/
function uw_cfg_common_field_widget_single_element_link_default_form_alter(array &$element, FormStateInterface $form_state, array $context) {
// Get field information from context.
$field_definition = $context['items']->getFieldDefinition();
// Set custom description only for event map field.
if ($field_definition->getName() == 'field_uw_event_map') {
$element['uri']['#description'] = t('Optional: provide a link to a map with the event location (e.g. https://uwaterloo.ca/map/). This must be an external URL such as https://example.com.');
}
else {
// Set custom description for all link fields except event map.
$element['uri']['#description'] = t('Start typing the title of a piece of content to select it. You can also enter an internal path such as /blog or an external URL such as https://example.com. Enter <front> to link to the front page.');
// Set custom description only for event host field.
if ($field_definition->getName() == 'field_uw_event_host') {
$element['uri']['#description'] .= ' ' . t('Enter <nolink> to display link text only.');
}
}
// Add link uri field element validation function.
$element['uri']['#element_validate'][] = '_uw_cfg_common_uw_link_validator';
}
/**
* Link uri field validation function.
*/
function _uw_cfg_common_uw_link_validator($element, &$form_state, $form) {
$uri = $element['#value'];
// Get the field name and set it up in the format used by setErrorByName.
$fieldname = rtrim($element['#name'], ']');
$pos = strpos($fieldname, '[');
$fieldname = substr_replace($fieldname, '][', $pos, 1);
// Buttons are just not allowed.
if ($uri == '<button>') {
$form_state->setErrorByName($fieldname, t('The &lt;button&gt; value is not supported for this field.'));
}
// Nolink is not allowed, with one exception.
if ($uri == '<nolink>' && $fieldname != 'field_uw_event_host][0][uri') {
$form_state->setErrorByName($fieldname, t('The &lt;nolink&gt; value is not supported for this field.'));
}
}
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