diff --git a/phpBB/adm/style/acp_attachments.html b/phpBB/adm/style/acp_attachments.html index 38be1baac0..c33d30b1c1 100644 --- a/phpBB/adm/style/acp_attachments.html +++ b/phpBB/adm/style/acp_attachments.html @@ -37,7 +37,6 @@ - {% import 'macros/form_macros.twig' as form_macros %}
@@ -52,7 +51,7 @@

{options.TITLE_EXPLAIN}
{% if options.CONTENT is iterable %} - {{ form_macros.build_template(options.CONTENT)}} + {{ FormsBuildTemplate(options.CONTENT)}} {% else %} {options.CONTENT} {% endif %} @@ -210,8 +209,7 @@
- {% from 'macros/form_macros.twig' import select %} - {{ select(EXT_GROUP_SIZE_OPTIONS) }} + {{ FormsSelect(EXT_GROUP_SIZE_OPTIONS) }}
diff --git a/phpBB/adm/style/acp_board.html b/phpBB/adm/style/acp_board.html index b809e2dfef..ae1525d586 100644 --- a/phpBB/adm/style/acp_board.html +++ b/phpBB/adm/style/acp_board.html @@ -13,7 +13,6 @@ -{% import 'macros/form_macros.twig' as form_macros %} @@ -30,7 +29,7 @@

{options.TITLE_EXPLAIN}
{% if options.CONTENT is iterable %} - {{ form_macros.build_template(options.CONTENT)}} + {{ FormsBuildTemplate(options.CONTENT)}} {% else %} {{ options.CONTENT }} {% endif %} diff --git a/phpBB/adm/style/acp_bots.html b/phpBB/adm/style/acp_bots.html index 2b9fec668f..b4f8ea5072 100644 --- a/phpBB/adm/style/acp_bots.html +++ b/phpBB/adm/style/acp_bots.html @@ -32,8 +32,7 @@

{L_BOT_LANG_EXPLAIN}
- {% import 'macros/form_macros.twig' as form_macros %} - {{ form_macros.select(LANG_OPTIONS) }} + {{ FormsSelect(LANG_OPTIONS) }}
diff --git a/phpBB/adm/style/acp_inactive.html b/phpBB/adm/style/acp_inactive.html index 81c019c47c..153a93fdb4 100644 --- a/phpBB/adm/style/acp_inactive.html +++ b/phpBB/adm/style/acp_inactive.html @@ -65,8 +65,7 @@
- {% from 'macros/form_macros.twig' import select %} - {{ select(INACTIVE_OPTIONS) }} + {{ FormsSelect(INACTIVE_OPTIONS) }}

{L_MARK_ALL}{L_UNMARK_ALL}

