From 3b8f388cbc7f923f733ad3dde80f038f17962b36 Mon Sep 17 00:00:00 2001 From: recrit Date: Fri, 5 May 2017 12:26:56 -0400 Subject: [PATCH 1/2] Adds entity summary support and url encoding of replacements --- minimal_share.services.yml | 2 +- src/MinimalShareManager.php | 65 +++++++++++++++++++- src/Plugin/MinimalShareProvider/LinkedIn.php | 4 +- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/minimal_share.services.yml b/minimal_share.services.yml index 663fb31..2edb023 100644 --- a/minimal_share.services.yml +++ b/minimal_share.services.yml @@ -4,5 +4,5 @@ services: parent: default_plugin_manager minimal_share.manager: class: Drupal\minimal_share\MinimalShareManager - arguments: ["@config.factory", "@plugin.manager.minimal_share_provider.processor"] + arguments: ["@config.factory", "@plugin.manager.minimal_share_provider.processor", "@token"] diff --git a/src/MinimalShareManager.php b/src/MinimalShareManager.php index a297821..5d48e92 100644 --- a/src/MinimalShareManager.php +++ b/src/MinimalShareManager.php @@ -10,6 +10,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Template\Attribute; use Drupal\Core\Url; +use Drupal\Core\Utility\Token; use Drupal\minimal_share\Plugin\MinimalShareProviderInterface; use Drupal\minimal_share\Plugin\MinimalShareProviderManager; @@ -30,15 +31,22 @@ class MinimalShareManager { */ protected $providerManager; + /** + * @var Token + */ + protected $tokenService; + /** * MinimalShareManager constructor. * * @param \Drupal\Core\Config\ConfigFactory $config_factory * @param \Drupal\minimal_share\Plugin\MinimalShareProviderManager $provider_manager + * @param \Drupal\Core\Utility\Token $token_service */ - public function __construct(ConfigFactory $config_factory, MinimalShareProviderManager $provider_manager) { + public function __construct(ConfigFactory $config_factory, MinimalShareProviderManager $provider_manager, Token $token_service) { $this->config = $config_factory->get('minimal_share.config')->get(); $this->providerManager = $provider_manager; + $this->tokenService = $token_service; } /** @@ -224,9 +232,40 @@ public function buildShareInfo(EntityInterface $entity) { return [ 'url' => $entity->toUrl('canonical', ['absolute' => TRUE])->toString(), 'title' => $entity->label(), + 'summary' => $this->buildShareSummary($entity), ]; } + /** + * Build a summary for the given entity. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * + * @return string + */ + public function buildShareSummary(EntityInterface $entity) { + $entity_type = $entity->getEntityTypeId(); + $token_name = NULL; + $token_type = NULL; + if ($entity_type === 'node') { + $token_type = 'node'; + $token_name = 'summary'; + } + elseif ($entity_type === 'taxonomy_term') { + $token_type = 'term'; + $token_name = 'description'; + } + if (isset($token_name) && isset($token_type)) { + $summary_token = '[' . $token_type . ':' . $token_name . ']'; + $summary = $this->tokenService->replace($summary_token, [ + $token_type => $entity, + ]); + if ($summary !== $summary_token) { + return strip_tags($summary); + } + } + } + /** * Build a Minimal Share widget render array. * @@ -372,10 +411,30 @@ public function buildShareUrl($provider, $info) { } $share_url = $provider['url']; - $title = $info['title']; $url = $info['url']; - $url = str_replace(['[url]', '[title]'], [$url, $title], $share_url); + // Title. + $title = ''; + if (!empty($info['title'])) { + if (!empty($provider['title_max_length'])) { + $title = substr($info['title'], 0, $provider['title_max_length']); + } + $title = rawurlencode($title); + } + + // Summary. + $summary = $title; + if (!empty($info['summary'])) { + $summary_max_length = !empty($provider['summary_max_length']) ? $provider['summary_max_length'] : 256; + $summary = rawurlencode(substr($info['summary'], 0, $summary_max_length)); + } + + // Replace tokens. + $url = str_replace( + ['[url]', '[title]', '[summary]'], + [$url, $title, $summary], + $share_url + ); return $url; } diff --git a/src/Plugin/MinimalShareProvider/LinkedIn.php b/src/Plugin/MinimalShareProvider/LinkedIn.php index 1f1a63e..0904a5b 100644 --- a/src/Plugin/MinimalShareProvider/LinkedIn.php +++ b/src/Plugin/MinimalShareProvider/LinkedIn.php @@ -10,7 +10,9 @@ * @MinimalShareProvider( * id = "linkedin", * label = @Translation("LinkedIn"), - * url = "http://www.linkedin.com/shareArticle?mini=true&url=[url]&title=[title]", + * url = "http://www.linkedin.com/shareArticle?mini=true&url=[url]&title=[title]&summary=[summary]", + * title_max_length = 200, + * summary_max_length = 256, * color = "#007bb6", * size = { * "width" = 520, From 3ec387f769dac1c435b7228cbb5d71cf65da6f81 Mon Sep 17 00:00:00 2001 From: recrit Date: Fri, 5 May 2017 12:27:20 -0400 Subject: [PATCH 2/2] Adds some useful theme template variables. --- minimal_share.module | 2 ++ src/MinimalShareManager.php | 1 + 2 files changed, 3 insertions(+) diff --git a/minimal_share.module b/minimal_share.module index 94b4612..ed780ca 100644 --- a/minimal_share.module +++ b/minimal_share.module @@ -15,6 +15,7 @@ function minimal_share_theme() { return [ 'minimal_share' => [ 'variables' => [ + 'title' => NULL, 'providers' => NULL, ], ], @@ -22,6 +23,7 @@ function minimal_share_theme() { 'variables' => [ 'title' => NULL, 'attributes' => NULL, + 'provider_id' => NULL, ], ], ]; diff --git a/src/MinimalShareManager.php b/src/MinimalShareManager.php index 5d48e92..32da522 100644 --- a/src/MinimalShareManager.php +++ b/src/MinimalShareManager.php @@ -314,6 +314,7 @@ public function buildProviderItem($provider, $info) { '#theme' => 'minimal_share_item', '#title' => $label, '#attributes' => $attributes, + '#provider_id' => $provider['id'], ]; if (!empty($provider['size'])) {