Commit 5f3fc5b6 authored by Ruben Marques's avatar Ruben Marques Committed by Ruben Marques
Browse files

Issue #3121967 by rutiolma: Social icons should be rendered as inline SVG

parent 9ed4b51a
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ class OpenfedSocialAdminForm extends FormBase {
      ->get('openfed_social_networks');
    $networks_enabled = \Drupal::config('openfed_social.settings')
      ->get('openfed_social_networks_enabled');
    $rendering = \Drupal::config('openfed_social.settings')
      ->get('openfed_social_theming');
    $options = $enabled = [];
    $counter = 0;
    $form['weight'] = ['#tree' => TRUE];
@@ -66,6 +68,23 @@ class OpenfedSocialAdminForm extends FormBase {
      '#default_value' => $enabled,
    ];

    $form['redering'] = [
      '#title' => t('Openfed Social SVG rendering type'),
      '#type' => 'radios',
      '#options' => [
        'image' => t('Image tag'),
        'inline' => t('Inline svg'),
      ],
      '#default_value' => $rendering ?: 'image',
    ];
    $moduleHandler = \Drupal::service('module_handler');
    if (!$moduleHandler->moduleExists('openfed_svg_file')) {
      // Rendering choice will be used only if openfed_svg_file is enabled. This
      // module is part of Openfed distribution.
      $form['redering']['#disabled'] = 'true';
      $form['redering']['#default_value'] = 'image';
    }

    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['submit'] = [
      '#type' => 'submit',
@@ -101,6 +120,10 @@ class OpenfedSocialAdminForm extends FormBase {
      ->getEditable('openfed_social.settings')
      ->set('openfed_social_networks', $networks)
      ->save();
    \Drupal::configFactory()
      ->getEditable('openfed_social.settings')
      ->set('openfed_social_theming', $submissions['redering'])
      ->save();
    $this->messenger()->addStatus(t('Configuration saved.'));
    return;
  }
+50 −8
Original line number Diff line number Diff line
@@ -3,7 +3,10 @@
namespace Drupal\openfed_social\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Core\TypedData\ListDataDefinition;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;

/**
 * This will create a Openfed Social Block where social network links will be
@@ -68,19 +71,58 @@ class OpenfedSocialBlock extends BlockBase {
    $path = drupal_get_path('module', 'openfed_social');
    $networks = \Drupal::config('openfed_social.settings')
      ->get('openfed_social_networks');
    $rendering = \Drupal::config('openfed_social.settings')
      ->get('openfed_social_theming');
    $sharelinks = [];
    // Creating 2 arrays, one with simple links and one with link and icon. Simple
    // links will be used for ShareThis theme support, icons and links will be
    // used on default theme.
    foreach ($networks_enabled as $network_key => $network) {

      $width = $height= 30;
      $alt = $networks[$network_key]['share_label'];
      $class = 'openfed_social_buttons';

      if ($rendering == 'inline') {
        $attributes['width'] = $width;
        $attributes['height'] = $height;
        $attributes['alt'] = $alt;
        $attributes['title'] = '';

        $uri = $path . '/assets/images/' . $network_key . '.svg';

        // We need the following for inline svgs.
        $svg_data = NULL;
        $svg_file = file_exists($uri) ? file_get_contents($uri) : NULL;
        if ($svg_file) {
          $dom = new \DomDocument();
          libxml_use_internal_errors(TRUE);
          $dom->loadXML($svg_file);
          if (isset($dom->documentElement)) {
            $dom->documentElement->setAttribute('height', $attributes['height']);
            $dom->documentElement->setAttribute('width', $attributes['width']);
          }
          $svg_data = $dom->C14N();
        }

        $icon = [
          '#theme' => 'openfed_svg_file__' . $rendering,
          '#attributes' => $attributes,
          '#alt_text' => NULL,
          '#svg_data' => $svg_data,
          '#uri' => $uri,
        ];
      } else {
        $icon = [
          '#theme' => 'image',
          '#uri' => $path . '/assets/images/' . $network_key . '.svg',
        '#alt' => $networks[$network_key]['share_label'],
        '#width' => '30',
        '#height' => '30',
        '#attributes' => ['class' => 'openfed_social_buttons'],
          '#alt' => $alt,
          '#width' => $width,
          '#height' => $height,
          '#attributes' => ['class' => $class],
        ];
      }

      // Special case for email. We should add a target attribute.
      $target = ($network_key == 'email') ? [] : ['target' => '_blank'];
      // Special case for print. URL won't be valid so'll add a tag.