wbrowar/adminbar

This package is abandoned and no longer maintained. The author suggests using the wbrowar/craft-admin-bar package instead.

Front-end shortcuts for clients logged into Craft CMS.

Installs: 77 948

Dependents: 2

Suggesters: 0

Security: 0

Stars: 32

Watchers: 2

Forks: 9

Open Issues: 0

Type:craft-plugin

5.1.1 2024-12-21 00:54 UTC

README

Screenshot

Admin Bar is made up of two parts:

  • Admin Bar Component is a standalone web component that can be added to the front end of any project.
  • Admin Bar (this plugin) provides plugin settings, a Twig tag, and checks to see if the user is logged in to determine if the Admin Bar Component should be displayed on the page.

If you are creating a Craft CMS site with a Twig front end, you can use this plugin and its Twig tag to add Admin Bar to your front end.

If you are creating a headless site, or a site that is statically cached, you may not need this plugin, and in that case you can add Admin Bar Component directly into your site’s JavaScript (via a bundler or a <script> tag).

⚠️ Admin Bar’s composer package name has changed from wbrowar/adminbar to wbrowar/craft-admin-bar. Update your composer.json to point to the new package on Packagist.

Requirements

  • Craft 5.5.0

Editions

The LITE edition of Admin Bar gives you quick links to frequently-used pages in the Craft CMS Control Panel. You can also use settings to add custom links or rebrand Admin Bar with CSS.

The PRO edition adds interactive menus, statuses, and other information relevant to the page your users are on. Admin Bar PRO integrates Admin Bar with third-party plugins and Craft CMS features.

The LITE edition is free and the PRO edition can be purchased on the Craft CMS Plugin Store.

Installation

To install the plugin, you can find it in the Craft Plugin Store, or follow these instructions:

  1. Open your terminal and go to your Craft project:

    cd /path/to/project
  2. Then tell Composer to require the plugin:

    composer require wbrowar/craft-admin-bar
  3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Admin Bar.

Add Admin Bar to Your Twig Template

To add Admin Bar to your website add the {{ adminBar() }} tag within your Twig template. Admin Bar will be displayed on any page that includes this tag when someone—who has the permission to view the CP—is logged into your website.

In your plugin settings you can use CSS Custom Properties to override the look of your admin bar to match your website’s look and feel.

You may pass in an array of arguments to make some changes on how Admin Bar looks and functions. In this example, you may pass in the entry that you'd like to edit when someone clicks the "Edit" link.

{{ adminBar({ entry: entry }) }}

Here is a list of available arguments:

Adding Text Elements to Admin Bar

Plain text and labels can be added to Admin Bar by adding objects into the textElements argument. Object properties can include any property accepted by <admin-bar-text> elements, as documented here. Each object will add a new text element and the properties in each object will be added using Craft’s attr() method.

For example, this will add a line of text that will display the number of entries in the current entry’s section:

{{ adminBar({
    entry: entry ?? null,
    textElements: [
        { 'label-content': craft.entries.section(entry.section.handle).count(), 'text-content': 'Entries in this section' },
    ],
}) }}

Configuration settings

The config file gives you the ability to adjust how Admin Bar looks and functions in multiple environments. It also allows you to create additional links for the Admin Bar, and allows for plugin actions to be called through these additional links.

To add a config file to your site, create a file at /config/admin-bar.php that returns an array of settings:

<?php

return array(
    // Settings go here
);

Here are the settings you can change with the config file:

Admin Bar

Additional Links

You can add links to Admin Bar using the config file by passing properties into an array, called additionalLinks. There are examples commented out in the config.php file, and here are the properties you can use to create links.

Optimizing CSS and JS Delivery for Performance

By default, using {{ adminBar() }} will render HTML, and it will link to the static CSS and JS assets that make Admin Bar work. The CSS is loaded in a file in the <head> and the JavaScript is loaded in a file at the bottom of the <body> tag—deferred so it’s not render blocking.

Because these add extra requests to your server, you may prefer to move the CSS and JS code into your Twig template. First, to turn off the deafult loading behavior, add useCss and useJs arguments to your embed code and set them both to false:

{{ adminBar({
    entry: entry ?? null,
    useCss: false,
    useJs: false,
}) }}

This turns off loading any JavaScript or CSS onto the page. To load those assets back in, you can add the following anywere on your Twig template:

