magento2 – Overriding GetDirectoryTree.php – preference not working

Having issues overriding MagentoMediaGalleryUiModelDirectoriesGetDirectoryTree.php.

It is related to a current Media Gallery bug that causes Folders not to display (https://github.com/magento/magento2/issues/30596)

Here is my setup:

di.xml

<preference for="MagentoMediaGalleryUiModelDirectoriesGetDirectoryTree"
type="VendorGeneralPluginMediaGalleryUiGetDirectoryTree"/>

GetDirectoryTree.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace VendorGeneralPluginMediaGalleryUi;

use MagentoFrameworkAppFilesystemDirectoryList;
use MagentoFrameworkExceptionValidatorException;
use MagentoFrameworkFilesystem;
use MagentoFrameworkFilesystemDirectoryReadInterface;
use MagentoFrameworkFilesystemGlob;
use MagentoMediaGalleryApiApiIsPathExcludedInterface;

/**
 * Build media gallery folder tree structure
 */
class GetDirectoryTree
{
    /**
     * @var Filesystem
     */
    private $filesystem;

    /**
     * @var IsPathExcludedInterface
     */
    private $isPathExcluded;

    /**
     * @var Glob
     */
    private $glob;

    /**
     * @param Filesystem $filesystem
     * @param Glob $glob
     * @param IsPathExcludedInterface $isPathExcluded
     */
    public function __construct(
        Filesystem $filesystem,
        Glob $glob,
        IsPathExcludedInterface $isPathExcluded
    ) {
        $this->filesystem = $filesystem;
        $this->glob = $glob;
        $this->isPathExcluded = $isPathExcluded;
    }

    /**
     * Return directory folder structure in array
     *
     * @return array
     * @throws ValidatorException
     */
    public function execute(): array
    {
        return $this->getDirectories();
    }

    /**
     * Read media directories recursively and build directory tree array in the jstree format
     *
     * @param string $path
     * @return array
     * @throws ValidatorException
     */
    private function getDirectories(string $path = ''): array
    {
        $directories = ();

        $absolutePath = $this->getMediaDirectory()->getAbsolutePath($path);
        foreach ($this->glob->glob(rtrim($absolutePath, '/') . '/*', Glob::GLOB_ONLYDIR) as $childPath) {
            $relativePath = $this->getMediaDirectory()->getRelativePath($childPath);
            if (!$this->isPathExcluded->execute($relativePath)) {
                $directories() = $this->getTreeNode($relativePath);
            }
        }

        return $directories;
    }

    /**
     * Format tree node based on path (relative to media directory)
     *
     * @param string $path
     * @return array
     * @throws ValidatorException
     */
    private function getTreeNode(string $path): array
    {
        $pathArray = explode('/', $path);
        return (
            'data' => count($pathArray) > 0 ? end($pathArray) : $path,
            'attr' => (
                'id' => $path
            ),
            'metadata' => (
                'path' => $path
            ),
            'path_array' => $pathArray,
            'children' => $this->getDirectories($path)
        );
    }

    /**
     * Retrieve media directory with read access
     *
     * @return ReadInterface
     */
    private function getMediaDirectory(): ReadInterface
    {
        return $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
    }
}

module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_General" setup_version="0.0.1">
        <sequence>            
            <module name="Magento_MediaGalleryUi"/>
        </sequence>
    </module>
</config>

Any ideas?

I have tried the plugin method too and it also did not work. I think I’m missing something that’s preventing me from pulling this class up.

UPDATE:

I looked into debug.log and saw this:

main.ERROR: Type Error occurred when creating object: MagentoMediaGalleryUiControllerAdminhtmlDirectoriesGetTreeInterceptor, Argument 3 passed to MagentoMediaGalleryUiControllerAdminhtmlDirectoriesGetTreeInterceptor::__construct() must be an instance of MagentoMediaGalleryUiModelDirectoriesGetDirectoryTree, instance of VendorGeneralPluginMediaGalleryUiGetDirectoryTree given, called in /var/www/php/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 121 () ()

I did go through the code generation steps and did remove generation folders:

php -dmemory_limit=6G bin/magento setup:upgrade
rm -rf var/generation/ var/view_preprocessed/ pub/static/frontend
php -dmemory_limit=6G bin/magento setup:di:compile
php -dmemory_limit=6G bin/magento setup:static-content:deploy -f
php -dmemory_limit=6G bin/magento cache:flush