From 9133266e7c72a7e113e497d58f4233abcf800c43 Mon Sep 17 00:00:00 2001 From: Awilum Date: Wed, 20 Nov 2019 20:46:49 +0300 Subject: [PATCH] feat(core): Forms API - add new heading field #299 --- flextype/core/Forms.php | 43 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/flextype/core/Forms.php b/flextype/core/Forms.php index a1d93d75..2d4ffd17 100644 --- a/flextype/core/Forms.php +++ b/flextype/core/Forms.php @@ -11,6 +11,8 @@ namespace Flextype; use Flextype\Component\Arr\Arr; use Flextype\Component\Form\Form; +use Flextype\Component\Html\Html; +use Flextype\Component\Filesystem\Filesystem; use Psr\Http\Message\ServerRequestInterface as Request; use function count; use function date; @@ -117,7 +119,7 @@ class Forms $property['attributes'] = Arr::keyExists($property, 'attributes') ? $property['attributes'] : []; // Create attribute class - $property['attributes']['class'] = Arr::keyExists($property, 'attributes.class') ? $this->field_class . ' ' . $property['attributes']['class'] : $this->field_class; + $property['attributes']['class'] = Arr::keyExists($property, 'attributes.class') ? $property['attributes']['class'] : ''; // Create attribute size $property['size'] = Arr::keyExists($property, 'size') ? $this->sizes[$property['size']] : $this->sizes['12']; @@ -169,6 +171,9 @@ class Forms case 'visibility_select': $form_field = $this->visibilitySelectField($field_id, $field_name, ['draft' => __('admin_entries_draft'), 'visible' => __('admin_entries_visible'), 'hidden' => __('admin_entries_hidden')], (! empty($form_value) ? $form_value : 'visible'), $property); break; + case 'heading': + $form_field = $this->headingField($field_id, $property); + break; case 'tags': $form_field = $this->tagsField($field_id, $field_name, $form_value, $property); break; @@ -263,6 +268,7 @@ class Forms protected function mediaSelectField(string $field_id, string $field_name, array $options, string $value, array $property) : string { $property['attributes']['id'] = $field_id; + $property['attributes']['class'] .= ' ' . $this->field_class; $field = '
'; $field .= ($property['title'] ? Form::label($field_id, __($property['title'])) : ''); @@ -288,6 +294,7 @@ class Forms protected function templateSelectField(string $field_id, string $field_name, string $value, array $property) : string { $property['attributes']['id'] = $field_id; + $property['attributes']['class'] .= ' ' . $this->field_class; $_templates_list = $this->flextype['themes']->getTemplates($this->flextype['registry']->get('settings.theme')); @@ -323,9 +330,10 @@ class Forms * * @access protected */ - protected function selectField(string $field_id, string $field_name, array $options, string $value, array $property) : string + protected function selectField(string $field_id, string $field_name, array $options, $value, array $property) : string { $property['attributes']['id'] = $field_id; + $property['attributes']['class'] .= ' ' . $this->field_class; $field = '
'; $field .= ($property['title'] ? Form::label($field_id, __($property['title'])) : ''); @@ -336,6 +344,30 @@ class Forms return $field; } + /** + * Heading field + * + * @param string $field_id Field ID + * @param array $property Field property + * + * @return string Returns field + * + * @access protected + */ + protected function headingField(string $field_id, array $property) : string + { + $title = isset($property['title']) ? $property['title'] : ''; + $h = isset($property['h']) ? $property['h'] : 3; + + $property['attributes']['id'] = $field_id; + + $field = '
'; + $field .= Html::heading(__($title), $h, $property['attributes']); + $field .= '
'; + + return $field; + } + /** * Html field * @@ -352,6 +384,7 @@ class Forms { $property['attributes']['id'] = $field_id; $property['attributes']['class'] .= ' js-html-editor'; + $property['attributes']['class'] .= ' ' . $this->field_class; $field = '
'; $field .= ($property['title'] ? Form::label($field_id, __($property['title'])) : ''); @@ -396,6 +429,7 @@ class Forms protected function textareaField(string $field_id, string $field_name, string $value, array $property) : string { $property['attributes']['id'] = $field_id; + $property['attributes']['class'] .= ' ' . $this->field_class; $field = '
'; $field .= ($property['title'] ? Form::label($field_id, __($property['title'])) : ''); @@ -421,6 +455,7 @@ class Forms protected function visibilitySelectField(string $field_id, string $field_name, array $options, string $value, array $property) : string { $property['attributes']['id'] = $field_id; + $property['attributes']['class'] .= ' ' . $this->field_class; $field = '
'; $field .= ($property['title'] ? Form::label($field_id, __($property['title'])) : ''); @@ -446,6 +481,7 @@ class Forms protected function textField(string $field_id, string $field_name, string $value, array $property) : string { $property['attributes']['id'] = $field_id; + $property['attributes']['class'] .= ' ' . $this->field_class; $field = '
'; $field .= ($property['title'] ? Form::label($field_id, __($property['title'])) : ''); @@ -470,10 +506,11 @@ class Forms protected function tagsField(string $field_id, string $field_name, string $value, array $property) : string { $property['attributes']['id'] = $field_id; + $property['attributes']['class'] .= ' ' . $this->field_class; $field = '
'; $field .= ($property['title'] ? Form::label($field_id, __($property['title'])) : ''); - $field .= ''; + $field .= ''; $field .= ($property['help'] ? '' . __($property['help']) . '' : ''); $field .= '
';