Hook: editor_block_directories
avatar
Señor FAQ

¡Hola, amigos! I’m Señor FAQ, the mustached maestro of questions and answers! With my trusty glasses and a book of endless wisdom, I turn dudas into solutions. Soy el héroe de los curiosos and the champion of clarity.


Hook: editor_block_directories

The editor_block_directories hook allows plugins to register their own block directories for the block editor. This enables plugins to extend the editor by providing custom blocks that are dynamically loaded.

Usage


$pluginDirectories = DynamicHooks::executeHook('editor_block_directories', true);
if (!empty($pluginDirectories)) {
    foreach ($pluginDirectories as $pluginName => $pluginDir) {
        if (!is_dir(APP_PATH . $pluginDir)) {
            error_log("Invalid directory returned by plugin: $pluginName -> $pluginDir");
            continue;
        }
        if (is_dir(APP_PATH . $pluginDir)) {
            $pluginBlocks = scandir(APP_PATH . $pluginDir);
            $categories[$pluginName] = []; // Create a new category for the plugin

            foreach ($pluginBlocks as $block) {
                if ($block !== '.' && $block !== '..' && pathinfo($block, PATHINFO_EXTENSION) === 'php') {
                    $blockName = pathinfo($block, PATHINFO_FILENAME);
                    $title = ucwords(str_replace('_', ' ', $blockName));
                    $categories[$pluginName][] = [
                        'file' => $pluginDir . '/' . $block,
                        'title' => $title
                    ];
                }
            }
        }
    }
}
    

This hook collects block directories registered by plugins and loads the blocks dynamically into the editor.

Example Implementation


DynamicHooks::addHook('editor_block_directories', function () {
    return [
        'marketplace' => 'plugins/your_plugin/frontend/blocks'
    ];
}, 'your_plugin');
    

Best Practices

  • Ensure registered directories are valid and accessible.
  • Organize block files with clear and descriptive filenames.
  • Test blocks thoroughly to ensure they are correctly loaded and functional.

Troubleshooting

  • Verify that the hook is registered with the correct namespace.
  • Check for missing or inaccessible block directories.
  • Inspect logs for errors during directory scanning or block loading.
Was this article helpful?
0 out of 0 found this helpful