Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions minimal_share.module
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ function minimal_share_theme() {
return [
'minimal_share' => [
'variables' => [
'title' => NULL,
'providers' => NULL,
],
],
'minimal_share_item' => [
'variables' => [
'title' => NULL,
'attributes' => NULL,
'provider_id' => NULL,
],
],
];
Expand Down
2 changes: 1 addition & 1 deletion minimal_share.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

66 changes: 63 additions & 3 deletions src/MinimalShareManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -275,6 +314,7 @@ public function buildProviderItem($provider, $info) {
'#theme' => 'minimal_share_item',
'#title' => $label,
'#attributes' => $attributes,
'#provider_id' => $provider['id'],
];

if (!empty($provider['size'])) {
Expand Down Expand Up @@ -372,10 +412,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;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Plugin/MinimalShareProvider/LinkedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down