Wiki source code of Extension Point Tutorial

Last modified by Vincent Massol on 2023/10/10

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc start="2"/}}
3 {{/box}}
4
5 == Introduction to Interface Extensions and Extension Points ==
6
7 User Interface Extensions (abbreviated as UIX) are used in order to provide a way to alter the content of existing interface elements. This functionality was added in version 4.2 and is documented in the [[UI Extension Module>>extensions:Extension.UIExtension Module]].
8
9 The main use case Interface Extensions try to fix is the need for applications (like Blog, Watchlist, etc.) to insert custom content in already existing interface components (like panels, menus, layout, etc.).
10
11 Let's take an example: We developed an application called 'Hello World' and we want to provide a link to it inside the 'Applications' panel.
12 There are two questions that need to be answered:
13
14 * where we insert? - this is the Extension Point (UIXP)
15 * what we insert? - this is the UI Extension (UIX)
16
17 === About Extension Points ===
18
19 Extensions Points (UIXP) specify where the new content is about to be inserted. Is like a hoof defined in the interface where Interface Extensions are gathered.
20 For our example, we need to know the extension point ID for the 'Applications' panel. That is '##org.xwiki.platform.panels.Applications##'.
21
22 There is a list of [[available extension points>>Documentation.DevGuide.ExtensionPoint.WebHome]] where we can add functionality, but we can also manually define new extension points.
23
24 === About Interface Extensions ===
25
26 All the UIXs provided for a given Extension Point are displayed in that location.
27 For our example, if we want to add a new link in the 'Applications' panel, we need to create a new UIX that uses the '##org.xwiki.platform.panels.Applications##' extension point. UIXs are stored as standard XObjects, instances of ##XWiki.UIExtensionClass##. For our UIX we will need to provide the label, target and icon parameters, in order to be properly displayed in the panel.
28
29 Read the documentation on [[how to add an UIX>>extensions:Extension.UIExtension Module||anchor="HWritingasimpleUIExtension"]] inside the 'Applications' panel.
30
31 == Adding your own Extension Point ==
32
33 Your UIExtension can define its own extension points, where other extensions can plug in. Here is an example of velocity code to include in your extension in order to make it an entry point for others:
34
35 * {{version since="14.0"}}Using the [[##uiextensions## macro>>https://extensions.xwiki.org/xwiki/bin/view/Extension/Extension%20Module/UI%20Extensions%20Macro/]]:
36
37 {{code}}
38 {{uiextensions extensionPoint="my.new.extension.point"/}}
39 {{/code}}{{/version}}
40 * In Velocity:(((
41 {{code language="velocity"}}
42 {{velocity}}
43 #foreach ($extension in $services.uix.getExtensions("my.new.extension.point"))
44 {{html clean=false}}$services.rendering.render($extension.execute(), 'html/5.0'){{/html}}
45 #end
46 {{/velocity}}
47 {{/code}}
48 )))
49 * Display the parameters:(((
50 {{code language="velocity"}}
51 {{velocity}}
52 #set ($extensions = $services.uix.getExtensions('my.new.extension.point', {'sortById' : ''}))
53 #foreach ($extension in $extensions)
54 $services.rendering.escape($extension.parameters.label, 'xwiki/2.1')
55 #end
56 {{/velocity}}
57 {{/code}}
58 )))
59 * Test the parameter for a particular value:(((
60 {{code language="velocity"}}
61 {{velocity}}
62 #foreach ($extension in $services.uix.getExtensions("my.new.extension.point"))
63 #if ($extension.getParameters().get('parameter_name') == 'expected_value')
64
65 {{html clean=false}}$services.rendering.render($extension.execute(), 'html/5.0'){{/html}}
66 #end
67 #end
68 {{/velocity}}
69 {{/code}}
70 )))
71
72 == Disabling some Extension Points ==
73
74 Until we fix [[XWIKI-13076>>http://jira.xwiki.org/browse/XWIKI-13076]], there is no 'clean' way to disable an UIX.
75
76 In order to disable a page having an ##XWiki.UIExtensionClass## object you can delete the page, delete the object, or use an invalid extension point ID.
77
78 For example, if you would want to remove "User Index" from the Drawer, you should:
79
80 * Identify the extension point used by the [[Drawer>>extensions:Extension.Flamingo Skin||anchor="HDrawer"]]. In our case this is ##[[org.xwiki.plaftorm.drawer>>xwiki:Documentation.DevGuide.ExtensionPoint.DrawerItem]]##.
81 * Identify all the pages that use this extension point. For this, you can use [[Search>>extensions:Extension.Solr Search Application]] to filter for documents that have ##Object Type## = ##UI Extension##. Make sure you have the "Display Hidden Pages" preference set to true, from the [[User Profile>>extensions:Extension.User Profile Application||anchor="HEditPreferences"]].
82 * You will get a list of such pages, like ##Applications.WebHome##, ##XWiki.UserIndexUIX##, ##XWiki.DocumentIndexUIX##, etc.
83 * Go to the ##XWiki.UserIndexUIX## page and from the [[Object Editor>>xwiki:Documentation.UserGuide.Features.PageEditing||anchor="HObjectseditingmode"]] change the “Extension Point ID" value from ##org.xwiki.plaftorm.drawer## ID to something else.
84 * Save the page and the Drawer entry should disappear.
85
86 In case you want to have conditional display for some entries, like showing "User Index" just for registered users, the ID needs to remain the valid one, but [[Velocity>>extensions:Extension.Velocity Macro]] can be used to write conditions in the ##Extension Content## property.

Get Connected