Skip to content

Commit

Permalink
Add custom icon to FormButton element
Browse files Browse the repository at this point in the history
Since in this module removed 'fontAwesome' and 'glyphicon' view helpers
there are problem with icon assigning to buttons appeared.

In this commit i added a support to assign an icon to FormButton element
via direct tag providing in element config.

Supported 2 variants:

1. By providing a scalar value containing tag
'element' => [
    'name' => 'button',
    'type' => Button::class,
    'options' => [
        'label'	=>'button',
	'icon' 	=> '<i class="fas fa-cogs"></i>'
    ]
]

2. By providing an array:
'element' => [
    'name' => 'button',
    'type' => Button::class,
    'options' => [
        'label' => 'button',
	'icon'  => [
	    'icon'     => '<i class="fas-fa-cogs"></i>',
	    'position' => 'prepend'
	]
    ]
]
  • Loading branch information
Андрей Чешуин authored and Андрей Чешуин committed Feb 8, 2019
1 parent 71feda8 commit 0a36522
Showing 1 changed file with 45 additions and 20 deletions.
65 changes: 45 additions & 20 deletions src/TwbsHelper/Form/View/Helper/FormButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public function render(ElementInterface $oElement, $sButtonContent = null)
$sIconHelperMethod = 'glyphicon';
} elseif (null !== ($aIconOptions = $oElement->getOption('fontAwesome'))) {
$sIconHelperMethod = 'fontAwesome';
} elseif (null !== ($aIconOptions = $oElement->getOption('icon'))) {
// Direct icon tag provided, disable helper
$sIconHelperMethod = null;
}

// Define button content
Expand Down Expand Up @@ -130,7 +133,7 @@ public function render(ElementInterface $oElement, $sButtonContent = null)
// Validate icon options type
if (! is_array($aIconOptions)) {
throw new LogicException(sprintf(
'"glyphicon" and "fontAwesome" button option expects a scalar value or an array, "%s" given',
'"glyphicon" and "fontAwesome" or "icon" button option expects a scalar value or an array, "%s" given',
is_object($aIconOptions) ? get_class($aIconOptions) : gettype($aIconOptions)
));
}
Expand All @@ -150,48 +153,70 @@ public function render(ElementInterface $oElement, $sButtonContent = null)
// Validate icon option type
if (! is_scalar($icon)) {
throw new LogicException(sprintf(
'Glyphicon and fontAwesome "icon" option expects a scalar value, "%s" given',
'Direct icon, Glyphicon and fontAwesome "icon" option expects a scalar value, "%s" given',
is_object($icon) ? get_class($icon) : gettype($icon)
));

// Validate icon position option type
} elseif (! is_string($position)) {
throw new LogicException(sprintf(
'Glyphicon and fontAwesome "position" option expects a string, "%s" given',
'Direct icon, Glyphicon and fontAwesome "position" option expects a string, "%s" given',
is_object($position) ? get_class($position) : gettype($position)
));

// Validate icon position option value
} elseif ($position !== self::ICON_PREPEND && $position !== self::ICON_APPEND) {
throw new LogicException(sprintf(
'Glyphicon and fontAwesome "position" option allows "'.self::ICON_PREPEND.'" or "'.self::ICON_APPEND.'", "%s" given',
'Direct icon, Glyphicon and fontAwesome "position" option allows "'.self::ICON_PREPEND.'" or "'.self::ICON_APPEND.'", "%s" given',
is_object($position) ? get_class($position) : gettype($position)
));
}

// Button content provided
if ($sButtonContent) {
// Prepend icon to button content
if ($position === self::ICON_PREPEND) {
$sButtonContent = $this->getView()->{$sIconHelperMethod}(
$icon,
isset($aIconOptions['attributes']) ? $aIconOptions['attributes'] : null
) . " {$sButtonContent}";
// fontAwesome or glyphicon provided
if ($sIconHelperMethod) {
// Button content provided
if ($sButtonContent) {
// Prepend icon to button content
if ($position === self::ICON_PREPEND) {
$sButtonContent = $this->getView()->{$sIconHelperMethod}(
$icon,
isset($aIconOptions['attributes']) ? $aIconOptions['attributes'] : null
) . " {$sButtonContent}";

// Append icon to button content
} else {
$sButtonContent .= ' ' . $this->getView()->{$sIconHelperMethod}(
$icon,
isset($aIconOptions['attributes']) ? $aIconOptions['attributes'] : null
);
}

// Append icon to button content
// No button content provided, set icon as button content
} else {
$sButtonContent .= ' ' . $this->getView()->{$sIconHelperMethod}(
$sButtonContent = $this->getView()->{$sIconHelperMethod}(
$icon,
isset($aIconOptions['attributes']) ? $aIconOptions['attributes'] : null
);
}

// No button content provided, set icon as button content
} else {
$sButtonContent = $this->getView()->{$sIconHelperMethod}(
$icon,
isset($aIconOptions['attributes']) ? $aIconOptions['attributes'] : null
);
// Direct icon tag provided
}
else {
// Button content provided
if ($sButtonContent) {
// Prepend predefined icon to button content
if ($position === self::ICON_PREPEND) {
$sButtonContent = $aIconOptions['icon'] . ' ' . $sButtonContent;

// Append icon to button content
} else {
$sButtonContent .= ' ' . $aIconOptions['icon'];
}

// No button content provided, set icon as button content
} else {
$sButtonContent = $aIconOptions['icon'];
}
}
}

Expand Down

0 comments on commit 0a36522

Please sign in to comment.