Skip to content

Commit

Permalink
Merge pull request #12 from Syberon/fix-button-icons
Browse files Browse the repository at this point in the history
Add custom icon to FormButton element
  • Loading branch information
neilime authored Feb 9, 2019
2 parents 71feda8 + 0a36522 commit 18cf759
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 18cf759

Please sign in to comment.