An admin class extends Admin and changes menus and views in configureViews(). Toolbar actions consist of a PHP ToolbarAction and a JavaScript class under the same key. The admin is built in assets/admin with npm; install-links=true requires a fresh state in node_modules after a bundle file has changed.
Series 5: Sulu for developers Chapter 9 of 16
Extending the admin
In short
The admin of Sulu 3 is a React application that gets its structure from the server. The PHP counterpart is an admin class. It says which menu items, views and toolbar actions exist. This chapter shows both with examples from the website bertrams-media.
Helpful first: 5.1 Sulu 3 at a glance
The admin class
An admin class extends Sulu\Bundle\AdminBundle\Admin\Admin. It does not have to be registered as a service: SuluAdminExtension registers all subclasses by autoconfiguration, and the services.yaml of the example website loads src/ with autoconfigure: true. The most important methods are configureNavigationItems(), configureViews() and getSecurityContexts().
With getPriority() you determine the order of the admin classes. The example website returns -1000 when a class changes the views of other classes. It then runs after them and already finds their views.
final class ConfigSnippetTabsAdmin extends Admin
{
private const VIEW = 'sulu_snippet_manager_config.edit_tabs.content';
public static function getPriority(): int
{
return -1000;
}
public function configureViews(ViewCollection $viewCollection): void
{
if (!$viewCollection->has(self::VIEW)) {
return;
}
$viewBuilder = $viewCollection->get(self::VIEW);
if ($viewBuilder instanceof FormViewBuilderInterface) {
$viewBuilder->setTabTitle('app.config_tab_style');
}
}
}
The check with has() is deliberate. If the view is missing, for example because a bundle is not installed, the class does nothing instead of throwing an error.
Changing views
The ViewCollection knows has(), get() and all(). The example website goes through all views with all(), checks the name with a regular expression and sets a view type of its own for the snippet types. The type receives an option from which the frontend builds the heading:
foreach ($viewCollection->all() as $name => $viewBuilder) {
if (!preg_match('/^sulu_snippet_manager_([a-z0-9-]+)\.(?:edit|add)_tabs$/', (string) $name, $match)) {
continue;
}
$viewBuilder->setType('app.snippet_resource_tabs')
->setOption('headingKey', ucwords(str_replace('-', ' ', $match[1])) . ' Administration');
}
The type app.snippet_resource_tabs must be known in the frontend. That happens in assets/admin/app.js with the viewRegistry:
import {viewRegistry} from 'sulu-admin-bundle/containers';
viewRegistry.add('app.snippet_resource_tabs', SnippetResourceTabs, {disableDefaultSpacing: true});
A toolbar action
A toolbar action is an entry in the bar above a list. It consists of two parts with the same key. The SuluBulkActionsBundle shows both. On the PHP side, its admin class attaches the action to every list view it considers suitable:
foreach ($viewCollection->all() as $name => $viewBuilder) {
if (!$viewBuilder instanceof ListViewBuilderInterface) {
continue;
}
$actions = $this->actionsFor($name);
if ([] === $actions) {
continue;
}
$viewBuilder->addToolbarActions([
new ToolbarAction('sulu_bulk_actions.actions', ['actions' => $actions]),
]);
}
On the JavaScript side, a class sits under the same key that extends AbstractListToolbarAction and describes with getToolbarItemConfig() what the button looks like and what it does:
import {listToolbarActionRegistry} from 'sulu-admin-bundle/views';
import BulkActionsToolbarAction from './listToolbarActions/BulkActionsToolbarAction';
listToolbarActionRegistry.add('sulu_bulk_actions.actions', BulkActionsToolbarAction);
Finally, every action needs a security context. The bundle creates it in getSecurityContexts() (sulu.bulk_actions.actions) and also checks it in the context of the respective list. The server checks every entry once more.
Building the admin
The admin code lives in assets/admin/, separate from the website assets. In index.js it imports the bundles of Sulu and then app.js. The comment in index.js says that only the command sulu:admin:update-build should change the file. Your own code belongs in app.js. You add additional bundles in the package.json as a file: dependency and import them in app.js. The admin runs on React 17 and MobX 4.
cd assets/admin
npm install
npm run build # webpack --mode production
npm run watch # webpack --mode development --watch
The .npmrc in assets/admin sets install-links=true. As a result, npm copies the file: packages into node_modules and does not update them on another npm install. If you have changed a bundle, delete node_modules/<package>, then if needed clear the npm cache with npm cache clean --force, and install again. Use grep to check that the new file is in node_modules before you build.
The result lands in public/build/admin. The directory public/build/ is in the .gitignore; on a server you rebuild the admin or copy the result.