Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Currently it is not possible to use custom social sharing icons #290

Closed
rooby opened this issue Jul 5, 2016 · 7 comments
Closed

Currently it is not possible to use custom social sharing icons #290

rooby opened this issue Jul 5, 2016 · 7 comments

Comments

@rooby
Copy link

rooby commented Jul 5, 2016

GovCMS includes the Service Links module to provide social media sharing.

On the admin settings form for the service links module (admin/config/services/service-links) there is a configuration option for "Service Links icons > Standard folder", where you can tell the module to get its icons from a different directory.

If you set this to sites/all/themes/THEMENAME/images or similar then you end up with broken images on the Acquia platform.

This is because when the page renders it is still looking for sites/all/themes/THEMENAME/images/facebook.png but the image has been moved to somewhere like this: sites/g/files/net1234/themes/site/images/twitter.png.

Previous discussions with Acquia have informed me that URLs have to run through drupal_get_path() for them to be correctly rewritten (I'm assuming this information is correct) so I thought maybe I could override the themeing of these icons to use that to generate paths but that hasn't worked either (the code I used to test is below).

I will ask Acquia to see if they can help but I'm also opening this issue in case someone here can help and also because whatever the solution, something needs to be rolled in here so that if you use that setting it works.

Theme override (my change is the code involving $icon_path).
It's hard, not being able to do debugging on the Acquia platform but in local tests my code runs and correctly generates the path

/**
 * Implements theme_service_links_build_link().
 *
 * Output the markup for a single service link following the style rules.
 *
 * If the service links icons standard folder has been changed to point to
 * somewhere in this theme. rewrite the icon path using drupal_get_path() so
 * that GovCMS file paths work correctly.
 *
 * @param array $variables
 *   The variables passed in from the theme call and preprocess functions.
 *
 * @return string
 *  The markup for the service link.
 */
function THEMENAME_service_links_build_link($variables) {
  $text = $variables['text'];
  $url = $variables['url'];
  $image = $variables['image'];
  $nodelink = $variables['nodelink'];
  $style = $variables['style'];
  $attributes = $variables['attributes'];

  // If we have overridden the icon path with a path to this theme then
  // generate the path in a manner that will work with GovCMS.
  $gov_cms_icon_path = NULL;
  if ($icon_path_override = variable_get('service_links_path_icons', '')) {
    $theme_path = path_to_theme();
    if (strpos($icon_path_override, $theme_path) === 0) {
      $gov_cms_icon_path = base_path() . drupal_get_path('theme', 'THEMENAME') . substr($icon_path_override, strlen($theme_path)) . '/' . $image;
    }
  }
  $icon_path = $gov_cms_icon_path ? $gov_cms_icon_path : service_links_expand_path($image);

  if ($nodelink) {
    $query = isset($url[1]) ? $url[1] : NULL;

    switch ($style) {
      case SERVICE_LINKS_STYLE_TEXT:
        $link = array(
          'title' => $text,
          'href' => $url[0],
          'query' => $query,
          'attributes' => $attributes,
        );
        break;
      case SERVICE_LINKS_STYLE_IMAGE:
        $alt_text = t('!name logo', array('!name' => $text));
        $link = array(
          'title' => theme('image', array('path' => $icon_path, 'alt' => $alt_text)),
          'href' => $url[0],
          'query' => $query,
          'attributes' => $attributes,
          'html' => TRUE,
        );
        break;
      case SERVICE_LINKS_STYLE_IMAGE_AND_TEXT:
        $alt_text = t('!name logo', array('!name' => $text));
        $link = array(
          'title' => theme('image', array('path' => $icon_path, 'alt' => $alt_text)) .' '. $text,
          'href' => $url[0],
          'query' => $query,
          'attributes' => $attributes,
          'html' => TRUE,
        );
        break;
      case SERVICE_LINKS_STYLE_EMPTY:
        $link = array(
          'title' => '<span class="element-invisible">' . $text . '</span>',
          'href' => $url[0],
          'query' => $query,
          'attributes' => $attributes,
          'html' => TRUE,
        );
        break;
      default:
        $link = theme($style, $variables);
    }
  }
  else {
    $attributes = array('attributes' => $attributes);
    if (isset($url[1])) {
      $attributes['query'] = $url[1];
    }

    switch ($style) {
      case SERVICE_LINKS_STYLE_TEXT:
        $link = l($text, $url[0], $attributes);
        break;
      case SERVICE_LINKS_STYLE_IMAGE:
        $attributes['html'] = TRUE;
        $alt_text = t('!name logo', array('!name' => $text));
        $link = l(theme('image', array('path' => $icon_path, 'alt' => $alt_text)), $url[0], $attributes);
        break;
      case SERVICE_LINKS_STYLE_IMAGE_AND_TEXT:
        $attributes['html'] = TRUE;
        $alt_text = t('!name logo', array('!name' => $text));
        $link = l(theme('image', array('path' => $icon_path, 'alt' => $alt_text)) .' '. $text, $url[0], $attributes);
        break;
      case SERVICE_LINKS_STYLE_EMPTY:
        $attributes['html'] = TRUE;
        $link = l('<span class="element-invisible">' . $text . '</span>', $url[0], $attributes);
        break;
      case SERVICE_LINKS_STYLE_FISHEYE:
        $attributes['attributes']['class'] = isset($attributes['attributes']['class']) ? array_merge($attributes['attributes']['class'], array('fisheyeItem')) : array('fisheyeItem');
        $attributes['html'] = TRUE;
        $link = l(theme('image', array('path' => service_links_expand_path($image, 'fisheye'), 'alt' => $text, 'getsize' => FALSE)) .'<span>'. $text .'</span>', $url[0], $attributes);
        break;
      default:
        $link = theme($style, $variables);
    }
  }

  return $link;
}
@rooby
Copy link
Author

rooby commented Jul 5, 2016

Actually thinking out load, maybe my use of $theme_path = path_to_theme(); is the problem, and I should actually be using drupal_get_path('theme', 'THEMENAME') there as well.

I will try that out.

@fiasco
Copy link
Contributor

fiasco commented Jul 5, 2016

ACSF also allows sites to be cloned to be used in a site collection also which means you can't rely on the theme paths as the net1234 part will likely change. As you suggest, drupal_get_path is going to be a better direction. However, if the service links module had an auto discovery mechanism, that would be handy too.

@rooby
Copy link
Author

rooby commented Jul 5, 2016

I got my workaround theme override to work after I realised that Drupal will return the path to the theme on the Acquia environment, which I was then checking against the path in the UI, which didn't match.

This code works but is not ideal due to the hard coding.

/**
 * Implements theme_service_links_build_link().
 *
 * Output the markup for a single service link following the style rules.
 *
 * If the service links icons standard folder has been changed to point to
 * somewhere in this theme. rewrite the icon path using drupal_get_path() so
 * that GovCMS file paths work correctly.
 *
 * @param array $variables
 *   The variables passed in from the theme call and preprocess functions.
 *
 * @return string
 *  The markup for the service link.
 */
function THEMENAME_service_links_build_link($variables) {
  $text = $variables['text'];
  $url = $variables['url'];
  $image = $variables['image'];
  $nodelink = $variables['nodelink'];
  $style = $variables['style'];
  $attributes = $variables['attributes'];

  // If we have overridden the icon path with a path to this theme then
  // generate the path in a manner that will work with GovCMS.
  $gov_cms_icon_path = NULL;
  if ($icon_path_override = variable_get('service_links_path_icons', '')) {
    // The original theme path, before it was on the Acquia platform.
    $theme_path = 'sites/all/themes/THEMENAME';
    // The icon path cannot start with a slash so the path to theme will be
    // the first character.
    if (strpos($icon_path_override, $theme_path) === 0) {
      $gov_cms_icon_path = base_path() . drupal_get_path('theme', 'THEMENAME') . substr($icon_path_override, strlen($theme_path)) . '/' . $image;
    }
  }
  $icon_path = $gov_cms_icon_path ? $gov_cms_icon_path : service_links_expand_path($image);

  if ($nodelink) {
    $query = isset($url[1]) ? $url[1] : NULL;

    switch ($style) {
      case SERVICE_LINKS_STYLE_TEXT:
        $link = array(
          'title' => $text,
          'href' => $url[0],
          'query' => $query,
          'attributes' => $attributes,
        );
        break;
      case SERVICE_LINKS_STYLE_IMAGE:
        $alt_text = t('!name logo', array('!name' => $text));
        $link = array(
          'title' => theme('image', array('path' => $icon_path, 'alt' => $alt_text)),
          'href' => $url[0],
          'query' => $query,
          'attributes' => $attributes,
          'html' => TRUE,
        );
        break;
      case SERVICE_LINKS_STYLE_IMAGE_AND_TEXT:
        $alt_text = t('!name logo', array('!name' => $text));
        $link = array(
          'title' => theme('image', array('path' => $icon_path, 'alt' => $alt_text)) .' '. $text,
          'href' => $url[0],
          'query' => $query,
          'attributes' => $attributes,
          'html' => TRUE,
        );
        break;
      case SERVICE_LINKS_STYLE_EMPTY:
        $link = array(
          'title' => '<span class="element-invisible">' . $text . '</span>',
          'href' => $url[0],
          'query' => $query,
          'attributes' => $attributes,
          'html' => TRUE,
        );
        break;
      default:
        $link = theme($style, $variables);
    }
  }
  else {
    $attributes = array('attributes' => $attributes);
    if (isset($url[1])) {
      $attributes['query'] = $url[1];
    }

    switch ($style) {
      case SERVICE_LINKS_STYLE_TEXT:
        $link = l($text, $url[0], $attributes);
        break;
      case SERVICE_LINKS_STYLE_IMAGE:
        $attributes['html'] = TRUE;
        $alt_text = t('!name logo', array('!name' => $text));
        $link = l(theme('image', array('path' => $icon_path, 'alt' => $alt_text)), $url[0], $attributes);
        break;
      case SERVICE_LINKS_STYLE_IMAGE_AND_TEXT:
        $attributes['html'] = TRUE;
        $alt_text = t('!name logo', array('!name' => $text));
        $link = l(theme('image', array('path' => $icon_path, 'alt' => $alt_text)) .' '. $text, $url[0], $attributes);
        break;
      case SERVICE_LINKS_STYLE_EMPTY:
        $attributes['html'] = TRUE;
        $link = l('<span class="element-invisible">' . $text . '</span>', $url[0], $attributes);
        break;
      case SERVICE_LINKS_STYLE_FISHEYE:
        $attributes['attributes']['class'] = isset($attributes['attributes']['class']) ? array_merge($attributes['attributes']['class'], array('fisheyeItem')) : array('fisheyeItem');
        $attributes['html'] = TRUE;
        $link = l(theme('image', array('path' => service_links_expand_path($image, 'fisheye'), 'alt' => $text, 'getsize' => FALSE)) .'<span>'. $text .'</span>', $url[0], $attributes);
        break;
      default:
        $link = theme($style, $variables);
    }
  }

  return $link;
}

@rooby
Copy link
Author

rooby commented Jul 5, 2016

So I think the best solution is that the service links module add the ability to use tokens when entering that path, which are then replaced using drupal_get_path().

I have opened an issue for this, which I will write a patch for soon: https://www.drupal.org/node/2760649

@fiasco
Copy link
Contributor

fiasco commented Jul 6, 2016

Thanks @rooby - I agree with this approach too. The tokens you mentioned in the ticket look arbitrary while the Drupal community do prefer use of the token module. So expect the maintainer to want something slightly different (just a heads up).

@rooby
Copy link
Author

rooby commented Jul 6, 2016

Yeah I expect discussion around the tokens, although that format is surprisingly common in this particular context.

@fiasco
Copy link
Contributor

fiasco commented Aug 31, 2016

Closing off as there isn't anything further to discuss but please file a new ticket should upstream modules require updating to support this functionality.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants