Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagephp
<?php
	$tabs = array(
		array('name'=>"Settings", 'attributes'=>array('href'=>$this->base_uri . "widget/my_plugin/client_main/settings/")),
		array('name'=>"Manage", 'current'=>true, 'attributes'=>array('href'=>$this->base_uri . "widget/my_plugin/client_main/manage/"))
	);

	$this->Widget->clear();
	$this->Widget->setTabs($tabs);    
	$this->Widget->create("My Widget Title", true));

	#
	# TODO: Place widget contents here
	# 

	$this->Widget->end();
?>

 


Links may be used in place of tabs, and may accompany link buttons. Links are displayed at the top left of a widget while link buttons are displayed at the top right. Links may also specify an "ajax" class to allow the widget content to be replaced via AJAX content from the specified href.

Code Block
languagephp
<?php
	$links = array(
		array('name'=>"Settings", 'current'=>true, 'attributes'=>array('href'=>$this->base_uri . "widget/my_plugin/client_main/settings/", 'class'=>"ajax")),
		array('name'=>"Manage", 'attributes'=>array('href'=>$this->base_uri . "widget/my_plugin/client_main/manage/", 'class'=>"ajax"))
	);
	$link_buttons = array(
		array('name'=>"Add Setting", 'attributes'=>array("href"=>$this->Html->safe($this->base_uri . "widget/my_plugin/client_main/settings/add/")))
	);
        
	$this->Widget->clear();
	$this->Widget->setLinks($links);
	$this->Widget->setLinkButtons($link_buttons);
	$this->Widget->create("My Widget Title", true));

	#
	# TODO: Place widget contents here
	# 

	$this->Widget->end();
?>

Filters

As of v4.10.0 it is possible to add filters to widgets, these can be used to filter the data on a table or any information presented within the widget. The selected filters will be posted and their values will be available from the $this->post variable in the controller.

The setFilters() method accepts a few parameters, described below:

  • $filters - An instance of InputFields, containing the fields display in the widget form.
  • $uri - The URI where the form fields will be submitted. 
  • $show_filters - Wether to show the filtering form on page load, false by default.

The list of input fields can be defined within the controller using a private method, These are defined in the same way as the input fields on Modules.

Code Block
languagephp
<?php
use Blesta\Core\Util\Input\Fields\InputFields;

class MyController extends MyPluginController {
	...

	public function myWidget()
	{
		// Set filters from post input
        $post_filters = [];
        if (isset($this->post['filters'])) {
            $post_filters = $this->post['filters'];
            unset($this->post['filters']);
        }

		...
		
		// Set the input field filters for the widget
        $filters = $this->getFilters($post_filters);
        $this->set('filters', $filters);
        $this->set('filter_vars', $post_filters);
	}

	private function getFilters(array $vars)
	{
		$filters = new InputFields();

		// Create "Service Name" label
        $service_name = $filters->label('Service Name', 'service_name');
		// Create "filters[service_name]" input field
        $service_name->attach(
            $filters->fieldText(
                'filters[service_name]',
                isset($vars['service_name']) ? $vars['service_name'] : null, // Pre-populate the field if it is defined in $vars
                [
                    'id' => 'text_input',
                    'class' => 'form-control',
                    'placeholder' => 'Service Name'
                ]
            )
        );
        $filters->setField($service_name);

		// Set control panels list
		$control_panels = [
			'cpanel' => 'cPanel',
			'plesk' => 'Plesk'
		];
		// Create "Control Panel" label
        $control_panel = $filters->label('Control Panel', 'control_panel');
		// Create "filters[control_panel]" select field
        $control_panel->attach(
            $filters->fieldSelect(
                'filters[control_panel]',
                $control_panels,
                isset($vars['control_panel']) ? $vars['control_panel'] : null,
                ['id' => 'control_panel', 'class' => 'form-control']
            )
        );
        $filters->setField($control_panel);

		// You can set HTML code as well!
        $fields->setHtml('
            <script type="text/javascript">alert("It Works!");</script>
        ');

		return $filters;
	}
}

After defining the filters in the controller, they can be added to the Widget from the view.

Code Block
languagephp
title/plugins/my_plugin/views/default/client_mywidget.pdt
linenumberstrue
<?php
$this->Widget->clear();
$this->Widget->create();
$this->Widget->setFilters($filters, $this->base_uri . 'plugin/my_plugin/mywidget/', !empty($filter_vars));

#
# TODO: Place widget contents here
#

$this->Widget->end();
?>

Widgets and Plugins

Widgets can appear anywhere within your plugin, but Blesta reserves a few Plugin Actions especially for rendering widgets. These actions will automatically load your widgets to display in various locations (if the Staff member has enabled the widget to appear). This allows plugins to display custom data/forms on pages that plugins can't otherwise access. For more information on registering your widgets with these actions see the Plugin Actions section of this manual.

...