Guide or documentation for creating new menu items with addNewFileMenuItems?

I’m working on a Nextcloud app and I’m trying to add new items to the “New” menu in the Files app. I’ve seen that there’s a function called addNewFileMenuItems that seems to be used for this purpose.

I’m looking for any official documentation, guides, or code examples that demonstrate how to properly use addNewFileMenuItems to create new menu entries. Specifically, I’d like to know:

  1. Is addNewFileMenuItems the correct method to use for adding new menu items in the latest Nextcloud version?
  2. Are there any best practices or guidelines for implementing this feature?
  3. Is there a comprehensive list of properties that can be set when adding a new menu item (like id, displayName, iconClass, etc.)?
  4. How should I handle the action when a new menu item is clicked?

Any guidance, documentation links, or code examples would be greatly appreciated.

Thank you for your time and assistance!

P.S. Is there a way to use it without build

Where did you find addNewFileMenuItems? I do not find anything at the moment about it. Or is this a typo and you are referring to addNewFileMenuEntry?

Sorry. I am not a programmer.

But the till Nextcloud 28 supprted app Transfer adds an entry to the menu e.g. with src/main.js . It uses addNewFileMenuEntry. Maybe it helps you.

Transfer would be a nice default function in Nextcloud, as it would partially save the diversions of a download via a client. Maybe someone would like to create a feature request.

It can’t be really complicated. Even the one-file file manager Tiny File Manager has this simple function.

Thank you to everyone who responded.

I hope more small sample codes become available to help grow the Nextcloud ecosystem.

I have one more issue. I want to automatically include the date in the filename, but it’s not working.

I am also trying to generate templates based on specific file extensions for the app to work properly, but it’s not working as expected. Do you have any recommended code or documentation for this?

<?php

declare(strict_types=1);

namespace OCA\DnaDoc\Listener;

use OCA\DnaDoc\AppInfo\Application;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Template\RegisterTemplateCreatorEvent;
use OCP\Files\Template\TemplateFileCreator;
use OCP\IL10N;

/** @template-implements IEventListener<RegisterTemplateCreatorEvent|Event> */
final class RegisterTemplateCreatorListener implements IEventListener {
    
    public function __construct(
        private IL10N $l10n
    ) {
    }

    public function handle(Event $event): void {
        // Ensure the event is of type RegisterTemplateCreatorEvent
        if (!($event instanceof RegisterTemplateCreatorEvent)) {
            return;
        }

        // Get the current date in 'Y-m-d' format
        $dateString = (new \DateTime())->format('Y-m-d');

        // Define the template types: Request, Plan, Result
        $types = [
            ['id' => 'request', 'name' => 'Request', 'extension' => '.req''],
            ['id' => 'plan', 'name' => 'Plan', 'extension' => '.pln'],
            ['id' => 'result', 'name' => 'Report', 'extension' => '.res'],
        ];

        // Register a template file creator for each type
        foreach ($types as $type) {
            $event->getTemplateManager()->registerTemplateFileCreator(function () use ($type, $dateString) {
                return $this->createTemplateFileCreator($type, $dateString);
            });
        }
    }

    private function createTemplateFileCreator(array $type, string $dateString): TemplateFileCreator {
        // Construct the file name in "Name-Date.extension" format
        $fileName = "{$type['name']}-{$dateString}{$type['extension']}";

        // Create a TemplateFileCreator instance
        $creator = new TemplateFileCreator(Application::APP_ID, $this->l10n->t($type['name']), $type['extension']);
        
        // Set the file mimetype to JSON (could be changed to any required type)
        $creator->addMimetype('application/json');
        
        // Set the inline SVG icon for the file type
        $creator->setIconSvgInline(file_get_contents(__DIR__ . "/../../img/{$type['id']}-icon.svg"));
        
        // Set the label for the user interface action
        $creator->setActionLabel($this->l10n->t("Create new {$type['name']}"));

        return $creator;
    }
}

image