Skip to content

Commit 9f6424a

Browse files
committed
Fix errors when blocks don't have collapse settings.
1 parent 70714ee commit 9f6424a

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/Plugin/Block/CollapsibleTrait.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public function blockForm($form, FormStateInterface $form_state) {
1616
'#title' => $this->t('Collapsible'),
1717
'#description' => $this->t('<insert description>'),
1818
'#default_value' => $this->configuration['collapsible'] ?? 0,
19-
2019
];
2120

2221
$form['collapse_width'] = [
@@ -42,12 +41,23 @@ public function blockSubmit($form, FormStateInterface $form_state): void {
4241
$this->configuration['collapse_width'] = $form_state->getValue('collapse_width');
4342
}
4443

44+
/**
45+
* Add collapsible data to a block build.
46+
*/
4547
protected function addCollabsibleData(array &$output) {
48+
49+
$collapsible = (bool) $this->configuration['collapsible'] ?? FALSE;
50+
$collapseWidth = (int) $this->configuration['collapse_width'] ?? 0;
51+
52+
// This needs a width to work.
53+
if ($collapseWidth === 0) {
54+
$collapsible = FALSE;
55+
}
56+
4657
$output['#attached']['drupalSettings']['localgov_publications'][$this->pluginId] = [
47-
'collapsible' => (bool) $this->configuration['collapsible'],
48-
'collapse_width' => (int) $this->configuration['collapse_width'],
58+
'collapsible' => $collapsible,
59+
'collapse_width' => $collapseWidth,
4960
];
50-
5161
$output['#attached']['library'][] = 'localgov_publications/localgov-publications-blocks';
5262
}
5363

0 commit comments

Comments
 (0)