Skip to content
Snippets Groups Projects
Commit d71c8414 authored by Viktor Holovachek's avatar Viktor Holovachek
Browse files

Issue #3421587 - Add new fields

parent a33e4883
No related branches found
Tags 8.x-1.0-alpha2
1 merge request!4Issue #3421587 - Add new fields
Pipeline #101372 passed
......@@ -249,11 +249,8 @@ function blocktheme_get_blockthemes() {
$options[] = t('- None -');
if ($blockthemes) {
$_sets = explode("\n", $blockthemes);
foreach ($_sets as $value) {
$set = explode('|', $value);
$options[$set[0]] = $set[1];
foreach ($blockthemes as $values) {
$options[$values['name']] = $values['label'];
}
}
......
......@@ -3,8 +3,18 @@ blocktheme.settings:
label: 'Block theme settings'
mapping:
templates:
type: string
type: sequence
label: 'Custom Block Templates'
sequence:
type: mapping
label: 'Suggestion'
mapping:
name:
type: string
label: 'Machine name'
label:
type: string
label: 'Human readable name'
show:
type: boolean
label: 'Show Custom Block Theme'
......
......@@ -73,10 +73,47 @@ class BlockthemeAdminSettingsForm extends ConfigFormBase {
];
$form['templates'] = [
'#type' => 'textarea',
'#type' => 'fieldset',
'#title' => $this->t('Custom Block Templates'),
'#description' => $this->t('Enter one value per row in the form: <em>customtemplate|Friendly Name</em>, where "customtemplate" corresponds to a twig file called <em>block--blocktheme--customtemplate.html.twig</em> as well as to the value of an extra variable <em>blocktheme</em> in the block template.'),
'#default_value' => $config->get('templates'),
'#description' => $this->t('Enter pairs of template suggestions, where machine name corresponds to a twig file called <em>block--blocktheme--{MACHINE_NAME}.html.twig</em>.'),
'#tree' => TRUE,
'#prefix' => '<div id="templates-fieldset-wrapper">',
'#suffix' => '</div>',
];
$count = $form_state->get('templates_count');
if (empty($count)) {
$config_count = !empty($config->get('templates')) ? count($config->get('templates')) : 1;
$form_state->set('templates_count', $config_count);
$count = $config_count;
}
for ($i = 0; $i < $count; $i++) {
$form['templates'][$i]['name'] = [
'#type' => 'textfield',
'#title' => $this->t('Template machine name #@number', ['@number' => $i + 1]),
'#default_value' => !empty($config->get('templates')[$i]['name']) ? $config->get('templates')[$i]['name'] : '',
];
$form['templates'][$i]['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Template label #@number', ['@number' => $i + 1]),
'#default_value' => !empty($config->get('templates')[$i]['label']) ? $config->get('templates')[$i]['label'] : '',
];
}
$form['templates']['actions'] = [
'#type' => 'actions',
];
$form['templates']['actions']['template'] = [
'#type' => 'submit',
'#value' => $this->t('Add new suggestion'),
'#submit' => ['::addMore'],
'#ajax' => [
'callback' => '::addMoreCallback',
'wrapper' => 'templates-fieldset-wrapper',
],
];
$form['show'] = [
......@@ -91,12 +128,34 @@ class BlockthemeAdminSettingsForm extends ConfigFormBase {
return parent::buildForm($form, $form_state);
}
/**
* Ajax handler for the 'Add more' button.
*/
public function addMoreCallback(array &$form, FormStateInterface $form_state) {
return $form['templates'];
}
/**
* Submit handler for the 'Add more' button.
*/
public function addMore(array &$form, FormStateInterface $form_state) {
$form_state->set('templates_count', $form_state->get('templates_count') + 1);
$form_state->setRebuild();
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$templates = [];
foreach ($form_state->getValue('templates') as $key => $data) {
if (is_numeric($key) && !empty($data['name']) && !empty($data['label'])) {
$templates[] = $data;
}
}
$this->config('blocktheme.settings')
->set('templates', $form_state->getValue('templates'))
->set('templates', $templates)
->set('show', (bool) $form_state->getValue('show'))
->save();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment