class SocialSharePrivacy in Share Message 8
SocialSharePrivacy plugin.
Plugin annotation
@SharePlugin(
id = "socialshareprivacy",
label = @Translation("Social Share Privacy"),
description = @Translation("Social Share Privacy is a jQuery plugin that lets you add social share buttons to your website that don't allow the social sites to track your users."),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\sharemessage\SharePluginBase implements SharePluginInterface uses StringTranslationTrait
- class \Drupal\sharemessage\Plugin\sharemessage\SocialSharePrivacy implements ContainerFactoryPluginInterface, SharePluginInterface
- class \Drupal\sharemessage\SharePluginBase implements SharePluginInterface uses StringTranslationTrait
Expanded class hierarchy of SocialSharePrivacy
1 file declares its use of SocialSharePrivacy
File
- src/
Plugin/ sharemessage/ SocialSharePrivacy.php, line 23
Namespace
Drupal\sharemessage\Plugin\sharemessageView source
class SocialSharePrivacy extends SharePluginBase implements SharePluginInterface, ContainerFactoryPluginInterface {
/**
* Social Share Privacy config.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $socialSharePrivacyConfig;
/**
* Constructs Social Share Privacy plugin.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ImmutableConfig $config
* An immutable configuration object.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ImmutableConfig $config) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->socialSharePrivacyConfig = $config;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory')
->get('sharemessage.socialshareprivacy'));
}
/**
* {@inheritdoc}
*/
public function build($context, $plugin_attributes) {
$attributes = new Attribute([
'class' => [
'socialshareprivacy',
],
]);
if ($plugin_attributes) {
$attributes['socialshareprivacy:url'] = $this->shareMessage
->getUrl($context);
$attributes['socialshareprivacy:title'] = $this->shareMessage
->getTokenizedField($this->shareMessage->title, $context);
$attributes['socialshareprivacy:description'] = $this->shareMessage
->getTokenizedField($this->shareMessage->message_long, $context);
}
// Add Social Share Privacy buttons.
$build = [
'#theme' => 'sharemessage_socialshareprivacy',
'#attributes' => $attributes,
'#attached' => [
'library' => [
'sharemessage/socialshareprivacy',
],
'drupalSettings' => [
'socialshareprivacy_config' => [
'services' => $this
->services(),
'url' => $this->shareMessage->share_url,
],
],
],
];
$cacheability_metadata = CacheableMetadata::createFromObject($this->socialSharePrivacyConfig);
$cacheability_metadata
->applyTo($build);
return $build;
}
/**
* {@inheritdoc}
*/
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('Default visible services'),
'#type' => 'select',
'#multiple' => TRUE,
'#options' => static::allServices(),
'#default_value' => $this
->getSetting('services'),
'#size' => 11,
'#states' => [
'visible' => [
':input[name="settings[override_default_settings]"]' => [
'checked' => TRUE,
],
],
],
];
$form['facebook_action'] = [
'#title' => $this
->t('Choose facebook action'),
'#type' => 'radios',
'#default_value' => $this
->getSetting('facebook_action') ?: 'like',
'#options' => [
'like' => $this
->t('Like'),
'recommend' => $this
->t('Recommend'),
],
'#states' => [
'visible' => [
':input[name="settings[override_default_settings]"]' => [
'checked' => TRUE,
],
],
],
];
$form['disqus_shortname'] = [
'#title' => $this
->t('Disqus shortname'),
'#type' => 'textfield',
'#description' => $this
->t('You can get shortname from <a href=":url">Disqus</a>.', [
':url' => 'https://disqus.com/',
]),
'#default_value' => $this
->getSetting('disqus_shortname'),
'#states' => [
'visible' => [
':input[name="settings[override_default_settings]"]' => [
'checked' => TRUE,
],
],
],
];
$form['flattr_uid'] = [
'#title' => $this
->t('Flattr user id'),
'#type' => 'textfield',
'#description' => $this
->t('You can get user id from <a href=":url">Flattr</a>.', [
':url' => 'https://flattr.com/',
]),
'#default_value' => $this
->getSetting('flattr_uid'),
'#states' => [
'visible' => [
':input[name="settings[override_default_settings]"]' => [
'checked' => TRUE,
],
],
],
];
return $form;
}
/**
* Gets all Social Share Privacy services.
*
* @return array
* All supported Social Share Privacy services.
*/
public static function allServices() {
return [
'buffer' => t('Buffer'),
'delicious' => t('Delicious'),
'disqus' => t('Disqus'),
'facebook' => t('Facebook Like/Recommend'),
'fbshare' => t('Facebook Share'),
'flattr' => t('Flattr'),
'gplus' => t('Google+'),
'hackernews' => t('Hacker News'),
'linkedin' => t('LinkedIn'),
'mail' => t('Mail'),
'pinterest' => t('Pinterest'),
'reddit' => t('Reddit'),
'stumbleupon' => t('Stumble Upon'),
'tumblr' => t('Tumblr'),
'twitter' => t('Twitter'),
'xing' => t('XING'),
];
}
/**
* Social Share Privacy services with settings.
*
* @return array
* Social Share Privacy services with settings.
*/
public function servicesWithSettings() {
return [
'facebook' => [
'action' => $this->shareMessage
->getSetting('facebook_action') ?: $this->socialSharePrivacyConfig
->get('facebook_action'),
],
'flattr' => [
'uid' => $this->shareMessage
->getSetting('flattr_uid') ?: $this->socialSharePrivacyConfig
->get('flattr_uid'),
],
'disqus' => [
'shortname' => $this->shareMessage
->getSetting('disqus_shortname') ?: $this->socialSharePrivacyConfig
->get('disqus_shortname'),
],
];
}
/**
* Prepare services for drupalSettings.
*
* @return array
* Prepared services array.
*/
public function services() {
$all_services = [];
foreach (array_keys(static::allServices()) as $service) {
$all_services[$service]['status'] = FALSE;
}
$enabled_services = [];
$library_discovery = \Drupal::service('library.discovery');
$library = $library_discovery
->getLibraryByName('sharemessage', 'socialshareprivacy');
if (!empty($library['library path'])) {
$images_folder = $library['library path'] . '/images/';
}
else {
$images_folder = 'libraries/socialshareprivacy/images/';
}
$services = $this->shareMessage
->getSetting('services') ?: $this->socialSharePrivacyConfig
->get('services');
foreach ($services as $service) {
$enabled_services[$service]['status'] = TRUE;
if (in_array($service, array_keys($this
->servicesWithSettings()))) {
$enabled_services[$service] = array_merge($enabled_services[$service], $this
->servicesWithSettings()[$service]);
}
if (in_array($service, [
'mail',
'tumblr',
'fbshare',
])) {
$enabled_services[$service]['line_img'] = file_create_url($images_folder . $service . '.png');
}
else {
$enabled_services[$service]['dummy_line_img'] = file_create_url($images_folder . 'dummy_' . $service . '.png');
}
}
return array_replace($all_services, $enabled_services);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
SharePluginBase:: |
protected | property | Share Message. | |
SharePluginBase:: |
public | function | ||
SharePluginBase:: |
public | function |
Provides the action link plugin's default configuration. Overrides ConfigurableInterface:: |
|
SharePluginBase:: |
public | function |
Provides the action link plugin's current configuration array. Overrides ConfigurableInterface:: |
|
SharePluginBase:: |
public | function |
Updates the plugin's current configuration. Overrides ConfigurableInterface:: |
|
SharePluginBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
SharePluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
1 |
SocialSharePrivacy:: |
protected | property | Social Share Privacy config. | |
SocialSharePrivacy:: |
public static | function | Gets all Social Share Privacy services. | |
SocialSharePrivacy:: |
public | function |
Creates the AddThis toolbar. Overrides SharePluginInterface:: |
|
SocialSharePrivacy:: |
public | function |
Form constructor. Overrides SharePluginBase:: |
|
SocialSharePrivacy:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
SocialSharePrivacy:: |
public | function |
Gets the setting for $key with overrides if applicable. Overrides SharePluginInterface:: |
|
SocialSharePrivacy:: |
public | function | Prepare services for drupalSettings. | |
SocialSharePrivacy:: |
public | function | Social Share Privacy services with settings. | |
SocialSharePrivacy:: |
public | function |
Constructs Social Share Privacy plugin. Overrides SharePluginBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |