Diem comes with a set of built-in widgets. They allow to generate valid HTML and require no technical knowledge.
To add a widget on a page, click the "Add" button on the front application lower tool bar.
These widgets add content on the page.
Add a title on the page.
Add a link on the page.
Add a text on the page. A text may be composed with a title, a body and a media.
Add an image on the page.
These widgets add navigation elements on the page.
Add an automatic breadcrumb on the page.
Allow to create and manage a menu by dropping pages from the PAGES panel.
Advanced widgets to enhance the site.
Add an internal search engine form to allow users to performs searches on the site.
Customize the search form template
Copy dmFrontPlugin/modules/dmWidget/templates/_dmWidgetAdvancedSearchForm.php
to apps/front/modules/dmWidget/templates/_dmWidgetAdvancedSearchForm.php
then modify it to match your needs.
Displays the search results for the current search. Should be added on the main/search page.
Customize the search results template
Copy dmFrontPlugin/modules/dmWidget/templates/_dmWidgetAdvancedSearchResults.php
to apps/front/modules/dmWidget/templates/_dmWidgetAdvancedSearchResults.php
then modify it to match your needs.
It's as simple as adding a component in the config/dm/modules.yml file.
In this example, we will create a simple widget that says hello.
config/dm/modules.yml:
Content:
Global:
main:
components:
hello: # declare a new component for the main module
Then go back to the front application and click the "Clear cache" button. It will generate the main/hello component and template in apps/front/modules/main.
If the files are not created, see the Troubleshooting.
Now we can click the front lower tool bar "Add" button, and drag & drop the new main/hello widget somewhere on the page.
Of course, it does nothing yet.
To write the controller, edit the file
apps/front/modules/main/actions/components.class.php:
class mainComponents extends myFrontModuleComponents { public function executeHello() { $this->name = $this->getRequest()->getParameter('name', 'anonymous'); } }
Then we will create the template, in
apps/front/modules/main/templates/_hello.php
echo 'Hello '.escape($name);
Now call your page with a name get parameter like "/mypage?name=jack".
For more info on specific widget creation, see the config/dm/modules.yml documentation.
All widgets can be placed on the page with a drag&drop from the Add menu; but you also can include them programmatically from a template. Such widgets will not be created in the database, and can not be modified from the front edition interface.
The basic syntax is
echo dm_get_widget($module, $action, $params);
For example, we will include your main/header widget, give it a CSS class and pass a param1 parameter to the component.
echo dm_get_widget('main', 'header', array( 'css_class' => 'my_custom_class' 'param1' => 'value1' //pass a parameter to the main/header component ));
Next example is to include your foo/show widget, and to pass a recordId parameter to the component.
echo dm_get_widget('foo', 'show', array( 'recordId' => $id //pass a recordId to the foo/show component ));
Your list widgets have 2 required parameters, to configure the query.
// include a list widget echo dm_get_widget('article', 'list', array( 'orderField' => 'title', //required 'orderType' => 'asc', //required 'maxPerPage' => 10 //optional 'navTop' => true //optional 'navBottom' => false //optional ));
For example, we will include the Diem search form widget and give it a CSS class.
echo dm_get_widget('dmWidgetSearch', 'form', array( 'css_class' => 'my_custom_class' ));
Some widgets require parameters, like the bread crumb widget.
echo dm_get_widget('dmWidgetNavigation', 'breadCrumb', array( 'separator' => '>', 'includeCurrent' => true ));
To know the required parameters of this built-in widget, see the dmWidgetNavigationBreadCrumbView class.
In this advanced section, we will learn how to customize built-in widgets.
As an example, let's suppose we want to modify the menu widget in order to give it an HTML ID.
We need to modify the menu widget form, to have an ID field.
We need to modify the menu widget view, to wrap the menu HTML into a div that has the specified ID.
You can replace every widget form and view classes in
config/dm/widget_types.yml
dmWidgetNavigation:
menu:
form_class: myWidgetMenuForm
view_class: myWidgetMenuView
This tells Diem to use myWidgetMenuForm as the menu form class, and myWidgetMenuView as the menu view class. See the whole list of overridable widget classes in dmCorePlugin/config/dm/widget_types.yml.
Create your menu widget form class:
lib/myWidgetMenuForm.php
class myWidgetMenuForm extends dmWidgetNavigationMenuForm { public function configure() { parent::configure(); $this->widgetSchema['wrapperId'] = new sfWidgetFormInputText(array( 'label' => 'Wrapper ID' )); $this->validatorSchema['wrapperId'] = new sfValidatorString(array('required' => false)); } }
Nothing special here, it's just about extending a symfony form to add a field.
Some widget forms are quite complex and need a template to be displayed. This is the case with the menu widget form. So we need to copy the template from
dmFrontPlugin/modules/dmWidget/templates/forms/dmWidgetNavigationMenu.php
to
apps/front/modules/dmWidget/templates/forms/dmWidgetNavigationMenu.php.
Then add the wrapperId field somewhere, for example at the end of the template, in the Advanced tab:
// ... template code _tag('div#'.$baseTabId.'_advanced', _tag('ul.dm_form_elements', $form['cssClass']->renderRow(). $form['ulClass']->renderRow(). $form['liClass']->renderRow(). $form['wrapperId']->renderRow() ) ), _close('div'); //div.dm_tabbed_form
You can tell the form to use a different template by overriding the "renderContent" method of your myWidgetMenuForm class
class myWidgetMenuForm extends dmWidgetNavigationMenuForm { protected function renderContent($attributes) { return $this->getHelper()->renderPartial('dmWidget', 'forms/myWidgetNavigationMenu', array( 'form' => $this, 'items' => $this->getValueOrDefault('items'), 'baseTabId' => 'dm_widget_menu_'.$this->dmWidget->get('id') )); }
Create your menu widget view class:
lib/myWidgetMenuView.php
class myWidgetMenuView extends dmWidgetNavigationMenuView { protected function doRender() { // use cache if available if ($this->isCachable() && $cache = $this->getCache()) { return $cache; } // get the view vars processed from the form $vars = $this->getViewVars(); if(isset($vars['wrapperId'])) { $html = $this->getHelper()->tag('div#'.$vars['wrapperId'], $vars['menu']->render()); } else { $html = $vars['menu']->render(); } // cache the HTML if ($this->isCachable()) { $this->setCache($html); } return $html; } }
By overriding the doRender() method, you can change the way the menu is rendered. Here, we wrap the menu with a div if the wrapperId var exists.
Apply all these changes by clicking the refresh button on the front tool bar, then enjoy your custom widget:
By using config/dm/widget_types.yml you can also create new built-in widgets with complex forms and views.
Please share them by packaging them in a plugin!
Questions and Feedback
If you need support or have a technical question, you can
The documentation is hosted on GitHub. Feel free to submit issues and patches!