You are here

public function Addthis::build in Share Message 8

Creates the AddThis toolbar.

Parameters

array $context: The form structure.

$plugin_attributes: Custom plugin attributes.

Return value

array $build Returns the modified configuration form structure.

Overrides SharePluginInterface::build

File

src/Plugin/sharemessage/Addthis.php, line 25

Class

Addthis
AddThis plugin.

Namespace

Drupal\sharemessage\Plugin\sharemessage

Code

public function build($context, $plugin_attributes) {
  $attributes = new Attribute([
    'class' => [
      'addthis_toolbox',
      'addthis_default_style',
      $this->shareMessage
        ->getSetting('icon_style') ?: \Drupal::config('sharemessage.addthis')
        ->get('icon_style'),
    ],
  ]);
  if ($plugin_attributes) {
    $attributes['addthis:url'] = $this->shareMessage
      ->getUrl($context);
    $attributes['addthis:title'] = $this->shareMessage
      ->getTokenizedField($this->shareMessage->title, $context);
    $attributes['addthis:description'] = $this->shareMessage
      ->getTokenizedField($this->shareMessage->message_long, $context);
  }

  // Add AddThis buttons.
  $build = [
    '#theme' => 'sharemessage_addthis',
    '#attributes' => $attributes,
    '#services' => $this->shareMessage
      ->getSetting('services') ?: \Drupal::config('sharemessage.addthis')
      ->get('services'),
    '#additional_services' => $this
      ->getSetting('additional_services') ?: \Drupal::config('sharemessage.addthis')
      ->get('additional_services'),
    '#counter' => $this
      ->getSetting('counter') ?: \Drupal::config('sharemessage.addthis')
      ->get('counter'),
    '#twitter_template' => $this->shareMessage
      ->getTokenizedField($this->shareMessage->message_short, $context),
    '#attached' => [
      'library' => [
        'sharemessage/addthis',
      ],
      'drupalSettings' => [
        'addthis_config' => [
          'data_track_addressbar' => TRUE,
        ],
      ],
    ],
  ];
  $cacheability_metadata = CacheableMetadata::createFromObject(\Drupal::config('sharemessage.addthis'));
  $cacheability_metadata
    ->applyTo($build);
  return $build;
}