Joomla! Template Override:

Kontaktformular mit Captcha am Ende


Das ist ein kleiner Override für das Kontaktformulars in Joomla. Der Captcha wird hier an letzte Stelle gesetzt im Formular.

Das Override beinhaltet die folgenden Features:

  • Verschiebung des Captcha ans Ende des Formulars
  • Pflichtfelder des Standard-Kontaktformulars können für den Besucher ausgeblendet werden
  • Custom Fields können individuell im Formular positioniert werden
  • Der Senden-Button kann beschriftet werden
  • Legenden des Formulars können ausgeblendet werden

Um die Features zu nutzen genügt es, einfache Eintragungen im Startbereich des Overrides zu ändern. 
Dies ermöglicht auch Nicht-Programmierern, das Formular individuell anzupassen.

Installation des Overrides

Kopiere die 2 Dateien default_form.php und bit_contact_tool.php in den richtigen Override-Ordner deines Templates:
<mein-template>/html/com_contact/contact
Wenn dieser Ordner noch nicht vorhanden ist, musst du ihn erstellen.
Achtung: Wenn bereits eine Datei mit dem Namen default_form.php vorhanden ist, wird diese durch unsere Datei ersetzt.
Nach dem Kopieren der 2 Dateien sollte dein Kontaktformular wie vorher aussehen, außer wenn das Captcha Feld in Verwendung ist: dieses Feld wird an das Ende des Formulars verschoben.

 

