Skip to content

Commit

Permalink
Merge pull request #244 from openeuropa/EWPP-4547
Browse files Browse the repository at this point in the history
EWPP-4547: Add settings form for Social share widget to allow rendering icons without labels.
  • Loading branch information
upchuk authored Aug 21, 2024
2 parents 8d1814b + 6f10950 commit 7f83922
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
icons: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
oe_webtools_social_share.settings:
type: config_object
label: 'Webtools Social share settings'
mapping:
icons:
type: boolean
label: 'Icons'
description: 'Whether the Social share block displays only icons without labels.'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
oe_webtools_social_share.settings:
title: 'Webtools Social share'
description: 'Configure Webtools Social share block.'
route_name: oe_webtools_social_share.settings
parent: system.admin_config_system
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
administer webtools social share block:
title: 'Administer Webtools Social share block'
restrict access: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
oe_webtools_social_share.settings:
path: '/admin/config/system/oe_webtools_social_share'
defaults:
_form: 'Drupal\oe_webtools_social_share\Form\SocialShareSettingsForm'
_title: 'Webtools Social share settings'
requirements:
_permission: 'administer webtools social share block'
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Drupal\oe_webtools_social_share\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
* Provides configuration form for the Social share webtools widget.
*/
class SocialShareSettingsForm extends ConfigFormBase {

/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'oe_webtools_social_share_settings';
}

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames(): array {
return ['oe_webtools_social_share.settings'];
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$config = $this->config('oe_webtools_social_share.settings');

$form['icons'] = [
'#type' => 'checkbox',
'#title' => $this->t('Display only icons'),
'#description' => $this->t('Check this box if you would like to display only the icons without labels for the Social share block.'),
'#default_value' => $config->get('icons'),
];

return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->config('oe_webtools_social_share.settings')
->set('icons', $form_state->getValue('icons'))
->save();
parent::submitForm($form, $form_state);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use Drupal\Component\Serialization\Json;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\Markup;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides a 'Social Share' Block.
Expand All @@ -17,7 +20,43 @@
* category = @Translation("Webtools"),
* )
*/
class SocialShareBlock extends BlockBase {
class SocialShareBlock extends BlockBase implements ContainerFactoryPluginInterface {

/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* Creates a SocialShareBlock instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('config.factory')
);
}

/**
* {@inheritdoc}
Expand All @@ -30,6 +69,8 @@ public function defaultConfiguration() {
* {@inheritdoc}
*/
public function build() {
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config */
$config = $this->configFactory->get('oe_webtools_social_share.settings');
$social_share_json = [
'service' => 'share',
'version' => '2.0',
Expand All @@ -40,6 +81,7 @@ public function build() {
'email',
'more',
],
'display' => $config->get('icons') ? 'icons' : 'button',
'stats' => TRUE,
'selection' => TRUE,
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\oe_webtools_social_share\Functional;

use Drupal\Tests\BrowserTestBase;

/**
* Tests the Social share widget settings form.
*/
class SocialShareSettingsFormTest extends BrowserTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = [
'node',
'language',
'block',
'oe_webtools_social_share',
];

/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';

/**
* Tests Social share form configuration.
*/
public function testSocialShareForm(): void {
// Assert user without permissions cannot access the settings form.
$this->drupalLogin($this->createUser());
$this->drupalGet('/admin/config/system/oe_webtools_social_share');
$assert = $this->assertSession();
$assert->pageTextContains('Access denied');
$assert->pageTextContains('You are not authorized to access this page.');

// Create another user with access to the form.
$this->drupalLogin($this->createUser(['administer webtools social share block']));
$this->drupalGet('/admin/config/system/oe_webtools_social_share');
$assert->pageTextContainsOnce('Webtools Social share settings');
// The block renders the icons labels, by default.
$assert->checkboxNotChecked('Display only icons');
$assert->pageTextContainsOnce('Check this box if you would like to display only the icons without labels for the Social share block.');
$this->assertEquals(FALSE, $this->config('oe_webtools_social_share.settings')->get('icons'));

// Enable the icons option.
$this->getSession()->getPage()->checkField('Display only icons');
$this->getSession()->getPage()->pressButton('Save configuration');
$this->container->get('config.factory')->reset('oe_webtools_social_share.settings');
// Assert the values are saved.
$assert->pageTextContainsOnce('The configuration options have been saved.');
$assert->checkboxChecked('Display only icons');
$this->assertEquals(TRUE, $this->config('oe_webtools_social_share.settings')->get('icons'));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Drupal\Tests\oe_webtools\Kernel;
namespace Drupal\Tests\oe_webtools_social_share\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\DomCrawler\Crawler;
Expand Down Expand Up @@ -50,9 +50,18 @@ public function testSocialShareBlockRendering(): void {
$crawler = new Crawler($html);
// Make sure that social share block is present.
$actual = $crawler->filter('script');
$this->assertEquals('{"service":"share","version":"2.0","networks":["twitter","facebook","linkedin","email","more"],"stats":true,"selection":true}', $actual->text());
$this->assertEquals('{"service":"share","version":"2.0","networks":["twitter","facebook","linkedin","email","more"],"display":"button","stats":true,"selection":true}', $actual->text());
// Make sure "Share this page" heading is present.
$this->assertStringContainsString('Share this page', $html);

$social_share_settings = $this->config('oe_webtools_social_share.settings');
$social_share_settings->set('icons', TRUE)->save();
$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","version":"2.0","networks":["twitter","facebook","linkedin","email","more"],"display":"icons","stats":true,"selection":true}', $actual->text());
}

}

0 comments on commit 7f83922

Please sign in to comment.