View source
<?php
namespace Drupal\sharemessage\Plugin\sharemessage;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
use Drupal\sharemessage\SharePluginBase;
use Drupal\sharemessage\SharePluginInterface;
use Drupal\Core\Url;
class Sharrre extends SharePluginBase implements SharePluginInterface {
public static function checkConfiguration($show_message = FALSE) {
if (\Drupal::config('sharemessage.sharrre')
->get('library_url')) {
return NULL;
}
if (!\Drupal::moduleHandler()
->moduleExists('libraries') && !\Drupal::config('sharemessage.sharrre')
->get('library_url') && $show_message) {
$form['message'] = [
'#type' => 'container',
'#markup' => t('Either set the library locally (in /libraries/sharrre) and enable the libraries module or enter the remote URL on <a href=":sharrre_settings">Sharrre settings page</a>.', [
':sharrre_settings' => Url::fromRoute('sharemessage.sharrre.settings')
->toString(),
]),
'#attributes' => [
'class' => [
'messages messages--error',
],
],
];
return $form;
}
if (\Drupal::moduleHandler()
->moduleExists('libraries')) {
$directory = libraries_get_path('sharrre');
$file = 'jquery.sharrre.min.js';
if (!file_exists($directory . '/' . $file) && $show_message) {
$form['message'] = [
'#type' => 'container',
'#markup' => t('The library file is not present in the expected directory (/libraries/sharrre).'),
'#attributes' => [
'class' => [
'messages messages--error',
],
],
];
return $form;
}
}
}
public function build($context, $plugin_attributes) {
$attributes = new Attribute([
'id' => [
'sharemessage',
],
]);
if ($plugin_attributes) {
$attributes['sharrre:url'] = $this->shareMessage
->getUrl($context);
$attributes['sharrre:title'] = $this->shareMessage
->getTokenizedField($this->shareMessage->title, $context);
$attributes['sharrre:description'] = $this->shareMessage
->getTokenizedField($this->shareMessage->message_long, $context);
}
$build = [
'#theme' => 'sharemessage_sharrre',
'#attributes' => $attributes,
'#attached' => [
'library' => [
'sharemessage/sharrre',
],
'drupalSettings' => [
'sharrre_config' => [
'services' => $this->shareMessage
->getSetting('services') ?: \Drupal::config('sharemessage.sharrre')
->get('services'),
'library_url' => $this->shareMessage
->getSetting('library_url') ?: \Drupal::config('sharemessage.sharrre')
->get('library_url'),
'shorter_total' => $this->shareMessage
->getSetting('shorter_total') ?: \Drupal::config('sharemessage.sharrre')
->get('shorter_total'),
'enable_hover' => $this->shareMessage
->getSetting('enable_hover') ?: \Drupal::config('sharemessage.sharrre')
->get('enable_hover'),
'enable_counter' => $this->shareMessage
->getSetting('enable_counter') ?: \Drupal::config('sharemessage.sharrre')
->get('enable_counter'),
'enable_tracking' => $this->shareMessage
->getSetting('enable_tracking') ?: \Drupal::config('sharemessage.sharrre')
->get('enable_tracking'),
'url_curl' => Url::fromRoute('sharemessage.sharrre.counter')
->toString(),
'url' => $this->shareMessage->share_url,
],
],
],
];
$cacheability_metadata = CacheableMetadata::createFromObject(\Drupal::config('sharemessage.sharrre'));
$cacheability_metadata
->applyTo($build);
return $build;
}
function getSetting($key) {
$override = $this->shareMessage
->getSetting('override_default_settings');
if (isset($override)) {
return $this->shareMessage
->getSetting($key);
}
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
if ($check_form = static::checkConfiguration(TRUE)) {
return $check_form;
}
$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' => [
'googlePlus' => $this
->t('Google+'),
'facebook' => $this
->t('Facebook'),
'twitter' => $this
->t('Twitter'),
'digg' => $this
->t('Digg'),
'delicious' => $this
->t('Delicious'),
'stumbleupon' => $this
->t('StumpleUpon'),
'linkedin' => $this
->t('Linkedin'),
'pinterest' => $this
->t('Pinterest'),
],
'#default_value' => $this
->getSetting('services'),
'#size' => 10,
'#states' => [
'invisible' => [
':input[name="settings[override_default_settings]"]' => [
'checked' => FALSE,
],
],
],
];
$form['sharrre_website_documentation'] = [
'#type' => 'item',
'#title' => t('See the <a href=":url">Sharrre documentation</a> page for more information.', [
':url' => 'http://sharrre.com',
]),
'#states' => [
'invisible' => [
':input[name="settings[override_default_settings]"]' => [
'checked' => FALSE,
],
],
],
];
$form['shorter_total'] = [
'#type' => 'checkbox',
'#title' => t('Shorter total'),
'#description' => t('Format number like 1.2k or 5M.'),
'#default_value' => $this
->getSetting('shorter_total'),
'#states' => [
'invisible' => [
':input[name="settings[override_default_settings]"]' => [
'checked' => FALSE,
],
],
],
];
$form['enable_counter'] = [
'#type' => 'checkbox',
'#title' => t('Counter'),
'#description' => t('Enable the total counter.'),
'#default_value' => $this
->getSetting('enable_counter'),
'#states' => [
'invisible' => [
':input[name="settings[override_default_settings]"]' => [
'checked' => FALSE,
],
],
],
];
$form['enable_hover'] = [
'#type' => 'checkbox',
'#title' => t('Hover'),
'#description' => t('Allow displaying the sharing buttons when hovering over the counter.'),
'#default_value' => $this
->getSetting('enable_hover'),
'#states' => [
'invisible' => [
':input[name="settings[override_default_settings]"]' => [
'checked' => FALSE,
],
],
],
];
$form['enable_tracking'] = [
'#type' => 'checkbox',
'#title' => t('Tracking'),
'#description' => t('Allow tracking social interaction with Google Analytics.'),
'#default_value' => $this
->getSetting('enable_tracking'),
'#states' => [
'invisible' => [
':input[name="settings[override_default_settings]"]' => [
'checked' => FALSE,
],
],
],
];
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$sharrre_config = \Drupal::config('sharemessage.sharrre');
$library_exists = FALSE;
if (\Drupal::moduleHandler()
->moduleExists('libraries')) {
$directory = libraries_get_path('sharrre');
$file = 'jquery.sharrre.min.js';
if (file_exists($directory . '/' . $file)) {
$library_exists = TRUE;
}
}
if (!$library_exists && !$sharrre_config
->get('library_url')) {
$form_state
->setErrorByName('plugin', t('Either set the library locally (in /libraries/sharrre) and enable the libraries module or enter the remote URL on <a href=":sharrre_settings">Sharrre settings page</a>.', [
':sharrre_settings' => Url::fromRoute('sharemessage.sharrre.settings')
->toString(),
]));
}
}
}