Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Commit

Permalink
Now the inputs and buttons classes can be configured throughout the F…
Browse files Browse the repository at this point in the history
…ormHelper `$config` array
elboletaire committed Sep 27, 2014
1 parent ae54df3 commit e3cbc3f
Showing 2 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Controller/Component/BootstrapComponent.php
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ private function replaceFlashMessage()
}

/**
* Returns the flash class name depending on it's alias
* Returns the flash class name depending on its alias
*
* @param string $name The alias name.
* @return string Equivalent class name. If it does not exist will return $name.
30 changes: 23 additions & 7 deletions src/View/Helper/FormHelper.php
Original file line number Diff line number Diff line change
@@ -20,10 +20,19 @@ class FormHelper extends Helper\FormHelper
// Load custom templates
public function __construct(View $View, array $config = [])
{
// Merge parent FormHelper defaults with this FormHelper ones
$this->_defaultConfig = array_merge([
'input_class' => 'form-control',
'button_class' => 'btn-default',
'force_class_btn' => true // after enabled can be disabled on runtime,
// passing `'btnClass' => false`
], $this->_defaultConfig);

// Force templateClass
$config['templateClass'] = 'Bootstrap\View\StringTemplate';

parent::__construct($View, $config);

$form_templates = Plugin::path('Bootstrap') . 'config' . DS;
$form_templates = realpath($form_templates . 'forms.php');
$form_templates = function() use ($form_templates) {
@@ -36,13 +45,18 @@ public function __construct(View $View, array $config = [])

/**
* {@inheritdoc}
*
* Add .btn and .btn-default classes to buttons
*
* @var string $title
* @var array $options
*/
// Add btn and btn-default classes to buttons
public function button($title, array $options = [])
{
$options += ['class' => 'btn-default'];
if (!isset($options['btnClass']) || (isset($options['btnClass']) && $options['btnClass'])) {
// always add .btn class
$options += ['class' => $this->config('button_class')];
$is_btn_enabled = !isset($options['btnClass']) || (isset($options['btnClass']) && $options['btnClass']);
// always add .btn class
if ($this->config('force_class_btn') && $is_btn_enabled) {
$options = $this->addClass($options, 'btn');
}
return parent::button($title, $options);
@@ -75,7 +89,7 @@ public function __call($method, $params)
throw new Error\Exception(sprintf('Missing field name for FormHelper::%s', $method));
}
$class = [
'class' => 'form-control'
'class' => $this->config('input_class')
];
if (isset($params[1])) {
$params[1] += $class;
@@ -91,7 +105,9 @@ public function __call($method, $params)
protected function _getInput($fieldName, $options)
{
if (in_array($options['type'], array('select', 'url', 'text', 'textarea'))) {
$options = $this->addClass($options, 'form-control');
foreach (explode(' ', $this->config('input_class')) as $class) {
$options = $this->addClass($options, $class);
}
}
return parent::_getInput($fieldName, $options);
}
@@ -103,7 +119,7 @@ protected function _getInput($fieldName, $options)
*/
private function addDatetimeClasses(array &$options = [], $periods = ['year', 'month', 'day', 'hour', 'minute'])
{
$class = ['class' => 'form-control'];
$class = ['class' => $this->config('input_class')];
foreach ($periods as $period) {
if (empty($options[$period])) {
$options[$period] = [];

0 comments on commit e3cbc3f

Please sign in to comment.