Benötigte Override-Dateien


  • <?php
    /**
     * @package     Joomla.Site
     * @subpackage  com_contact
     *              override by barg-it -- requires 'bit_contact_tool.php'
     *				override version 1.0.0
     *
     * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
     * @license     GNU General Public License version 2 or later; see LICENSE.txt
     *
     */
    defined('_JEXEC') or die;
    JHtml::_('behavior.keepalive');
    JHtml::_('behavior.formvalidator');
    require 'bit_contact_tool.php';
    $BIT_TOOL = new BIT_CONTACT_TOOL($this->form, $this->captchaEnabled); // 1. the form object | 2. (protected) captchaEnabled
    
    $BIT_TOOL->show_admin_infos = false;          // .. um die verfügbaren custom-fields zu zeigen -- und mehr / shows available fields and more 
    $BIT_TOOL->show_legends = true;             // .. um alle fieldset/legend-tags im html zu entfernen (ohne CSS) / set this to 'false' if you want to hide all field legends
    $BIT_SENDBUTTON_TEXT = JText::_('JSUBMIT'); // .. um die Beschriftung des Versenden-Buttons zu definieren -- Standard: JText::_('COM_CONTACT_CONTACT_SEND'); / text for submit button
    
    // Positionieren der Custom Fields / Positioning of Custom Fields
    // Please insert your custom fields where you want them to show up, 
    // e.g. $BIT_TOOL->custom_positions['contact_name'] = 'phone,fax' - will place the custom fields phone and fax after the contact name
    $BIT_TOOL->custom_positions['spacer'] = ''; // ganz am Anfang / top
    $BIT_TOOL->custom_positions['contact_name'] = '';  // after name field
    $BIT_TOOL->custom_positions['contact_email'] = 'terms'; // after e-mail field
    $BIT_TOOL->custom_positions['contact_subject'] = ''; // after subject
    $BIT_TOOL->custom_positions['contact_message'] = '';	// after message
    
    // Verbergen von System-Feldern durch Vergabe eines Wertes (erforderlich, da Pflichtfelder)
    // Hide mandatory system fields by assigning a value, e.g. 
    // $BIT_TOOL->contact_presets['contact_name'] = 'guest'; will hide the name field (the e-mail will contain 'guest' as name)
    $BIT_TOOL->contact_presets['contact_name'] = '';
    $BIT_TOOL->contact_presets['contact_email'] = '';
    $BIT_TOOL->contact_presets['contact_subject'] = '';
    $BIT_TOOL->contact_presets['contact_message'] = '';
    
    ?>
    
    <div class="contact-form">
    	<form id="contact-form" action="<?php echo JRoute::_('index.php'); ?>" method="post" class="form-validate form-horizontal well">
    <?php $BIT_TOOL->show_fields();?>
    		<div class="control-group">
    			<div class="controls">
    				<button class="btn btn-primary validate" type="submit"><?php echo $BIT_SENDBUTTON_TEXT; ?></button>
    				<input type="hidden" name="option" value="com_contact" />
    				<input type="hidden" name="task" value="contact.submit" />
    				<input type="hidden" name="return" value="<?php echo $this->return_page; ?>" />
    				<input type="hidden" name="id" value="<?php echo $this->contact->slug; ?>" />
    				<?php echo JHtml::_('form.token'); ?>
    			</div>
    		</div>
    	</form>
    </div>
  • <?php
    /**
     * @copyright   Copyright (C) 2019 BARG-IT
     * @license     GNU General Public License version 2 or later
     *
     * helps customizing the contact-form (default_form.php)
     */
    
    defined('_JEXEC') or die;
    class BIT_CONTACT_TOOL
    {
        public $show_admin_infos = false;
        public $show_legends = true;
        public $contact_presets = ['contact_name' => '', 'contact_email' => '', 'contact_subject' => '', 'contact_message' => ''];
        public $custom_positions = ['spacer' => '', 'contact_name' => '', 'contact_email' => '', 'contact_subject' => '', 'contact_message' => ''];
    
        private $rendered_cf = [];
        private $rendered_captcha = '';
    
        public function wrap_with_class($inner = '', $dom_el = 'div', $css_class = '')
        {
            $class_html = trim($css_class) == '' ? '' : ' class="' . $css_class . '"';
            $start_tag = '<' . trim($dom_el) . $class_html . '>';
            $end_tag = '</' . trim($dom_el) . '>';
            return $start_tag . $inner . $end_tag;
        }
    
        public function admin_info($header = '', $what = '')
        {
            if ($this->show_admin_infos) {
                $html = ($header != '') ? $this->wrap_with_class($header, 'h4', '') : '';
                $out = print_r($what, true);
                $out = is_array($what) ? substr($out, 8, (strlen($out) - 10)) : $out;
                echo $this->wrap_with_class($html . $out, 'pre', 'admin-info');
            }
        }
    
        private function uses_custom_positioning()
        {
            $x = $this->custom_positions['spacer'] . $this->custom_positions['contact_name'] . $this->custom_positions['contact_email'] . $this->custom_positions['contact_subject'] . $this->custom_positions['contact_message'];
            return (trim($x) != '');
        }
    
        private function is_customfield_set($fs_name)
        {
            return (substr($fs_name, 0, 7) === 'fields-');
        }
    
        private function html_4_connected_custom_fields($fn)
        {
            $html = '';
            if (isset($this->custom_positions[$fn])) {
                $arr_cf = array_map('trim', explode(",", $this->custom_positions[$fn]));
                foreach ($arr_cf as $cf) {
                    if (isset($this->rendered_cf[$cf])) {$html .= $this->rendered_cf[$cf];}
                }
            }
            return $html;
        }
    
        private function html_4_contact_field($field)
        {
            $html = '';
            $clean_name = explode(']', explode('[', $field->name)[1])[0];
            // preg_match ("/\[(.*)\]/", $field->name, $match); $clean_name =  $match[1]; // alternativ
            if (isset($this->contact_presets[$clean_name]) && ($preset = trim($this->contact_presets[$clean_name])) !== '') {
                // Hide basic contact-field (required field) by presetting
                $html .= '<input type="hidden" name="jform[' . $clean_name . ']" id="jform_' . $clean_name . '" value="' . $preset . '">';
            } else {
                $html .= $field->renderField();
            }
            $html .= $this->html_4_connected_custom_fields($clean_name);
            return $html;
        }
    
        public function html_4_fieldset($fieldset)
        {
            $html = '';
            $fields = $this->form->getFieldset($fieldset->name);
            if (count($fields)) {
                foreach ($fields as $field) {
                    if ($fieldset->name == 'contact') {
                        $html .= $this->html_4_contact_field($field);
                    } else if ($this->is_customfield_set($fieldset->name)) {
                        $html .= $this->uses_custom_positioning() ? '' : $field->renderField();
                    } else {
                        $html .= $field->renderField();
                    }
                };
            }
            if ($this->show_legends) {
                if (!($this->is_customfield_set($fieldset->name) && $this->uses_custom_positioning())) {
                    if (isset($fieldset->label) && ($legend = trim(JText::_($fieldset->label))) !== '') {$html = $this->wrap_with_class($legend, 'legend', '') . $html;}
                    $html = $this->wrap_with_class($html, 'fieldset', $fieldset->name);
                }
            }
            return $html;
        }
    
        public function show_fields()
        {
            $this->admin_info('1. Hide mandatory system fields by assigning values:  ', $this->contact_presets);
            $this->admin_info('2. Custom Fields  <br><br>   Available fields:   ' . implode(array_keys($this->rendered_cf), ' ,  ') . '  <br><br>   Positions:', $this->custom_positions);
            foreach ($this->form->getFieldsets() as $fs) {
                if ($fs->name != 'captcha') {
                    echo $this->html_4_fieldset($fs);
                }
            }
            echo $this->rendered_captcha;
            if (!$this->rendered_captcha) {$this->admin_info('CAPTCHA -- not enabled');}
        }
    
        public function __construct($form, $captchaEnabled)
        {
            $this->form = $form;
            $this->captchaEnabled = $captchaEnabled;
            foreach ($this->form->getFieldsets() as $current_set) {
                if ($this->is_customfield_set($current_set->name)) { // store custom fields (as strings) to rendered_cf[]
                    foreach ($this->form->getFieldset($current_set->name) as $field) {
                        $name_4_rendered_cf = trim(str_replace("jform_com_fields", "", str_replace(["]", "-", "["], ["", "_", "_"], $field->name)), '_');
                        $this->rendered_cf[$name_4_rendered_cf] = $field->renderField();
                    }
                } else if ('captcha' == $current_set->name) { // store captcha-Fieldset (as string) to rendered_captcha
                    $this->rendered_captcha = $this->captchaEnabled ? $this->html_4_fieldset($current_set, $this->form->getFieldset($current_set->name)) : '';
                }
            }
    
        }
    
    }


Michael Barg




Fehler melden

Bitte beachte unsere Datenschutzerklärung

Alle mit * markierten Felder werden benötigt um eine E-Mail zu senden.