Skip to content

Commit fd5074b

Browse files
committed
Initial commit.
1 parent fc6c80a commit fd5074b

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
1-
# webform_checkboxes_table
1+
# Webform Checkboxes Table
2+
3+
Webform checkboxes table (grid) component for Drupal 8 Webform module.
4+
5+
The idea is to display checkboxes field in a grid (similar to Likert field but supports multiple values).
6+
7+
| Field title | Option 1 | Option 2 | Option 3 | ... | Option N |
8+
| ------------- | ------------- | ------------- | ------------- | ------------- | ------------- |
9+
| Checkbox field title 1 | [ ] | [ ] | [x] | ... | [ ] |
10+
| Checkbox field title 2 | [x] | [ ] | [x] | ... | [x] |
11+
12+
Here is example YAML source of the checkboxes table grid.
13+
14+
```YAML
15+
checkboxes_table:
16+
'#type': checkboxes_table
17+
'#header':
18+
- 'Field Label'
19+
- 'Option 1'
20+
- 'Option 2'
21+
- 'Option 3'
22+
- 'Option 4'
23+
my_checkboxes_field_title:
24+
'#type': checkboxes
25+
'#title': 'My Checkboxes field title'
26+
'#options':
27+
'Checkbox 1': 'Checkbox 1'
28+
'Checkbox 2': 'Checkbox 2'
29+
'Checkbox 3': 'Checkbox 3'
30+
'Checkbox 4': 'Checkbox 4'
31+
another_checkboxes_field:
32+
'#type': checkboxes
33+
'#title': 'Another checkboxes field'
34+
'#options':
35+
'Option 1': 'Option 1'
36+
'Option 2': 'Option 2'
37+
'Option 3': 'Option 3'
38+
'Option 4': 'Option 4'
39+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Drupal\webform_checkboxes_table\Element;
4+
5+
use Drupal\Core\Render\Element\Table;
6+
7+
/**
8+
* Provides a render element for a checkboxes table.
9+
*
10+
* @RenderElement("webform_checkboxes_table")
11+
*/
12+
class WebformCheckboxesTable extends Table {
13+
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public static function preRenderTable($element) {
18+
$element = parent::preRenderTable($element, $form_state, $complete_form);
19+
$row = 0;
20+
foreach (self::getCheckboxesElement($element) as $el_id) {
21+
$title_array = ['data' => $element[$el_id]['#title']];
22+
// Add field title to the beginning of the table data array.
23+
array_unshift($element['#rows'][$row]['data'], $title_array);
24+
$row++;
25+
}
26+
return $element;
27+
}
28+
29+
/**
30+
* Get checkboxes elements from a huge array mess (not sure if this is a reliable way).
31+
*/
32+
protected static function getCheckboxesElement($element) {
33+
$ids = [];
34+
foreach ($element as $key => $value) {
35+
if (!strstr($key, '#')) {
36+
$ids[] = $key;
37+
}
38+
}
39+
return $ids;
40+
}
41+
42+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Drupal\webform_checkboxes_table\Plugin\WebformElement;
4+
5+
use Drupal\webform\Plugin\WebformElement\Table;
6+
7+
/**
8+
* Provides a 'checkbxoes table' element.
9+
*
10+
* @WebformElement(
11+
* id = "webform_checkboxes_table",
12+
* api = "https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!Element!Table.php/class/Table",
13+
* label = @Translation("Checkboxes Table"),
14+
* description = @Translation("Provides an element to render checkboxes in a table."),
15+
* category = @Translation("Markup elements"),
16+
* )
17+
*/
18+
class CheckboxesTable extends Table {
19+
20+
}

webform_checkboxes_table.info.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: Webform Checkboxes Table
2+
type: module
3+
description: Display checkboxes element in a table grid
4+
core: 8.x
5+
package: 'Webform example'
6+
dependencies:
7+
- 'webform:webform'

0 commit comments

Comments
 (0)