Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPENEUROPA-1747: Add social sharing block. #46

Merged
merged 5 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: OpenEuropa Webtools Social Share
description: Provide social sharing functionality for a site.
package: OpenEuropa Webtools
type: module
version: 1.0
core: 8.x
15 changes: 15 additions & 0 deletions modules/oe_webtools_social_share/oe_webtools_social_share.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* @file
* OpenEuropa Webtools Social Share module.
*/

declare(strict_types = 1);

/**
* Implements hook_page_attachments().
*/
function oe_webtools_social_share_attachments(array &$attachments) {
$attachments['#attached']['library'][] = 'oe_webtools/drupal.webtools-smartloader';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to the block render array itself, so that it is included only when the block is rendered.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_webtools_social_share\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
* Provides a 'Social Share' Block.
*
* @Block(
* id = "social_share",
* admin_label = @Translation("Social Share"),
* category = @Translation("Webtools"),
* )
*/
class SocialShareBlock extends BlockBase {

/**
* {@inheritdoc}
*/
public function build() {
$social_share_json = [
'service' => 'share',
'popup' => FALSE,
'selection' => TRUE,
'to' => [
'more',
'twitter',
'facebook',
'linkedin',
'e-mail',
],
'stats' => TRUE,
];
return [
'script' => [
'#type' => 'html_tag',
'#tag' => 'script',
'#value' => json_encode($social_share_json),
'#attributes' => ['type' => 'application/json'],
],
];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types = 1);

namespace Drupal\Tests\oe_webtools\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\DomCrawler\Crawler;

/**
* Tests the Social Share widget provided by oe_webtools.
*/
class SocialShareBlockTest extends KernelTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'user',
'oe_webtools_social_share',
];

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('user');
$this->installSchema('system', 'sequences');
$this->installConfig(['system']);
}

/**
* Test social share block rendering.
*/
public function testSocialShareBlockRendering(): void {
// Setup and render social share block.
$config = [
'id' => 'social_share',
'label' => 'OpenEuropa social share',
'provider' => 'oe_webtools_social_share',
'label_display' => '0',
];
$plugin = $this->container->get('plugin.manager.block')->createInstance('social_share', $config);
$render = $plugin->build();
$html = (string) $this->container->get('renderer')->renderRoot($render);
$crawler = new Crawler($html);
// Make sure that social share block is present.
$actual = $crawler->filter('script');
$this->assertEquals('{"service":"share","popup":false,"selection":true,"to":["more","twitter","facebook","linkedin","e-mail"],"stats":true}', $actual->text());
}

}
1 change: 1 addition & 0 deletions runner.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ drupal:
- "./vendor/bin/drush en oe_webtools oe_webtools_laco_service -y"
- "./vendor/bin/drush en oe_webtools oe_webtools_laco_widget -y"
- "./vendor/bin/drush en oe_webtools oe_webtools_cookie_consent -y"
- "./vendor/bin/drush en oe_webtools oe_webtools_social_share -y"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- "./vendor/bin/drush en oe_webtools oe_webtools_social_share -y"
- "./vendor/bin/drush en oe_webtools_social_share -y"

It should be fixed in all lines except the first 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Let's put all our modules in one line

- "./vendor/bin/drush en language -y"
- "./vendor/bin/drush cr"
settings:
Expand Down