You are here

function shariff_preprocess_block_shariff in Shariff Social Media Buttons 8

Implements hook_preprocess().

File

./shariff.module, line 69
Integrating Shariff library, providing settings form and block.

Code

function shariff_preprocess_block_shariff(&$variables, $hook) {

  // Get settings.
  $settings = _shariff_get_settings($variables['blocksettings']);

  // Build data attributes and escape settings.
  $data = [];
  foreach ($settings as $key => $setting) {
    if (!empty($setting)) {
      switch ($key) {
        case 'services':
          $value = '["' . implode('","', $setting) . '"]';
          break;
        case 'referrer_track':
          $value = urlencode(Html::escape($setting));
          break;
        case 'backend_url':
        case 'url':
        case 'mail_url':
        case 'media_url':
        case 'info_url':
          $value = Html::escape(UrlHelper::stripDangerousProtocols($setting));
          break;
        case 'twitter_via':
        case 'mail_subject':
        case 'mail_body':
        case 'flattr_user':
        case 'flattr_category':
        case 'info_display':
        case 'title':
        case 'button_style':
        case 'css':
        case 'orientation':
        default:
          $value = Html::escape($setting);
          break;
        case 'shariff_theme':
          $value = Html::escape($setting);
          $key = 'theme';
          break;
        case 'lang':
          $value = $setting;
          break;
      }

      // Shariff requires data-attributes with dashes instead of underscores.
      $data['data-' . str_replace('_', '-', $key)] = $value;
    }
  }

  // See https://www.drupal.org/node/1727592.
  $variables['data_attributes'] = new Attribute($data);

  // Load shariff library.
  $variant = isset($settings['css']) ? $settings['css'] : 'complete';
  $variables['#attached'] = [
    'library' => [
      'shariff/shariff-' . $variant,
    ],
  ];
}