-1

In my case, My site URL is www.example.co.uk and also have a content page www.example.co.uk/bbc.

There is a menu link called BBC with https://www.bbc.co.uk under my menu menu menu link list. I can change the link from backend but how I alter that link-path programmatically?

I want to change "www" depending on sites, like if it's on test site then the link should be wwwtest.bbc.co.uk or in develop then wwwdev.bbc.co.uk .

  • 1
    It's simple, do not add the domain name to your link path. Don't do absolute path wwwtest.bbc.co.uk/home simply do what is called a relative path /home. – No Sssweat May 19 '18 at 0:37
  • Thanks, @NoSssweat , but in my case, I don't want to make any changes on the backend. – Sue Jun 12 '18 at 14:57
0
/**
 * Implements template_preprocess_menu_link().
 */
function my_module_preprocess_menu_link(&$variables) {
  $element = !empty($variables['element']) ? $variables['element'] : array();
  if (!empty($element['#title']) && $element['#title'] == 'BBC') {
    $url = $variables['element']['#href']; 
    $url_parts = parse_url($url); 
    $path = $GLOBALS['base_url'] . $url_parts['path']; 
    $options = array();
    foreach (array('query', 'fragment') as $part) {
      if (!empty($url_parts[$part])) {
        if ($part == 'query') {
          $option = drupal_get_query_parameters(drupal_get_query_array($url_parts[$part]));
        }
        else {
          $option = $url_parts[$part];
        }
        $options[$part] = $option;
      } 
    }
    $variables['element']['#href'] = url($path, $options);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.