Last active
September 9, 2019 08:11
-
-
Save renekreijveld/04418458a8b3e86ab3d7d17a5493c2a6 to your computer and use it in GitHub Desktop.
Custom form field to create a select list with year numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @version 1.0.0 | |
* @author Rene Kreijveld <[email protected]> | |
* @copyright 2019 DSD Business Internet | |
* @license GNU General Public License version 2 or later; see LICENSE.txt | |
* | |
* Example form xml definition: | |
* <field name="established" | |
* type="yearlist" | |
* start="1950" | |
* end="9999" | |
* defaultvalue="before 1950" | |
* defaulttext="Before 1950" | |
* label="Year of establishment" | |
* description="Choose the year this company was established" | |
* /> | |
*/ | |
defined('JPATH_BASE') or die; | |
use \Joomla\CMS\HTML\HTMLHelper; | |
/** | |
* Supports an HTML select list of years | |
* | |
* @since 1.6 | |
*/ | |
class JFormFieldYearList extends \Joomla\CMS\Form\FormField | |
{ | |
/** | |
* The form field type. | |
* | |
* @var string | |
* @since 1.6 | |
*/ | |
protected $type = 'yearlist'; | |
/** | |
* Method to get the field input markup. | |
* | |
* @return string The field input markup. | |
* | |
* @since 1.6 | |
*/ | |
protected function getInput() | |
{ | |
$this->required = (string) $this->getAttribute('required', 0); | |
// the year where the list starts with | |
$this->start = (int) $this->getAttribute('start', 1950); | |
// the year where the list ends with | |
// if end = 9999, the current year will be used | |
$this->end = (string) $this->getAttribute('end', 9999); | |
// default value to be used at the start of the list | |
// degfault text to be used at the start if the list | |
$this->defaultvalue = (string) $this->getAttribute('defaultvalue'); | |
$this->defaulttext = (string) $this->getAttribute('defaulttext'); | |
$input_options = 'class="' . $this->getAttribute('class') . '"'; | |
if ($this->required === "true") | |
{ | |
$input_options .= 'required="required"'; | |
} | |
// array that holds the options for the select list | |
$options = array(); | |
// do we need to add a default value at the start of the list? | |
if ($this->defaultvalue) | |
{ | |
$options[] = HTMLHelper::_('select.option', $this->defaultvalue, $this->defaulttext); | |
} | |
if ($this->end === 9999) | |
{ | |
$this->end = (int) date('Y'); | |
} | |
// build list | |
for ($i = $this->start; $i <= $this->end; $i++) | |
{ | |
$options[] = HTMLHelper::_('select.option', (string) $i, (string) $i); | |
} | |
// create HTML for the select list | |
$html = HTMLHelper::_('select.genericlist', $options, $this->name, $input_options, 'value', 'text', $this->value, $this->id); | |
return $html; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment