You are here

function rrssb_get_buttons in Ridiculously Responsive Social Sharing Buttons 8.2

Same name and namespace in other branches
  1. 7.2 rrssb.module \rrssb_get_buttons()
  2. 7 rrssb.module \rrssb_get_buttons()

Returns a Drupal render array for the buttons.

Parameters

object $node: The node object to build buttons for.

string $context: Additional cache context to set if applicable.

Return value

array A render array for the list of buttons.

3 calls to rrssb_get_buttons()
RRSSBBlock::build in src/Plugin/Block/RRSSBBlock.php
Builds and returns the renderable array for this block plugin.
RRSSBField::render in src/Plugin/views/field/RRSSBField.php
Renders the field.
rrssb_node_view in ./rrssb.module
Implements hook_ENTITY_TYPE_view().

File

./rrssb.module, line 144

Code

function rrssb_get_buttons($buttonSet, $node, $context = NULL) {
  $config = \Drupal::config("rrssb.button_set.{$buttonSet}");
  if (!$config) {
    return [];
  }
  $token_service = \Drupal::service('token');
  $follow = $config
    ->get('follow');
  $meta = BubbleableMetadata::createFromObject($config);
  if (!$follow) {

    // Share buttons need to pick up attributes from the node/page using tokens.
    // Skip this for follow buttons which are the same throughout the site.
    if ($context) {
      $meta
        ->setCacheContexts([
        $context,
      ]);
    }

    // Create an array for how we will map [rrssb:XXX] tokens.  The key is the
    // XXX value and the value is an array of other tokens to try in turn until
    // one works.  For the image token, we allow the user to configure the list
    // of tokens.
    $image_tokens = explode('|', $config
      ->get('image_tokens') ?: RRSSB_DEFAULT_IMAGE_TOKEN);
    $mapping = [
      'url' => [
        '[node:url]',
        '[current-page:url]',
      ],
      'title' => [
        '[node:title]',
        '[current-page:title]',
      ],
      'image' => $image_tokens,
    ];

    // Replace tokens.
    foreach ($mapping as $param => $tokens) {
      foreach ($tokens as $token) {
        $rrssb[$param] = $token_service
          ->replace($token, [
          'node' => $node,
        ], [
          'clear' => TRUE,
          'callback' => '_rrssb_decode',
        ], $meta);
        if ($rrssb[$param]) {
          break;
        }
      }
    }

    // If the image returned a comma separated list, just take the first entry.
    list($rrssb['image']) = explode(',', $rrssb['image']);
  }

  // Create list of buttons.
  $key = $follow ? 'follow_url' : 'share_url';
  $buttons = [];
  foreach (rrssb_settings($buttonSet) as $name => $button) {
    $rrssb['username'] = $button['username'];

    // No need to pass in $meta again as it already contains the correct value from above.
    $link = $token_service
      ->replace($button[$key], [
      'rrssb' => $rrssb,
    ], [
      'urlencode' => TRUE,
    ]);
    $class = $button['popup'] ? 'class="popup"' : '';
    $buttons[] = [
      'svg' => $button['svg'],
      'text' => $token_service
        ->replace($button['text'], [
        'rrssb' => $rrssb,
      ]),
      'link' => $link,
      'name' => $name,
      'class' => $class,
    ];
  }
  $items = [
    '#theme' => 'rrssb_button_list',
    '#prefix_text' => $config
      ->get('prefix'),
    '#button_set' => $buttonSet,
    '#buttons' => $buttons,
  ];
  $meta
    ->applyTo($items);

  // Attach library, first making sure the auto-generated CSS file exists.
  if (!\Drupal::state()
    ->get('rrssb_css_file')) {
    rrssb_gen_css();
  }
  $items['#attached']['library'][] = 'rrssb/init';
  $items['#attached']['drupalSettings']['rrssb'][$buttonSet] = $config
    ->get('appearance');
  return $items;
}