1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-30 02:59:29 +02:00

[ticket/15289] Add twig extension to generate inputs from the template

PHPBB3-15289
This commit is contained in:
Rubén Calvo 2017-07-31 22:58:29 +02:00
parent 9fd8a6ee0f
commit 19b99a0770
5 changed files with 97 additions and 14 deletions

View File

@ -11,24 +11,28 @@
{% for storage in STORAGES %}
<fieldset>
<legend>{{ lang('STORAGE_' ~ storage.get_name | upper ~ '_TITLE') }}</legend>
<dl>
<dt><label for="{{ storage.get_name }}">{{ lang('STORAGE_SELECT') }}{{ lang('COLON') }}</label><br /><span>{{ lang('STORAGE_SELECT_DESC') }}</span></dt>
<dd>
<select id="{{ storage.get_name }}" name="{{ storage.get_name }}[provider]" data-togglable-settings="true">
{% for provider in PROVIDERS if provider.is_available %}
<option value="{{ provider.get_class }}"{{ attribute(config, 'storage\\' ~ storage.get_name ~ '\\provider') == provider.get_class ? ' selected' : '' }} data-toggle-setting="#{{ storage.get_name }}_{{ provider.get_name }}_settings">
{{ lang('STORAGE_ADAPTER_' ~ provider.get_name | upper ~ '_NAME') }}
</option>
{% endfor %}
</select>
</dd>
</dl>
<dl>
<dt><label for="{{ storage.get_name }}">{{ lang('STORAGE_SELECT') }}{{ lang('COLON') }}</label><br /><span>{{ lang('STORAGE_SELECT_DESC') }}</span></dt>
<dd>
<select id="{{ storage.get_name }}" name="{{ storage.get_name }}[provider]" data-togglable-settings="true">
{% for provider in PROVIDERS if provider.is_available %}
<option value="{{ provider.get_class }}"{{ attribute(config, 'storage\\' ~ storage.get_name ~ '\\provider') == provider.get_class ? ' selected' : '' }} data-toggle-setting="#{{ storage.get_name }}_{{ provider.get_name }}_settings">
{{ lang('STORAGE_ADAPTER_' ~ provider.get_name | upper ~ '_NAME') }}
</option>
{% endfor %}
</select>
</dd>
</dl>
</fieldset>
{% for provider in PROVIDERS if provider.is_available %}
<fieldset id="{{ storage.get_name }}_{{ provider.get_name }}_settings">
<legend>{{ lang('STORAGE_' ~ storage.get_name | upper ~ '_TITLE') }} - {{ lang('STORAGE_ADAPTER_' ~ provider.get_name | upper ~ '_NAME') }}</legend>
provider.get_options
{% for name, options in provider.get_options %}
{% set lang_name = 'STORAGE_ADAPTER_' ~ storage.get_name | upper ~ '_OPTION_' ~ name | upper %}
{% set options = options|merge({'name': storage.get_name ~ '[' ~ name ~ ']'}) %}
{{ adm_block(lang(lang_name), '', input(options)) }}
{% endfor %}
</fieldset>
{% endfor %}

View File

@ -48,6 +48,12 @@ services:
tags:
- { name: twig.extension }
template.twig.extensions.form:
class: phpbb\template\twig\extension\form
arguments:
tags:
- { name: twig.extension }
template.twig.extensions.debug:
class: Twig_Extension_Debug

View File

@ -46,6 +46,8 @@ $lang = array_merge($lang, array(
'STORAGE_AVATAR_TITLE' => 'Avatars storage',
'STORAGE_BACKUP_TITLE' => 'Backup storage',
'STORAGE_ADAPTER_LOCAL_OPTION_PATH' => 'Path',
'STORAGE_ADAPTER_LOCAL_NAME' => 'Local',
));

View File

@ -38,7 +38,7 @@ class local implements provider_interface
*/
public function get_options()
{
return ['path' => array('lang' => 'PATH', 'validate' => 'string', 'type' => 'text:40:255', 'explain' => false)];
return ['path' => array('type' => 'text')];
}
/**

View File

@ -0,0 +1,71 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\template\twig\extension;
// I will use this to generate forms in twig until there is something better
class form extends \Twig_Extension
{
/**
* Constructor
*/
public function __construct()
{
}
public function getFunctions()
{
return [
new \Twig_SimpleFunction('input', '\\phpbb\\template\\twig\\extension\\form::generate_input'),
new \Twig_SimpleFunction('adm_block', '\\phpbb\\template\\twig\\extension\\form::generate_block'),
];
}
public static function generate_input($options)
{
$input = '<input ';
switch ($options['type'])
{
case 'radio':
break;
default:
foreach ($options as $key => $value)
{
if (in_array($key, ['lang']))
continue;
$input .= "$key=\"$value\" ";
}
break;
}
$input .= '>';
return $input;
}
public static function generate_block($name, $description = '', $content)
{
return <<<EOF
<dl>
<dt><label for="">$name</label><br /><span>$description</span></dt>
<dd>
$content
</dd>
</dl>
EOF;
}
}