• Document Up to Date
  • Updated On 4.0.1

Crafter Studio Toolbar Plugin Example

The toolbar is a fixed element at the top of Studio. It provides contextual workflow and other options relative to the page you are looking at, content you have selected or tool you are using.

Studio Toolbar

Let’s take a look at an example of creating a toolbar plugin in Studio using a project called My Editorial created using the Website Editorial blueprint.

  1. The first thing we have to do is to create the folder structure where we will be placing the JS file for our toolbar project plugin. We’ll follow the convention listed in UI Plugin Directory Structure. For our example, CATEGORY is toolbar and the NAME is test-toolbar

    In a local folder, create the descriptor file for your project plugin craftercms-plugin.yaml with the plugin.id set to org.craftercms.plugin.exampletoolbar, then create the following folder structure:

    Toolbar Plugin Directory Structure
    <plugin-folder>/
      craftercms-plugin.yaml
      authoring/
        static-assets/
          plugins/
            org/
              craftercms/
                plugin/
                  exampletoolbar/
                    toolbar/
                      test-toolbar/
    

    We will be placing the JS file implementing the toolbar project plugin under the test-toolbar folder For our example, the <plugin-folder> is located here: /users/myuser/myplugins/toolbar-plugin

  2. We’ll create the JavaScript file for our plugin by following the instructions in the plugin example here which will generate the index.js file.

    Inside the test-toolbar folder, create two empty files, index.css and script.js, and place the index.js file in it.

  3. To setup our toolbar project plugin to be automatically wired in the corresponding configuration file in Studio (which for a toolbar tool, is the User Interface Configuration file) during the installation, add the following to your craftercms-plugin.yaml descriptor file

    craftercms-plugin.yaml
     1installation:
     2  - type: preview-app
     3    parentXpath: //widget[@id='craftercms.components.PreviewToolbar']
     4    elementXpath: //plugin[@id='org.craftercms.sampleToolbarPlugin.components.reactComponent']
     5    element:
     6      name: configuration
     7      children:
     8      - name: rightSection
     9        children:
    10        - name: widgets
    11          children:
    12          - name: widget
    13            attributes:
    14            - name: id
    15              value: org.craftercms.sampleToolbarPlugin.components.reactComponent
    16            children:
    17            - name: plugin
    18              attributes:
    19              - name: id
    20                value: org.craftercms.plugin.exampletoolbar
    21              - name: type
    22                value: toolbar
    23              - name: name
    24                value: test-toolbar
    25              - name: file
    26                value: index.js
    

    Remember to use the same value used in plugin.id (found at the top of the descriptor file) for the installation section plugin.id which for our example is org.craftercms.plugin

  4. After placing your plugin files and setting up auto-wiring, the project plugin may now be installed for testing/debugging using the crafter-cli command copy-plugin.

    Toolbar project plugin directory/files

    When running a crafter-cli command, the connection to CrafterCMS needs to be setup via the add-environment command. Once the connection has been established, we can now install the plugin to the project my-editorial by running the following:

    ./crafter-cli copy-plugin -e local -s my-editorial --path /users/myuser/myplugins/toolbar-plugin
    

  5. Let’s take a look at our plugin in action by refreshing your browser:

    Toolbar project plugin in action

    Here’s the auto-wired section in the configuration after installing the plugin:

     1<siteUi>
     2...
     3  <widget id="craftercms.components.PreviewToolbar">
     4    <configuration>
     5      <leftSection>
     6        <widgets>
     7          <widget id="craftercms.components.SiteSwitcherSelect"/>
     8          <widget id="craftercms.components.QuickCreate"/>
     9        </widgets>
    10      </leftSection>
    11      <middleSection>
    12        <widgets>
    13          <widget id="craftercms.components.PreviewAddressBar"/>
    14        </widgets>
    15      </middleSection>
    16      <rightSection>
    17        <widgets>
    18          <widget id="craftercms.components.EditModesSwitcher"/>
    19          <widget id="craftercms.components.PublishingStatusButton">
    20            <configuration>
    21              <variant>icon</variant>
    22            </configuration>
    23          </widget>
    24          <widget id="craftercms.components.WidgetDialogIconButton">
    25            <configuration>
    26              <title id="words.search" defaultMessage="Search"/>
    27              <icon id="@mui/icons-material/SearchRounded"/>
    28              <widget id="craftercms.components.EmbeddedSearchIframe"/>
    29            </configuration>
    30          </widget>
    31          <widget id="org.craftercms.sampleToolbarPlugin.components.reactComponent">
    32            <plugin id="org.craftercms.plugin"
    33                    type="toolbar"
    34                    name="test-toolbar"
    35                    file="index.js"/>
    36          </widget>
    37        </widgets>
    38      </rightSection>
    39    </configuration>
    40  </widget>
    41
    42  ...