{% css adminBarCssFile({ contents: true }) %}
{% css adminBarOnPageCss() %}
{% js adminBarJsFile({ contents: true }) %}
  • adminBarCssFile() gets the CSS that comes with Admin Bar Component and adds that to the page.
    • Setting the contents argument to true will use PHP’s file_get_contents() method to extract the CSS code from the file as a string.
  • The adminBarOnPageCss() method adds some CSS that is specific to the Admin Bar plugin along with any CSS that was added in the Custom CSS Admin Bar plugin setting.
  • adminBarJsFile() gets the JavaScript code that comes with Admin Bar Component and adds that to the page.
    • The contents argument also uses file_get_contents() to get the JavaScript code to place inside of a <script> tag.

Note

How you embed your static assets are up to you. One less server request is usually better, but adding a few extra KB to every page in your HTML markup might not be as performant as caching and loading static .css and .js files.

Dynamically Loading Admin Bar

Using Admin Bar with Blitz Static Caching

The Blitz Craft CMS plugin provides a Twig function, called includeDynamic(), that lets you render a Twig template on a statically-cached page. With this function, Admin Bar can be dynamically included into your template so it will work on cached and uncached pages.

With Blitz installed, you can start by moving the {{ adminBar() }} method from your Twig templates into a separate Twig file in your templates directory. For this example, we’ll add the following code to a file at /templates/includes/admin-bar.twig.

{% set entry = entryUri ?? false ? craft.entries.uri(entryUri).one() : null %}

{{ adminBar({
  entry: entry ?? null,
  useCss: false,
  useJs: false,
}) }}

Notice that useCss and useJs are both set to false. This is because registering CSS and JS files from dynamic includes won’t work on a statically-cached page. You can set any other adminBar() argument that you’d like here.

Next, call the dynamicInclude() function in the place in your layout or page template where you would like Admin Bar to be embedded:

{{ craft.blitz.includeDynamic('includes/admin-bar.twig', { entryUri: entry.uri ?? '' }) }}

{% css adminBarCssFile() %}
{% css adminBarOnPageCss() %}
{% js adminBarJsFile() with { defer: true, type: 'module' } %}

You can pass parameters into includeDynamic(), so we can pass the URI of the current page entry—which will be used to figure out what page Admin Bar will use as the "Edit" button.

In order to load Admin Bar’s CSS and JS onto the front end, we can use Admin Bar’s built-in Twig functions anywhere within our template.

Admin Bar Widgets

Admin Bar PRO provides widgets based on the features you are using in Craft CMS and the other Craft CMS plugins you have installed in your project.

When you visit the Admin Bar Plugin Settings page you’ll see a section that lets you enable Admin Bar Widgets. Most of the widgets require that you pass an entry into your adminBar Twig tag ({{ adminBar({ entry: entry }) }}) in order to display relevant information for the current page you are on. The Preview area on the settings page will try to load an entry from your site and pass it into the Admin Bar Preview so you can see how the widgets would work for that entry.

The list of widgets that can be enabled will include some widgets based around Craft CMS features. Other widgets will be available if you have their third-party plugin installed. A list of supported plugins that are not installed will appear on the Admin Bar Settings page for reference.

When enabled, a widget might use the current user’s permissions or other factors to decide what to display. When no relevant information or actions are available a widget will not be shown.

Because Craft CMS plugins can change over time, features and availability of Admin Bar Widgets may change in future updates. Here is the list of currently supported widgets:

Requesting a New Admin Bar Widget

If you have an idea for an Admin Bar Widget, please start a new discussion on the Admin Bar Github Discussions page with your idea for a new widget. PRs are also welcome (but the final implementation may differ)!

While there are no hard rules for creating a widget, here is some general criteria:

  • When displayed on a page, the widget should provide information or actions relevant to the current page entry, current user, or the current Craft CMS Site.
  • Widgets should use stable, public APIs and Twig tags provided by Craft CMS or Craft CMS plugins.
  • Where possible, Widgets should be optimized for quick loading performance.
  • Widget code should follow accesibilty patterns and use web standard HTML and CSS.
  • Widgets shouldn’t expose any sensitive information.
  • Widgets should utilize the components built into Admin Bar Component to minimize CSS leaking in from the page Admin Bar is embedded on.

Releases

Release notes can be found at CHANGELOG.md

Supported Versions

Here is a general goal for adding and supporting features for Admin Bar going forward:

  • New features for the plugin will be added to the current major plugin version that targets the current released version of Craft CMS.
  • The latest major plugin version that targets the previous released major version of Craft CMS will be supported with bug fixes intruduced in updates to that version of Craft CMS.
  • Previous major plugin versions will only get security-related updates—when necessary.

Brought to you by Will Browar