{S_FORM_TOKEN} diff --git a/phpBB/adm/style/acp_users_prefs.html b/phpBB/adm/style/acp_users_prefs.html index 11f6a71e20..68420389b4 100644 --- a/phpBB/adm/style/acp_users_prefs.html +++ b/phpBB/adm/style/acp_users_prefs.html @@ -43,8 +43,7 @@
- {% import 'macros/form_macros.twig' as form_macros %} - {{ form_macros.select(LANG_OPTIONS) }} + {{ FormsSelect(LANG_OPTIONS) }}
diff --git a/phpBB/adm/style/timezone_option.html b/phpBB/adm/style/timezone_option.html index 58b88cf6b4..9b0817902a 100644 --- a/phpBB/adm/style/timezone_option.html +++ b/phpBB/adm/style/timezone_option.html @@ -1,15 +1,14 @@
- {% from "macros/form_macros.twig" import select %} -
+
{% if TIMEZONE_OPTIONS %}
- {% set tz_date_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_CURRENT_TIME') }]|merge(TIMEZONE_OPTIONS.options) }) %} - {{ select(tz_date_data, 'autowidth tz_select', 'tz_date', 'tz_date', true) }} + {% set tz_date_data = TIMEZONE_OPTIONS | merge({options: [{ value: "", label: lang('SELECT_CURRENT_TIME')}] | merge(TIMEZONE_OPTIONS.options) }) %} + {{ FormsSelect(tz_date_data | merge({class: 'autowidth tz_select', id: 'tz_date', name: 'tz_date', group_only: true})) }}
{% endif %}
- {% set tz_select_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_TIMEZONE') }]|merge(TIMEZONE_OPTIONS.options) }) %} - {{ select(tz_select_data, 'autowidth tz_select', 'timezone') }} + {% set tz_select_data = TIMEZONE_OPTIONS | merge({ options: [{ value: "", label: lang('SELECT_TIMEZONE') }] | merge(TIMEZONE_OPTIONS.options) }) %} + {{ FormsSelect(tz_select_data | merge({class: 'autowidth tz_select', id: 'timezone'})) }} {% INCLUDEJS('timezone.js') %}
diff --git a/phpBB/config/default/container/services_twig.yml b/phpBB/config/default/container/services_twig.yml index 747d2ee155..8700630a73 100644 --- a/phpBB/config/default/container/services_twig.yml +++ b/phpBB/config/default/container/services_twig.yml @@ -69,6 +69,13 @@ services: template.twig.extensions.debug: class: Twig\Extension\DebugExtension + template.twig.extensions.forms: + class: phpbb\template\twig\extension\forms + arguments: + - '@user' + tags: + - { name: twig.extension } + template: class: phpbb\template\twig\twig arguments: diff --git a/phpBB/phpbb/template/twig/environment.php b/phpBB/phpbb/template/twig/environment.php index 5843cc0762..86c04195de 100644 --- a/phpBB/phpbb/template/twig/environment.php +++ b/phpBB/phpbb/template/twig/environment.php @@ -75,7 +75,7 @@ class environment extends \Twig\Environment $options = array_merge(array( 'cache' => (defined('IN_INSTALL')) ? false : $cache_path, - 'debug' => false, + 'debug' => true, 'auto_reload' => (bool) $this->phpbb_config['load_tplcompile'], 'autoescape' => false, ), $options); diff --git a/phpBB/phpbb/template/twig/extension/forms.php b/phpBB/phpbb/template/twig/extension/forms.php new file mode 100644 index 0000000000..21dcd79dca --- /dev/null +++ b/phpBB/phpbb/template/twig/extension/forms.php @@ -0,0 +1,217 @@ + + * @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; + +use phpbb\template\twig\environment; +use phpbb\user; +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; + +class forms extends AbstractExtension +{ + /** @var user */ + protected $user; + + /** + * Constructor. + * + * @param user $user User object + */ + public function __construct(user $user) + { + $this->user = $user; + } + + /** + * Returns the name of this extension. + * + * @return string The extension name + */ + public function getName() + { + return 'forms'; + } + + /** + * Returns a list of functions to add to the existing list. + * + * @return TwigFunction[] Array of twig functions + */ + public function getFunctions(): array + { + return [ + new TwigFunction('FormsBuildTemplate', [$this, 'build_template'], ['needs_environment' => true]), + new TwigFunction('FormsDimension', [$this, 'dimension'], ['needs_environment' => true]), + new TwigFunction('FormsInput', [$this, 'input'], ['needs_environment' => true]), + new TwigFunction('FormsRadioButtons', [$this, 'radio_buttons'], ['needs_environment' => true]), + new TwigFunction('FormsSelect', [$this, 'select'], ['needs_environment' => true]), + new TwigFunction('FormsTextarea', [$this, 'textarea'], ['needs_environment' => true]), + ]; + } + + /** + * Renders a form template + * + * @param environment $environment + * @param array $form_data + * + * @return string Rendered form template + */ + public function build_template(environment $environment, array $form_data): string + { + try + { + return $environment->render('macros/forms/build_template.twig', [ + 'form_data' => $form_data ?? [], + ]); + } + catch (\Twig\Error\Error $e) + { + return ''; + } + } + + /** + * Renders form dimension fields + * + * @param environment $environment The twig environment + * @param array $form_data The form data + * + * @return string Form dimension fields + */ + public function dimension(environment $environment, array $form_data): string + { + try + { + return $environment->render('macros/forms/dimension.twig', [ + 'WIDTH' => $form_data['width'], + 'HEIGHT' => $form_data['height'], + ]); + } + catch (\Twig\Error\Error $e) + { + return ''; + } + } + + /** + * Renders a form input field + * + * @param environment $environment The twig environment + * @param array $form_data The form data + * + * @return string Form input field + */ + public function input(environment $environment, array $form_data): string + { + try + { + return $environment->render('macros/forms/input.twig', [ + 'ID' => (string) $form_data['id'] ?? '', + 'TYPE' => (string) $form_data['type'], + 'NAME' => (string) $form_data['name'], + 'SIZE' => (int) $form_data['size'] ?? 0, + 'MAXLENGTH' => (int) $form_data['maxlength'] ?? 0, + 'MIN' => (int) $form_data['min'] ?? 0, + 'MAX' => (int) $form_data['max'] ?? 0, + 'STEP' => (int) $form_data['step'] ?? 0, + 'CHECKED' => (bool) $form_data['checked'] ?? false, + 'CLASS' => (string) $form_data['class'] ?? '', + 'VALUE' => (string) $form_data['value'], + ]); + } + catch (\Twig\Error\Error $e) + { + return ''; + } + } + + /** + * Renders form radio buttons + * + * @param environment $environment The twig environment + * @param array $form_data The form data + * + * @return string Form radio buttons + */ + public function radio_buttons(environment $environment, array $form_data): string + { + try + { + return $environment->render('macros/forms/radio_buttons.twig', [ + 'FIRST_BUTTON' => $form_data['buttons'][0], + 'FIRST_BUTTON_LABEL' => $form_data['buttons'][0]['label'], + 'SECOND_BUTTON' => $form_data['buttons'][1], + 'SECOND_BUTTON_LABEL' => $form_data['buttons'][1]['label'], + ]); + } + catch (\Twig\Error\Error $e) + { + return ''; + } + } + + /** + * Renders a form select field + * + * @param environment $environment The twig environment + * @param array $form_data The form data + * + * @return string Form select field + */ + public function select(environment $environment, array $form_data): string + { + try + { + return $environment->render('macros/forms/select.twig', [ + 'ID' => (string) $form_data['id'] ?? '', + 'CLASS' => (string) $form_data['class'] ?? '', + 'NAME' => (string) $form_data['name'], + 'TOGGLEABLE' => (bool) $form_data['toggleable'] ?? false, + 'OPTIONS' => $form_data['options'] ?? [], + 'GROUP_ONLY' => (bool) $form_data['group_only'] ?? false, + ]); + } + catch (\Twig\Error\Error $e) + { + return ''; + } + } + + /** + * Renders a form textarea field + * + * @param environment $environment + * @param array $form_data + * + * @return string Form textarea field + */ + public function textarea(environment $environment, array $form_data): string + { + try + { + return $environment->render('macros/forms/textarea.twig', [ + 'ID' => (string) $form_data['id'] ?? '', + 'NAME' => (string) $form_data['name'] ?? '', + 'ROWS' => (int) $form_data['rows'] ?? 1, + 'COLS' => (int) $form_data['cols'] ?? 1, + 'CONTENT' => (string) $form_data['content'] ?? '', + ]); + } + catch (\Twig\Error\Error $e) + { + return ''; + } + } +} diff --git a/phpBB/phpbb/template/twig/twig.php b/phpBB/phpbb/template/twig/twig.php index 1259618175..14dd5734ce 100644 --- a/phpBB/phpbb/template/twig/twig.php +++ b/phpBB/phpbb/template/twig/twig.php @@ -90,6 +90,7 @@ class twig extends \phpbb\template\base { $this->twig->addExtension($extension); } + $this->twig->addExtension(new \Twig\Extension\DebugExtension()); // Add admin namespace if ($this->path_helper->get_adm_relative_path() !== null diff --git a/phpBB/styles/all/template/macros/form_macros.twig b/phpBB/styles/all/template/macros/form_macros.twig deleted file mode 100644 index a4fa779ff8..0000000000 --- a/phpBB/styles/all/template/macros/form_macros.twig +++ /dev/null @@ -1,86 +0,0 @@ -{% macro input(form_data) %} - {% apply replace({"\n": ' ', '\t': ''}) %} - - {% endapply %} -{% endmacro %} - -{% macro dimension(form_data) %} - {{ _self.input(form_data.width) }} x {{ _self.input(form_data.height) }} -{% endmacro %} - -{% macro textarea(form_data) %} - {% apply replace({"\n": ' ', '\t': ''}) %} - - {% endapply %} -{% endmacro %} - -{% macro radio_buttons(form_data) %} - - -{% endmacro %} - -{% macro select(form_data, class, id, name, group_only) %} - {% apply replace({"\n": ' ', '\t': ''}) %} - -{% endmacro %} - -{% macro build_template(form_data) %} - {% if form_data.tag == 'input' %} - {{ _self.input(form_data) }} - {% elseif form_data.tag == 'dimension' %} - {{ _self.dimension(form_data) }} - {% elseif form_data.tag == 'radio' %} - {{ _self.radio_buttons(form_data) }} - {% elseif form_data.tag == 'select' %} - {{ _self.select(form_data) }} - {% elseif form_data.tag == 'textarea' %} - {{ _self.textarea(form_data) }} - {% elseif form_data[0] %} - {% for element in form_data %} - {{ _self.build_template(element) }} - {% endfor %} - {% endif %} - {% if form_data.append %}{{ form_data.append }}{% endif %} -{% endmacro %} diff --git a/phpBB/styles/all/template/macros/forms/build_template.twig b/phpBB/styles/all/template/macros/forms/build_template.twig new file mode 100644 index 0000000000..fb63e3e1ed --- /dev/null +++ b/phpBB/styles/all/template/macros/forms/build_template.twig @@ -0,0 +1,16 @@ +{% if form_data.tag == 'input' %} + {{ FormsInput(form_data) }} +{% elseif form_data.tag == 'dimension' %} + {{ FormsDimension(form_data) }} +{% elseif form_data.tag == 'radio' %} + {{ FormsRadioButtons(form_data) }} +{% elseif form_data.tag == 'select' %} + {{ FormsSelect(form_data) }} +{% elseif form_data.tag == 'textarea' %} + {{ FormsTextarea(form_data) }} +{% elseif form_data[0] %} + {% for element in form_data %} + {{ FormsBuildTemplate(element) }} + {% endfor %} +{% endif %} +{% if form_data.append %}{{ form_data.append }}{% endif %} diff --git a/phpBB/styles/all/template/macros/forms/dimension.twig b/phpBB/styles/all/template/macros/forms/dimension.twig new file mode 100644 index 0000000000..05bf563168 --- /dev/null +++ b/phpBB/styles/all/template/macros/forms/dimension.twig @@ -0,0 +1 @@ +{{ FormsInput(WIDTH) }} x {{ FormsInput(HEIGHT) }} diff --git a/phpBB/styles/all/template/macros/forms/input.twig b/phpBB/styles/all/template/macros/forms/input.twig new file mode 100644 index 0000000000..cf58826235 --- /dev/null +++ b/phpBB/styles/all/template/macros/forms/input.twig @@ -0,0 +1,15 @@ +{% apply replace({"\n": ' ', "\t": ''}) %} + +{% endapply %} diff --git a/phpBB/styles/all/template/macros/forms/radio_buttons.twig b/phpBB/styles/all/template/macros/forms/radio_buttons.twig new file mode 100644 index 0000000000..1ef7804c29 --- /dev/null +++ b/phpBB/styles/all/template/macros/forms/radio_buttons.twig @@ -0,0 +1,2 @@ + + diff --git a/phpBB/styles/all/template/macros/forms/select.twig b/phpBB/styles/all/template/macros/forms/select.twig new file mode 100644 index 0000000000..2f16a1afe2 --- /dev/null +++ b/phpBB/styles/all/template/macros/forms/select.twig @@ -0,0 +1,26 @@ +{% apply replace({"\n": ' ', "\t": ''}) %} + diff --git a/phpBB/styles/all/template/macros/forms/textarea.twig b/phpBB/styles/all/template/macros/forms/textarea.twig new file mode 100644 index 0000000000..51a4c98ee6 --- /dev/null +++ b/phpBB/styles/all/template/macros/forms/textarea.twig @@ -0,0 +1,9 @@ +{% apply replace({"\n": ' ', '\t': ''}) %} + +{% endapply %} diff --git a/phpBB/styles/prosilver/template/timezone_option.html b/phpBB/styles/prosilver/template/timezone_option.html index 47da5f63c1..0c7be8de74 100644 --- a/phpBB/styles/prosilver/template/timezone_option.html +++ b/phpBB/styles/prosilver/template/timezone_option.html @@ -1,16 +1,15 @@
- {% from "macros/form_macros.twig" import select %}
{% if TIMEZONE_OPTIONS %}
- {% set tz_date_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_CURRENT_TIME') }]|merge(TIMEZONE_OPTIONS.options) }) %} - {{ select(tz_date_data, 'autowidth tz_select', 'tz_date', 'tz_date', true) }} + {% set tz_date_data = TIMEZONE_OPTIONS | merge({ options: [{ value: "", label: lang('SELECT_CURRENT_TIME') }] | merge(TIMEZONE_OPTIONS.options) }) %} + {{ FormsSelect(tz_date_data | merge({class: 'autowidth tz_select', id: 'tz_date', name: 'tz_date', group_only: true})) }}
{% endif %}
- {% set tz_select_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_TIMEZONE') }]|merge(TIMEZONE_OPTIONS.options) }) %} - {{ select(tz_select_data, 'autowidth tz_select timezone', 'timezone') }} + {% set tz_select_data = TIMEZONE_OPTIONS | merge({ options: [{ value: "", label: lang('SELECT_TIMEZONE') }] | merge(TIMEZONE_OPTIONS.options) }) %} + {{ FormsSelect(tz_select_data | merge({class: 'autowidth tz_select', id: 'timezone'})) }} {% INCLUDEJS('timezone.js') %}
diff --git a/phpBB/styles/prosilver/template/ucp_agreement.html b/phpBB/styles/prosilver/template/ucp_agreement.html index 69458e198a..94d280a31a 100644 --- a/phpBB/styles/prosilver/template/ucp_agreement.html +++ b/phpBB/styles/prosilver/template/ucp_agreement.html @@ -6,8 +6,7 @@

- {% import "macros/form_macros.twig" as form_macros %} - {{ form_macros.select(LANG_OPTIONS) }} + {{ FormsSelect(LANG_OPTIONS) }} {S_HIDDEN_FIELDS}

diff --git a/phpBB/styles/prosilver/template/ucp_prefs_personal.html b/phpBB/styles/prosilver/template/ucp_prefs_personal.html index 2c5ff918e5..4e81b78fb3 100644 --- a/phpBB/styles/prosilver/template/ucp_prefs_personal.html +++ b/phpBB/styles/prosilver/template/ucp_prefs_personal.html @@ -54,8 +54,7 @@
- {% import "macros/form_macros.twig" as form_macros %} - {{ form_macros.select(LANG_OPTIONS) }} + {{ FormsSelect(LANG_OPTIONS) }}
diff --git a/phpBB/styles/prosilver/template/ucp_register.html b/phpBB/styles/prosilver/template/ucp_register.html index ad45812ab0..0956317497 100644 --- a/phpBB/styles/prosilver/template/ucp_register.html +++ b/phpBB/styles/prosilver/template/ucp_register.html @@ -61,8 +61,7 @@
- {% import "macros/form_macros.twig" as form_macros %} - {{ form_macros.select(LANG_OPTIONS) }} + {{ FormsSelect(LANG_OPTIONS) }}