You are here

class Addthis in Share Message 8

AddThis plugin.

Plugin annotation


@SharePlugin(
  id = "addthis",
  label = @Translation("AddThis"),
  description = @Translation("AddThis plugin for Share Message module."),
)

Hierarchy

Expanded class hierarchy of Addthis

File

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

Namespace

Drupal\sharemessage\Plugin\sharemessage
View source
class Addthis extends SharePluginBase implements SharePluginInterface {

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Gets the default AddThis settings.
   *
   * @param string $key
   *   The settings key.
   *
   * @return array
   */
  public function getSetting($key) {
    $override = $this->shareMessage
      ->getSetting('override_default_settings');
    if (isset($override)) {
      return $this->shareMessage
        ->getSetting($key);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

    // Settings fieldset.
    $form['override_default_settings'] = [
      '#type' => 'checkbox',
      '#title' => t('Override default settings'),
      '#default_value' => $this
        ->getSetting('override_default_settings'),
    ];
    $form['services'] = [
      '#title' => t('Visible services'),
      '#type' => 'select',
      '#multiple' => TRUE,
      '#options' => sharemessage_get_addthis_services(),
      '#default_value' => $this
        ->getSetting('services'),
      '#size' => 10,
      '#states' => [
        'invisible' => [
          ':input[name="settings[override_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['additional_services'] = [
      '#type' => 'checkbox',
      '#title' => t('Show additional services button'),
      '#default_value' => $this
        ->getSetting('additional_services'),
      '#states' => [
        'invisible' => [
          ':input[name="settings[override_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['counter'] = [
      '#type' => 'select',
      '#title' => t('Show AddThis counter'),
      '#empty_option' => t('No'),
      '#options' => [
        'addthis_pill_style' => t('Pill style'),
        'addthis_bubble_style' => t('Bubble style'),
      ],
      '#default_value' => $this
        ->getSetting('counter'),
      '#states' => [
        'invisible' => [
          ':input[name="settings[override_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['icon_style'] = [
      '#title' => t('Default icon style'),
      '#type' => 'radios',
      '#options' => [
        'addthis_16x16_style' => '16x16 pix',
        'addthis_32x32_style' => '32x32 pix',
      ],
      '#default_value' => $this
        ->getSetting('icon_style'),
      '#states' => [
        'invisible' => [
          ':input[name="settings[override_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Addthis::build public function Creates the AddThis toolbar. Overrides SharePluginInterface::build
Addthis::buildConfigurationForm public function Form constructor. Overrides SharePluginBase::buildConfigurationForm
Addthis::getSetting public function Gets the default AddThis settings. Overrides SharePluginInterface::getSetting
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
SharePluginBase::$shareMessage protected property Share Message.
SharePluginBase::calculateDependencies public function
SharePluginBase::defaultConfiguration public function Provides the action link plugin's default configuration. Overrides ConfigurableInterface::defaultConfiguration
SharePluginBase::getConfiguration public function Provides the action link plugin's current configuration array. Overrides ConfigurableInterface::getConfiguration
SharePluginBase::setConfiguration public function Updates the plugin's current configuration. Overrides ConfigurableInterface::setConfiguration
SharePluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
SharePluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1
SharePluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.