You are here

class Sharrre in Share Message 8

Sharrre plugin.

Plugin annotation


@SharePlugin(
  id = "sharrre",
  label = @Translation("Sharrre"),
  description = @Translation("Sharrre is a jQuery plugin that allows you to create nice widgets sharing for Facebook, Twitter, Google Plus (with PHP script) and more. (http://sharrre.com)"),
)

Hierarchy

Expanded class hierarchy of Sharrre

1 file declares its use of Sharrre
SharrreSettingsForm.php in src/Form/SharrreSettingsForm.php
2 string references to 'Sharrre'
ShareMessageSettingsTest::testShareMessageSettings in tests/src/Functional/ShareMessageSettingsTest.php
Tests if default and Share Message specific settings work correctly.
sharemessage_libraries_info in ./sharemessage.module
Implements hook_libraries_info().

File

src/Plugin/sharemessage/Sharrre.php, line 21

Namespace

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

  /**
   * Check if the plugin can work.
   *
   * The library needs to be either external or we need the local library module with the library present.
   *
   * @param $show_message
   *   A flag that determines if the warning should be shown or not.
   *
   * @return mixed
   */
  public static function checkConfiguration($show_message = FALSE) {
    if (\Drupal::config('sharemessage.sharrre')
      ->get('library_url')) {

      // We have an external library URL configured. Fine.
      return NULL;
    }

    // Check for both, local library and remote URL.
    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')) {

      // Check if local library is set correctly.
      $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;
      }
    }
  }

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

    // Add Sharrre buttons.
    $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;
  }

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

    // Check if the library configuration is valid.
    if ($check_form = static::checkConfiguration(TRUE)) {
      return $check_form;
    }

    // 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' => [
        '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;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $sharrre_config = \Drupal::config('sharemessage.sharrre');
    $library_exists = FALSE;

    // Check if the library module is enabled and if the library is present.
    if (\Drupal::moduleHandler()
      ->moduleExists('libraries')) {
      $directory = libraries_get_path('sharrre');
      $file = 'jquery.sharrre.min.js';
      if (file_exists($directory . '/' . $file)) {
        $library_exists = TRUE;
      }
    }

    // General error if neither the library nor the URL from configuration exist.
    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(),
      ]));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
Sharrre::build public function Creates the AddThis toolbar. Overrides SharePluginInterface::build
Sharrre::buildConfigurationForm public function Form constructor. Overrides SharePluginBase::buildConfigurationForm
Sharrre::checkConfiguration public static function Check if the plugin can work.
Sharrre::getSetting function Gets the setting for $key with overrides if applicable. Overrides SharePluginInterface::getSetting
Sharrre::validateConfigurationForm public function Form validation handler. Overrides SharePluginBase::validateConfigurationForm
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.