function address_map_link_preprocess_field in Address Map (& Directions) Link 8
Implements hook_preprocess_HOOK() for field templates.
Adds link around address fields.
File
- ./
address_map_link.module, line 144 - Contains address_map_link.module.
Code
function address_map_link_preprocess_field(&$variables) {
if ($variables['element']['#field_type'] === 'address') {
// Drill down to field formatter settings.
$render_display = EntityViewDisplay::collectRenderDisplay($variables['element']['#object'], $variables['element']['#view_mode']);
$field_display = $render_display
->getComponent($variables['element']['#field_name']);
// Check setting to determine if we are supposed to render as a link.
if (isset($field_display['third_party_settings']['address_map_link'])) {
$settings =& $field_display['third_party_settings']['address_map_link'];
if ($settings['link_address'] && isset($settings['map_link_type'])) {
/** @var \Drupal\address_map_link\MapLinkManager $mapLinkManager */
$mapLinkManager = \Drupal::service('plugin.manager.map_link');
/** @var \Drupal\address_map_link\MapLinkInterface $mapLinkType */
$mapLinkType = $mapLinkManager
->createInstance($settings['map_link_type']);
// Check setting, determine if we should open the link in a new window.
$openInNewWindow = !empty($settings['map_link_new_window']);
// Check setting to determine where we are placing the link.
$linkPosition = !empty($settings['map_link_position']) ? $settings['map_link_position'] : 'address';
// Check setting to determine the link's text.
if (!empty($settings['map_link_text'])) {
$linkText = Html::escape($settings['map_link_text']);
// Replace tokens if token module is enabled.
if (\Drupal::moduleHandler()
->moduleExists('token')) {
$linkText = \Drupal::token()
->replace($linkText, [
$variables['element']['#entity_type'] => $variables['element']['#object'],
]);
}
}
else {
$linkText = t('View Map');
}
/** @var \Drupal\address\AddressInterface $item */
foreach ($variables['element']['#items'] as $delta => &$item) {
$url = $mapLinkType
->getAddressUrl($item)
->toString();
$aTagOpen = '<a href="' . $url . '" class="address-map-link"' . ($openInNewWindow ? ' target="_blank"' : '') . '>';
$aTagClose = '</a>';
// Define prefix and suffix if not already set.
if (!isset($variables['items'][$delta]['content']['#prefix'])) {
$variables['items'][$delta]['content']['#prefix'] = '';
}
if (!isset($variables['items'][$delta]['content']['#suffix'])) {
$variables['items'][$delta]['content']['#suffix'] = '';
}
switch ($linkPosition) {
case 'before':
$variables['items'][$delta]['content']['#prefix'] = $aTagOpen . $linkText . $aTagClose . $variables['items'][$delta]['content']['#prefix'];
break;
case 'after':
$variables['items'][$delta]['content']['#suffix'] .= $aTagOpen . $linkText . $aTagClose;
break;
case 'address':
default:
$variables['items'][$delta]['content']['#prefix'] = $aTagOpen . $variables['items'][$delta]['content']['#prefix'];
$variables['items'][$delta]['content']['#suffix'] .= $aTagClose;
break;
}
}
}
}
}
}