Skip to content

Commit 77ed125

Browse files
committed
feature symfony#54737 [Notifier] [Slack] Add button block element and emoji/verbatim options to section block (cvergne)
This PR was squashed before being merged into the 7.2 branch. Discussion ---------- [Notifier] [Slack] Add button block element and `emoji`/`verbatim` options to section block | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | - | License | MIT Hi, When using the Slack Notifier bridge, I've noticed it wasn't possible to add a button as accessory to a section because button is only managed in the SlackActionsBlock, which is not allowed as an accessory in a Section block. So the first purpose of this PR is add a SlackButtonBlockElement we can use as `accessory` into a section block (and use that block into SlackActionsBlock as it follows the same structure). Then, I noticed `verbatim` (for markdown) and `emoji` (for plain text) options were not available in SectionBlock (but available in ContextBlock), so I've added them. Note that originally, I was going to add them as optional arguments (`?bool $verbatim = null`) to avoid adding them if not explicitly given and keep the Slack default value, but the ContextBlock adds a default value (the same as Slack) for them, so I kept the same argument signature. You can see an example of both use of button as accessory and verbatim param with the following link on [Slack Block Kit Builder](https://app.slack.com/block-kit-builder/#%7B%22blocks%22:%5B%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22mrkdwn%22,%22text%22:%22Example%20with%20URL%20:%20Symfony.com%22,%22verbatim%22:true%7D,%22accessory%22:%7B%22type%22:%22button%22,%22text%22:%7B%22type%22:%22plain_text%22,%22emoji%22:true,%22text%22:%22Button%20as%20accessory%22%7D,%22value%22:%22https://symfony.com%22%7D%7D,%7B%22type%22:%22context%22,%22elements%22:%5B%7B%22type%22:%22mrkdwn%22,%22text%22:%22from%20*Symfony.com*%22,%22verbatim%22:true%7D%5D%7D%5D%7D) (_must be logged in to a Slack Account_). _These minor additions are in the same PR, but tell me if I should split them into two PRs._ Thanks :) Commits ------- e9b7954 [Notifier] [Slack] Add button block element and `emoji`/`verbatim` options to section block
2 parents e8bd4f1 + e9b7954 commit 77ed125

File tree

6 files changed

+79
-17
lines changed

6 files changed

+79
-17
lines changed

src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackActionsBlock.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,9 @@ public function button(string $text, string $url, ?string $style = null): static
3030
throw new \LogicException('Maximum number of buttons should not exceed 25.');
3131
}
3232

33-
$element = [
34-
'type' => 'button',
35-
'text' => [
36-
'type' => 'plain_text',
37-
'text' => $text,
38-
],
39-
'url' => $url,
40-
];
33+
$element = new SlackButtonBlockElement($text, $url, $style);
4134

42-
if ($style) {
43-
// primary or danger
44-
$element['style'] = $style;
45-
}
46-
47-
$this->options['elements'][] = $element;
35+
$this->options['elements'][] = $element->toArray();
4836

4937
return $this;
5038
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Slack\Block;
13+
14+
/**
15+
* @author Christophe Vergne <christophe.vergne@gmail.com>
16+
*/
17+
final class SlackButtonBlockElement extends AbstractSlackBlockElement
18+
{
19+
public function __construct(string $text, string $url, ?string $style = null)
20+
{
21+
$this->options = [
22+
'type' => 'button',
23+
'text' => [
24+
'type' => 'plain_text',
25+
'text' => $text,
26+
],
27+
'url' => $url,
28+
];
29+
30+
if ($style) {
31+
// primary or danger
32+
$this->options['style'] = $style;
33+
}
34+
}
35+
}

src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackSectionBlock.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,46 @@ public function __construct()
2424
/**
2525
* @return $this
2626
*/
27-
public function text(string $text, bool $markdown = true): static
27+
public function text(string $text, bool $markdown = true, bool $emoji = true, bool $verbatim = false): static
2828
{
2929
$this->options['text'] = [
3030
'type' => $markdown ? 'mrkdwn' : 'plain_text',
3131
'text' => $text,
3232
];
3333

34+
// verbatim is only available for markdown
35+
if ($markdown) {
36+
$this->options['text']['verbatim'] = $verbatim;
37+
} else {
38+
$this->options['text']['emoji'] = $emoji;
39+
}
40+
3441
return $this;
3542
}
3643

3744
/**
3845
* @return $this
3946
*/
40-
public function field(string $text, bool $markdown = true): static
47+
public function field(string $text, bool $markdown = true, bool $emoji = true, bool $verbatim = false): static
4148
{
4249
if (10 === \count($this->options['fields'] ?? [])) {
4350
throw new \LogicException('Maximum number of fields should not exceed 10.');
4451
}
4552

46-
$this->options['fields'][] = [
53+
$field = [
4754
'type' => $markdown ? 'mrkdwn' : 'plain_text',
4855
'text' => $text,
4956
];
5057

58+
// verbatim is only available for markdown
59+
if ($markdown) {
60+
$field['verbatim'] = $verbatim;
61+
} else {
62+
$field['emoji'] = $emoji;
63+
}
64+
65+
$this->options['fields'][] = $field;
66+
5167
return $this;
5268
}
5369

src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
7.2
5+
---
6+
7+
* Add `SlackButtonBlockElement` to add button as accessory to a section block
8+
* Add `emoji` and `verbatim` options to `text` and `field` methods in `SlackSectionBlock`
9+
410
6.3
511
---
612

src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackSectionBlockTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,32 @@ public function testCanBeInstantiated()
2222
$section = new SlackSectionBlock();
2323
$section->text('section text');
2424
$section->field('section field');
25+
$section->field('section field with verbatim', true, true, false);
26+
$section->field('section field in plain text with verbatim', false, true, true);
2527
$section->accessory(new SlackImageBlockElement('https://example.com/image.jpg', 'an image'));
2628

2729
$this->assertSame([
2830
'type' => 'section',
2931
'text' => [
3032
'type' => 'mrkdwn',
3133
'text' => 'section text',
34+
'verbatim' => false,
3235
],
3336
'fields' => [
3437
[
3538
'type' => 'mrkdwn',
3639
'text' => 'section field',
40+
'verbatim' => false,
41+
],
42+
[
43+
'type' => 'mrkdwn',
44+
'text' => 'section field with verbatim',
45+
'verbatim' => false,
46+
],
47+
[
48+
'type' => 'plain_text',
49+
'text' => 'section field in plain text with verbatim',
50+
'emoji' => true,
3751
],
3852
],
3953
'accessory' => [

src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackOptionsTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public static function fromNotificationProvider(): iterable
146146
'text' => [
147147
'type' => 'mrkdwn',
148148
'text' => $subject,
149+
'verbatim' => false,
149150
],
150151
],
151152
],
@@ -162,13 +163,15 @@ public static function fromNotificationProvider(): iterable
162163
'text' => [
163164
'type' => 'mrkdwn',
164165
'text' => $subject,
166+
'verbatim' => false,
165167
],
166168
],
167169
[
168170
'type' => 'section',
169171
'text' => [
170172
'type' => 'mrkdwn',
171173
'text' => $content,
174+
'verbatim' => false,
172175
],
173176
],
174177
],

0 commit comments

Comments
 (0)