Skip to content

Commit

Permalink
Merge pull request #53 from pjonckiere/issues/25
Browse files Browse the repository at this point in the history
Issues/25
  • Loading branch information
John Doyle committed May 24, 2016
2 parents 30256aa + 37628ed commit 60304d1
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 102 deletions.
5 changes: 0 additions & 5 deletions addthis.services.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
services:
# AddThis service?
addthis.addthis:
class: Drupal\addthis\AddThis
arguments: ['@addthis.script_manager', '@config.factory']

# This service creates a more complex access check.
addthis.script_manager:
class: Drupal\addthis\AddThisScriptManager
Expand Down
73 changes: 0 additions & 73 deletions src/AddThis.php

This file was deleted.

46 changes: 46 additions & 0 deletions src/AddThisScriptManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
namespace Drupal\addthis;


use Drupal\addthis\Util\AddThisJson;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Url;

class AddThisScriptManager {
Expand Down Expand Up @@ -128,6 +132,48 @@ protected function getServiceNamesAsCommaSeparatedString($services) {
return $servicesAsCommaSeparatedString;
}

/**
* Get an array containing the rendered AddThis services.
*
* @return array
* An array containing the rendered AddThis services.
*/
public function getServices() {
$rows = array();
$json = new AddThisJson();
$services = $json->decode($this->getServicesJsonUrl());
if (empty($services)) {
drupal_set_message(t('AddThis services could not be loaded from @service_url', array(
'@service_url',
$this->getServicesJsonUrl()
)), 'warning');
}
else {
foreach ($services['data'] as $service) {
$serviceCode = SafeMarkup::checkPlain($service['code']);
$serviceName = SafeMarkup::checkPlain($service['name']);
$service = array(
'#type' => 'inline_template',
'#template' => '<span class="addthis_service_icon icon_' . $serviceCode . '"></span> ' . $serviceName,
);
//#options expects a string, not an array. Render the element so it becomes a string.
$rows[$serviceCode] = render($service);
}
}
return $rows;
}

/**
* Gets the AddThis services url.
*
* @return string
*/
public function getServicesJsonUrl() {
$config = $this->config_factory->get('addthis.settings.advanced');
$service_json_url_key = $config->get('addthis_services_json_url');
$service_json_url_key = isset($service_json_url_key) ? $service_json_url_key : 'http://cache.addthiscdn.com/services/v1/sharing.en.json';
return Html::escape(UrlHelper::stripDangerousProtocols($service_json_url_key));
}

/**
* Attach the widget js to the element.
Expand Down
1 change: 0 additions & 1 deletion src/Form/AddThisSettingsAdvancedForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Component\Utility\Xss;
use Drupal\addthis\AddThis;

/**
* Defines a form to configure maintenance settings for this site.
Expand Down
28 changes: 7 additions & 21 deletions src/Form/AddThisSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Drupal\addthis\Form;

use Drupal\addthis\AddThisScriptManager;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Xss;
Expand All @@ -15,22 +17,6 @@
*/
class AddThisSettingsForm extends ConfigFormBase {

/**
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $config_factory;

/**
* Construct function.
*
* @param \Drupal\Core\Language\LanguageManager $languageManager
* @param \Drupal\Core\Config\ConfigFactory $configFactory
*/
public function __construct(\Drupal\Core\Config\ConfigFactory $configFactory) {
$this->config_factory = $configFactory;
}


/**
* {@inheritdoc}
*/
Expand All @@ -50,6 +36,8 @@ protected function getEditableConfigNames() {
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('addthis.settings');
$add_this_script_manager = \Drupal::service('addthis.script_manager');


//Add our library to the settings form to add in custom CSS.
$form['#attached']['library'][] = 'addthis/addthis.admin';
Expand Down Expand Up @@ -126,12 +114,10 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#open' => FALSE,
);

$add_this_service = \Drupal::service('addthis.addthis');

$form['compact_menu']['enabled_services']['addthis_enabled_services'] = array(
'#type' => 'checkboxes',
'#title' => t('Enabled services'),
'#options' => $add_this_service->getServices(),
'#options' => $add_this_script_manager->getServices(),
'#default_value' => $config->get('compact_menu.enabled_services.addthis_enabled_services'),
'#required' => FALSE,
'#columns' => 3,
Expand Down Expand Up @@ -176,7 +162,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$form['excluded_services']['addthis_excluded_services'] = array(
'#type' => 'checkboxes',
'#title' => t('Excluded services'),
'#options' => $add_this_service->getServices(),
'#options' => $add_this_script_manager->getServices(),
'#default_value' => $config->get('excluded_services.addthis_excluded_services'),
'#required' => FALSE,
'#columns' => 3,
Expand Down Expand Up @@ -251,7 +237,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$can_do_google_social_tracking = \Drupal::moduleHandler()
->moduleExists('google_analytics');
//@TODO Get back to this.
$google_analytics_config = $this->config_factory->get('google_analytics.settings');
$google_analytics_config = $this->configFactory()->get('google_analytics.settings');
$google_analytics_account = $google_analytics_config->get('google_analytics_account');
$is_google_analytics_setup = $can_do_google_social_tracking && isset($google_analytics_account);
$form['analytics']['google_analytics'] = array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\addthis\AddThis;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\addthis\AddThis;

/**
* Plugin implementation of the 'addthis_basic_toolbox' formatter.
Expand Down
3 changes: 3 additions & 0 deletions src/Tests/AddThisBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

use Drupal\simpletest\WebTestBase;

/**
* Tests the add this functionality.
*/
class AddThisBaseTest extends WebTestBase {

/**
Expand Down

0 comments on commit 60304d1

Please sign in to comment.