ModalHelper
extends Helper
in package
uses
ClassTrait, EasyIconTrait, StringTemplateTrait
Modal helper library.
Automatic generation of Bootstrap HTML modals.
Table of Contents
Properties
- $easyIcon : bool
- Set to false to disable easy icon processing.
- $helpers : array<string|int, mixed>
- Other helpers used by ModalHelper.
- $Html : HtmlHelper
- $_current : string
- Current part of the modal(`null`, `'header'`, `'body'`, `'footer'`).
- $_currentId : mixed
- Current id of the modal.
- $_defaultConfig : array<string|int, mixed>
- Default configuration for the ModalHelper.
Methods
- addClass() : array<string|int, mixed>
- Adds the given class to the element options.
- body() : string
- Create or open a modal body.
- create() : string
- Open a modal
- end() : string
- Closes a modal, cleans part that have not been closed correctly and optionaly adds a footer with buttons to the modal.
- footer() : string
- Create or open a modal footer.
- header() : string
- Create or open a modal header.
- _addButtonClasses() : array<string|int, mixed>
- Add classes to options according to the default values of bootstrap-type and bootstrap-size for button (see configuration).
- _cleanCurrent() : string
- Cleans the current modal part and return necessary HTML closing elements.
- _createBody() : string
- Create or open a modal body.
- _createFooter() : string
- Create or open a modal footer.
- _createHeader() : string
- Create or open a modal header.
- _easyIconOption() : array<string|int, mixed>
- Remove the `easyIcon` option from the given array and return it together with the array.
- _injectIcon() : string
- Inject icon into the given string.
- _isAssociativeArray() : bool
- Check weither the specified array is associative or not.
- _makeIcon() : string
- Try to convert the specified string to a bootstrap icon. The string is converted if it matches a format `i:icon-name` (leading and trailing spaces or ignored) and if easy-icon is activated.
- _part() : string
- Cleans the current modal part, create a new ones with the given content, and update the internal `_current` variable if necessary.
Properties
$easyIcon
Set to false to disable easy icon processing.
public
bool
$easyIcon
= true
$helpers
Other helpers used by ModalHelper.
public
array<string|int, mixed>
$helpers
= ["Html"]
$Html
public
HtmlHelper
$Html
$_current
Current part of the modal(`null`, `'header'`, `'body'`, `'footer'`).
protected
string
$_current
= null
$_currentId
Current id of the modal.
protected
mixed
$_currentId
= null
$_defaultConfig
Default configuration for the ModalHelper.
protected
array<string|int, mixed>
$_defaultConfig
= ["templates" => ["modalStart" => '<div class="modal fade{{attrs.class}}" tabindex="-1" {{attrs}} aria-hidden="true">{{dialogStart}}{{contentStart}}', "modalEnd" => "</div>", "modalDialogStart" => '<div class="modal-dialog{{attrs.class}}"{{attrs}}>', "modalDialogEnd" => "</div>", "modalContentStart" => '<div class="modal-content{{attrs.class}}"{{attrs}}>', "modalContentEnd" => "</div>", "headerStart" => '<div class="modal-header{{attrs.class}}"{{attrs}}>', "headerEnd" => "</div>", "modalHeaderCloseButton" => '<button type="button" class="btn-close {{attrs.class}}" data-bs-dismiss="modal" aria-label="{{label}}"{{attrs}}></button>', "modalHeaderCloseContent" => '<span aria-hidden="true">×</span>', "modalTitle" => '<h5 class="modal-title{{attrs.class}}"{{attrs}}>{{content}}</h5>', "bodyStart" => '<div class="modal-body{{attrs.class}}"{{attrs}}>', "bodyEnd" => "</div>", "footerStart" => '<div class="modal-footer{{attrs.class}}"{{attrs}}>', "footerEnd" => "</div>", "modalFooterCloseButton" => '<button type="button" class="btn btn-primary{{attrs.class}}" data-bs-dismiss="modal"{{attrs}}>{{content}}</button>'], "templateClass" => "Bootstrap\\View\\EnhancedStringTemplate"]
Methods
addClass()
Adds the given class to the element options.
public
addClass([array<string|int, mixed> $options = [] ][, string|array<string|int, mixed> $class = null ][, string $key = "class" ]) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed> = []
-
Array of options/attributes to add a class to.
- $class : string|array<string|int, mixed> = null
-
The class names to be added.
- $key : string = "class"
-
The key to use for class (default to
'class').
Return values
array<string|int, mixed> —Array of options with $key set or modified.
body()
Create or open a modal body.
public
body([array<string|int, mixed>|string|null $info = null ][, array<string|int, mixed> $options = [] ]) : string
If $content is a string, create a modal body using the specified content and
$options.
echo $this->Modal->body('Modal Content', ['class' => 'my-class']);
If $content is null, create a formated opening tag for a modal body using the
specified $options.
echo $this->Modal->body(null, ['class' => 'my-class']);
If $content is an array, used it as $options and create a formated opening tag for
a modal body.
echo $this->Modal->body(['class' => 'my-class']);
Options
templateVarsProvide template variables for thebodyStarttemplate.- Other attributes will be assigned to the modal body element.
Parameters
- $info : array<string|int, mixed>|string|null = null
-
The body content, or
null, or an array of options.$options. - $options : array<string|int, mixed> = []
-
Array of options. See above.
Return values
string —A formated opening tag for the modal body or the complete modal body.
create()
Open a modal
public
create([array<string|int, mixed>|string $title = null ][, array<string|int, mixed> $options = [] ]) : string
If $title is a string, the modal header is created using $title as its
content and default options.
echo $this->Modal->create('My Modal Title');
If the modal header is created, the modal body is automatically opened after
it, except if the body options is specified(see below).
If $title is an array, it is used as $options.
echo $this->Modal->create(['class' => 'my-modal-class']);
Options
bodyIf$titleis a string, set tofalseto not open the body after the panel header. Default istrue.closeSet tofalseto not add a close button to the modal. Default istrue.idIdentifier of the modal. If specified, aaria-labelledbyHTML attribute will be added to the modal and the header will be set accordingly.sizeSize of the modal. Either a shortcut('lg'/'large'/'modal-lg'or ('sm'/'small'/'modal-sm') orfalse(no size specified) or a custom class. Other options will be passed to theHtml::divmethod for creating the outer modal<div>.
Parameters
- $title : array<string|int, mixed>|string = null
-
The modal title or an array of options.
- $options : array<string|int, mixed> = []
-
Array of options. See above.
Return values
string —An HTML string containing opening elements for a modal.
end()
Closes a modal, cleans part that have not been closed correctly and optionaly adds a footer with buttons to the modal.
public
end([array<string|int, mixed>|null $buttons = null ][, array<string|int, mixed> $options = [] ]) : string
If $buttons is not null, the footer() method will be used to create the modal
footer using $buttons and $options:
echo $this->Modal->end([$this->Form->button('Save'), $this->Form->button('Close')]);
Parameters
- $buttons : array<string|int, mixed>|null = null
-
Array of buttons for the
footer()method ornull. - $options : array<string|int, mixed> = []
-
Array of options for the
footer()method.
Return values
string —An HTML string containing closing tags for the modal.
footer()
Create or open a modal footer.
public
footer([string|array<string|int, mixed>|null $buttons = null ][, array<string|int, mixed> $options = [] ]) : string
If $buttons is a string, create a modal footer using the specified content
and $options.
echo $this->Modal->footer('Footer Content', ['class' => 'my-class']);
If $buttons is null, create a formated opening tag for a modal footer using the
specified $options.
echo $this->Modal->footer(null, ['class' => 'my-class']);
If $buttons is an associative array, used it as $options and create a
formated opening tag for a modal footer.
echo $this->Modal->footer(['class' => 'my-class']);
If $buttons is a non-associative array, its elements are glued together to
create the content. This can be used to generate a footer with buttons:
echo $this->Modal->footer([$this->Form->button('Close'), $this->Form->button('Save')]);
Options
templateVarsProvide template variables for thefooterStarttemplate.- Other attributes will be assigned to the modal footer element.
Parameters
- $buttons : string|array<string|int, mixed>|null = null
-
The footer content, or
null, or an array of options. - $options : array<string|int, mixed> = []
-
Array of options. See above.
Return values
string —A formated opening tag for the modal footer or the complete modal footer.
header()
Create or open a modal header.
public
header([string|array<string|int, mixed>|null $info = null ][, array<string|int, mixed> $options = [] ]) : string
If $text is a string, create a modal header using the specified content
and $options.
echo $this->Modal->header('Header Content', ['class' => 'my-class']);
If $text is null, create a formated opening tag for a modal header using the
specified $options.
echo $this->Modal->header(null, ['class' => 'my-class']);
If $text is an array, used it as $options and create a formated opening tag for
a modal header.
echo $this->Modal->header(['class' => 'my-class']);
Options
closeSet tofalseto not add a close button to the modal. Default istrue.templateVarsProvide template variables for theheaderStarttemplate.- Other attributes will be assigned to the modal header element.
Parameters
- $info : string|array<string|int, mixed>|null = null
-
The modal header content, or an array of options.
- $options : array<string|int, mixed> = []
-
Array of options. See above.
Return values
string —A formated opening tag for the modal header or the complete modal header.
_addButtonClasses()
Add classes to options according to the default values of bootstrap-type and bootstrap-size for button (see configuration).
protected
_addButtonClasses(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
-
The initial options with btype and/or size, block values.
Return values
array<string|int, mixed> —The new options with class values (btn, and btn-* according to initial options).
_cleanCurrent()
Cleans the current modal part and return necessary HTML closing elements.
protected
_cleanCurrent() : string
Return values
string —An HTML string containing closing elements.
_createBody()
Create or open a modal body.
protected
_createBody([string $text = null ][, array<string|int, mixed> $options = [] ]) : string
Options
templateVarsProvide template variables for thebodyStarttemplate.- Other attributes will be assigned to the modal body element.
Parameters
- $text : string = null
-
The modal body content, or
nullto only open the body. - $options : array<string|int, mixed> = []
-
Array of options. See above.
Tags
Return values
string —A formated opening tag for the modal body or the complete modal body.
_createFooter()
Create or open a modal footer.
protected
_createFooter([string $content = null ][, array<string|int, mixed> $options = [] ]) : string
If $content is null and the 'close' option(see below) is true, a close
button is created inside the footer.
Options
closeSet totrueto add a close button to the footer if$contentis empty. Default istrue.templateVarsProvide template variables for thefooterStarttemplate.- Other attributes will be assigned to the modal footer element.
Parameters
- $content : string = null
-
The modal footer content, or
nullto only open the footer. - $options : array<string|int, mixed> = []
-
Array of options. See above.
Return values
string —A formated opening tag for the modal footer or the complete modal footer.
_createHeader()
Create or open a modal header.
protected
_createHeader([string $title = null ][, array<string|int, mixed> $options = [] ]) : string
Options
closeSet tofalseto not add a close button to the modal. Default istrue.templateVarsProvide template variables for theheaderStarttemplate.- Other attributes will be assigned to the modal header element.
Parameters
- $title : string = null
-
The modal header content, or null to only open the header.
- $options : array<string|int, mixed> = []
-
Array of options. See above.
Tags
Return values
string —A formated opening tag for the modal header or the complete modal header.
_easyIconOption()
Remove the `easyIcon` option from the given array and return it together with the array.
protected
_easyIconOption(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
-
Array of options from which the easy-icon option should be extracted.
Return values
array<string|int, mixed> —An array containing the options and the easy-icon option.
_injectIcon()
Inject icon into the given string.
protected
_injectIcon(string $title, bool $easyIcon) : string
Parameters
- $title : string
-
Input string where icon should be injected following the easy-icon process.
- $easyIcon : bool
-
Boolean indicating if the easy-icon process should be applied.
Return values
string_isAssociativeArray()
Check weither the specified array is associative or not.
protected
_isAssociativeArray(array<string|int, mixed> $array) : bool
Parameters
- $array : array<string|int, mixed>
-
The array to check.
Return values
bool —true if the array is associative, false otherwize.
_makeIcon()
Try to convert the specified string to a bootstrap icon. The string is converted if it matches a format `i:icon-name` (leading and trailing spaces or ignored) and if easy-icon is activated.
protected
_makeIcon(string $text[, bool &$converted = false ]) : string
Note: This function will currently fail if the Html helper associated with the view is not BootstrapHtmlHelper.
Parameters
- $text : string
-
The string to convert.
- $converted : bool = false
-
If specified, will contains
trueif the text was converted,falseotherwize.
Return values
string —The text after conversion.
_part()
Cleans the current modal part, create a new ones with the given content, and update the internal `_current` variable if necessary.
protected
_part(string $part[, string $content = null ][, array<string|int, mixed> $options = [] ]) : string
Parameters
- $part : string
-
The name of the part(
'header','body','footer'). - $content : string = null
-
The content of the part or
null. - $options : array<string|int, mixed> = []
-
Array of options for the
Html::tagmethod.